Taming Search using Rules

You cannot tame a dragon with a history lesson.
~George R.R. Martin. 🐉

The Lucene powered Search Engines like Solr and Elasticsearch are Information Retrieval Beasts, with enormous potential. At Walmart Labs, ASDA.com, there was a phase. We were migrating from the closed Blackbox Oracle Endeca Server which is a hybrid search-analytical database to the mighty Opensource Dragon, Apache Solr which is a popular, blazing-fast, open-source enterprise search platform built on Apache Lucene. In this blog will cover the learning of a tiny portion of our journey.

In this blog, will look on the solutions for Queries like :

  1. Boost some products of brand apple when people search for the iPhone.

and many more, We face such scenarios more often in eCommerce.

Origin

A coin has two sides

A similar way an e-commerce search has two aspects one is the Customer, and the other is Merchandisers. All of us are familiar with the Customer side and the relevance tuning. Here we will discuss the other side of the wall, i.e., Merchandisers.

Business knowledge translated to the Business rules by merchandisers, which help us to improve the CTR. These are useful because merchandisers have done already extensive research on user behaviour and customer experience analysis. A rule-based system is an excellent alternative AI approach in terms of how to make the search experience better. Reason for this is

1. Easy to implement

2. Easy to understand and main

3. More accessible to non-technical users.

The Better search experience is the one where users can find what they are looking for with less effort of filtering, sorting or pagination.

As an example, if someone searcher for Apple or starting with a patter iPhone 5/6/7/X/XR, instead of showing them any product which has apple in it, we just want it to see Apple Electronic products on top. Rules came handy in such cases.

Endeca already has a rule engine called XM(Experience Manager), where merchandisers use to put rules, for the search and catalogue navigation browsing. While migrating from Endeca to Solr, one of the biggest mountains was, How to make sure that such functionality should be there in the new Search Engine.

Reaching Foothills of Misty Mountain

Then, we went through a lot of brainstorming to identify how can we take the rules out to the new system. One of the ideas was putting the rules in the key-value store like Redis and then retrieve them for the search term. But It wasn’t that easy as it looks.

This idea got rejected as there were many problems with the idea few of them were the search terms can be stemmed, and there could be a partial phrase search.

A Rule in general consist of 4 major parts:

  1. Search Term, for which rule is applicable.

SearchTerm for which rules have been defined can also have matching criteria, i.e. how it should match the Query, for example:

  • It is precisely matching the SearchTerm, i.e. Match Exact.

After lots of discussions, thinking, and POC’s, we get two options for Rule Engine:

  1. One of them which I came up with was to keep Rules within SOLR as a separate collection.

In this blog, we will focus on our encounter with Querqy.

Climbing the Forest of Rules: Querqy

Querqy is a framework for query preprocessing in Java-based search engines. It comes with a powerful, rule-based preprocessor named ‘Common Rules’ rewriter, which provides query-time synonyms, query-dependent boosting and down-ranking, and query-dependent filters.
René Kriegler, Committer/Maintainer

In this blog, we will explain about Querqy, how to install it under Solr and how to configure rules.

When we encountered Querqy, It looks for the best fit for our problem statement. Querqy internally used search based on trie, which is pretty fast. But there also we found some gaps in what Querqy provides and what we wanted.

Querqy solved our search and instructions, but still, we have multiple criteria to select the rule and some other information like facets which we need to apply. We discuss these possibilities with René Kriegler, and after discussions, and several pull requests, the solution was found, and we contributed that to the Querqy.

The Criteria includes the Filters, Limit and Sort.

  • Filters: For a search term, select the rules with specific filters; for example, the rule must be active.

A sample querqy rule looks like:

#This format using the @ character
notebook =>
SYNONYM: laptop
DOWN(100): case
@_id: "ID1"
@_log: "notebook,modified 2019-04-03"
@group: "electronics"
@enabled: true
@priority: 100
@tenant: ["t1", "t2", "t3"]
@culture: {"lang": "en", "country": ["gb", "us"]}
#This format represents the properties as a JSON object
notebook =>
SYNONYM: laptop
DOWN(100): case
@{
_id: "ID1",
_log: "notebook,modified 2019-04-03",
group: "electronics",
enabled: true,
priority: 100,
tenant: ["t1", "t2", "t3"],
culture: {
"lang": "en",
"country": ["gb", "us"]
}
}@

Moving in-depth

Querqy implementation is straightforward to understand. From the rules, provided in a file in the given format It stores them in a prefix trie and suffix trie.

This implementation helps us to achieve a partial match and full match of the SearchTerm.

There are various rewriters, which help the parser/handler to rewrite the query after fetching the rule from the Querqy.

Querqy treats all the actions as Instructions which needs to get applied in the query for example Sort, Boost, Burry, Filter, Delete.

And to select the specific rule, there are various criteria which someone can provide for example Date Range, State of activeness, Priority and many more.

Nirvana: When Solr Meets Querqy

There is a path for everyone

Querqy was pretty flexible when it comes to adding it to Solr. It is pretty easy and quick. Querqy read all the rules from the file rules.txt and internally creates a Prefix Trie for the matching, and once after all the preprocessing completed, our Index is ready to serve the Rules.

When a new Query comes, we fetch the Rule from Querqy, then instructions from Rules will be applied to the Query. These instructions could be:

  1. Decorate
    Decorate rules are not strictly Query rewriting rules, but they are quite handy to add Query-dependent information to search results.
<queryParser name="querqy" class="querqy.solr.DefaultQuerqyDismaxQParserPlugin">

<lst name="rewriteChain">
<lst name="rewriter">
<!--
Note the rewriter ID:
-->
<str name="id">common1</str>
<str name="class">querqy.solr.SimpleCommonRulesRewriterFactory</str>
<str name="rules">rules.txt</str>
<!-- ... -->

<!--
Define a selection strategy, named 'expr':
-->
<lst name="rules.selectionStrategy">
<lst name="strategy">
<str name="id">expr</str>
<!--
This selection strategy implementation allows us to select and order rules by properties:
-->
<str name="class">querqy.solr.ExpressionSelectionStrategyFactory</str>
</lst>
</lst>
</lst>
</lst>

Release the Dragon 🐲

After the querqy gets integrated with Solr, our Beast was ready to serve the need of merchandisers.

We created an Ingestion flow to Solr, which will discuss in follow up Posts.

But there was one problem, which might come that was Since Solr Cloud uses Zookeeper for config sync. What if rules file exceeded the 1MB file size limit of Zookeeper? This question also got resolved, with recent modifications in querqy, Querqy now supports GZ compressed file format along with that one can provide a list of rules files comma separated.

In My Opinion, Querqy seems to be a good fit for such kind of problems. It’s simpler and quite performant.

Querqy also has a pretty good UI to manage and publish the rules.

To make a note here, Querqy is available for both Solr and Elasticsearch.

Conclusion

In this blog, we discussed how we could incorporate Rules inside search. This approach can provide a massive advantage to the Merchandisers.

We discussed one of the approaches we discovered, i.e. Querqy. I will add another follow-up post where we will discuss the other strategy, i.e. Rules in a Collection.

Thanks for reading. Please keep watching the space for more updates.

References

  1. https://github.com/querqy/querqy

Image Ref:
1. http://getyourimage.club/resize-15-may.html
2.https://www.reddit.com/r/skyrim/comments/1ebblg/dragons_in_a_mountain_after_the_fight_with_alduin/

Information Retrieval Enthusiast | Software Engineer @Walmartlabs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store