Home Insights How to sort parent documents by child attributes in Solr

How to sort parent documents by child attributes in Solr

How to sort parent documents by child attributes in Solr

E-commerce customers often need to sort products by price, size, and other SKU-level attributes. Our job is to make this process as easy and pleasant for them as we can, because the more products they find, the more they buy. How do we help them find what they need?For general searching and faceting in Solr/Lucene, we use Block Join Query. If users don’t know exactly what product they need or want to see our available products ranked by price or size, we run into the challenge of mapping the hierarchical structure of a typical e-commerce catalog into an inverted index.

Based on Grid Dynamics’ many years of Solr/Lucene experience, we suggest doing this with either the Index-time approach or the Query-time approach:

Index-time approach

Using this approach, we can collect and move up (propagate) values for sortable attributes from child documents to parent document during indexing. There is no built-in functionality in Solr that does this, so we need to write a bit of custom code to perform the following actions:

  • Extend the Update Processor Request’s framework with a new, dedicated processor.
  • The new processor receives instances of hierarchical SolrInputDocument and copies values from required attributes (for example, price) from all child documents to a multivalued attribute (for example,parent_price) on the parent document.
  • Insert the new processor into the processor chain.

If you’re using Data Import Handler for data population purposes, SOLR-9479 can help.

Now, at query time we just need to sort parent documents by min(max) values of their multivalued attributes.

One disadvantage of this approach is that we have to write our own code (at least with Solr 6 and lower). Another disadvantage is a possible false positive cross match. For eg. if a customer is searching for something “green,” which is a value of the SKU-level COLOR attribute, and wants to sort products by price in the results list, he’ll probably expect that products will be sorted according to  “green” SKU prices only. Unfortunately,  by implementing an index-time approach the results will be sorted by prices from all SKUs.

Query time approach

Fortunately, we can address both of index-time approach disadvantages by designing a Solr sorting clause to achieve the same results without “propagating,” i.e. moving attributes up from child to parent.  In order to do that, we are going to construct that clause based on a couple of Solr’s features.

Solr has a function query with syntax {!func}$field_name which is parsed into FunctionQuery. The score of this query is a $field_name’s attribute value. We can also sort documents by the scores of the function queries. The following clause will sort SKU documents by prices in descending order.

...sort={!func}price desc...

ToParentBlockJoinQuery supports several score calculation modes. For example, a score for a parent could be calculated as a min(max) score among of all its children’s scores. So, with the piece of code below we can sort parent documents by their children’s prices in descending order.

...sort={!parent which=doc_type:parent score=max v={!func}price} desc…

However, using this code will generate an exception because:

"msg": "Child query must not match same docs with parent filter.."

Basically, this code produces a limitation:  a child query inside ToParentBlockJoinQuery must match child and only child documents even if it is only used for sorting. To get around this, put a “children only” filter into the child query:

...sort={!parent which=doc_type:parent score=max v=’+doc_type:child +{!func}price’} desc…

The child filter can be extended in the same manner to take only prices from “green” SKUs into account, or any other appropriate SKU-level filter.

And that’s how to use ToParentBlockJoin for efficient hierarchical sorting.

Working with an inverted index isn’t hard, as you have just learned, and e-commerce customers will be glad you put in the effort to set it up. We say this over and over, because it’s always true: anything we can do that makes it easier for customers to buy from us is a good thing.

Stay tuned for future posts about interesting, often little-known or under-utilized Solr attributes. And, as always, if you have any questions, leave them as a comment below.

Tags

You might also like

Code on the left side with vibrant pink, purple, and blue fluid colors exploding across a computer screen, representing the dynamic nature of modern web development.
Article
Tailwind CSS: The developers power tool
Article Tailwind CSS: The developers power tool

When it comes to the best web development frameworks, finding the right balance between efficiency, creativity, and maintainability is key to building modern, responsive designs. Developers constantly seek tools and approaches that simplify workflows while empowering them to create visually strikin...

Cube emitting colorful data points, with blue, red, and gold light particles streaming upward against a black background, representing data transformation and AI capabilities.
Article
Data as a product: The missing link in your AI-readiness strategy
Article Data as a product: The missing link in your AI-readiness strategy

Most enterprise leaders dip their toe into AI, only to realize their data isn’t ready—whether that means insufficient data, legacy data formats, lack of data accessibility, or poorly performing data infrastructure. In fact, Gartner predicts that through 2026, organizations will abandon 60% of AI pr...

Multicolor whisps of smoke on a black background
Article
Headless CMS for the AI era with Grid Dynamics, Contentstack, and Google Cloud
Article Headless CMS for the AI era with Grid Dynamics, Contentstack, and Google Cloud

For many businesses, moving away from familiar but inherently unadaptable legacy suites is challenging. However, eliminating this technical debt one step at a time can bolster your confidence. The best starting point is transitioning from a monolithic CMS to a headless CMS. This shift to a modern c...

Orange blocks against a grey background to represent microservices in the cloud
Article
Cloud modernization playbook: From monolith to microservices
Article Cloud modernization playbook: From monolith to microservices

Many organizations have already embraced practices like Agile and DevOps to enhance collaboration and responsiveness in meeting customer needs. While these advancements mark significant milestones, the journey doesn't end here. Microservices offer another powerful way to accelerate business capabil...

Kubernetes use cases beyond container scheduling
Article
Kubernetes use cases beyond container scheduling
Article Kubernetes use cases beyond container scheduling

From AI/ML workloads and multi-tenancy to test labs and edge computing, uncover 5 practical examples of Kubernetes-based platform engineering.

The critical importance 
of accessibility testing 
and maintenance
Article
The critical importance of accessibility testing and maintenance: Envisioning the future with GenAI for industry-specific enhancements
Article The critical importance of accessibility testing and maintenance: Envisioning the future with GenAI for industry-specific enhancements

Accessibility is a critical factor for businesses across various industries, including retail, technology and media, insurance, FMCG, HORECA, and manufacturing. The potential impact of neglecting accessibility can be immense, not only from a legal standpoint but also in terms of lost revenue an...

Purple background with computer screens and shield icon to represent core web vitals
Article
The evolution of Google Web Vitals: What to expect beyond Core Web Vitals
Article The evolution of Google Web Vitals: What to expect beyond Core Web Vitals

Buckle up, web enthusiasts! We’re about to explore the fascinating world of Google’s Web Vitals—the crucial initiative that has reshaped how we approach web performance and user experience. My name is Maksym, and with more than 8 years in front-end development, I’ve seen firsthand how Web Vitals ha...

Get in touch

Let's connect! How can we reach you?

    Invalid phone format
    Submitting
    How to sort parent documents by child attributes in Solr

    Thank you!

    It is very important to be in touch with you.
    We will get back to you soon. Have a great day!

    check

    Something went wrong...

    There are possible difficulties with connection or other issues.
    Please try again after some time.

    Retry