The previous posts in this series focused on aggregations with which we did some faceting and analytics. Now let us put aside aggregations for a while and check out some other exciting features of Elasticsearch that could be put to good use in our e-commerce context. This post introduces Percolation as a useful tool from the merchant’s perspective.

Percolation is a feature by which you get notified based on your registered interests. For example, assume you are interested in Elasticsearch as a topic. Wouldn’t it be great if a tool existed that would notify you whenever anything related to Elasticsearch gets updated or added in the web? Google, for example, has an alert feature that is a percolator, in essence. It is not not exactly comprehensive, however, since it relies on both content getting spidered and that content meeting a minimum popularity threshhold. Since v.0.15, Elasticsearch has also shipped such a feature. In the right hands, it can do some amazing things for you.

A useful analogy for a percolator is a reverse search engine. Typically, users type a query into a search box and are then served results that match the query. A percolator will take a document and tell you which queries resulted in this document getting served.

To make use of the percolation feature, we first must register our interests in the form of queries expressed in json. Once registered, you can percolate any document with respect to your registered queries. The queries we index in the percolator is stored in the index itself under a special type .percolator as a special structured document. For more on comprehensive percolator features, feel free to reference Qbox developer Michael Lussier ‘s blog post Introduction to Elasticsearch Percolate.

What does percolation offer in terms of e-commerce applications? Let us go through a couple of use cases so that you can understand how percolation can be put to good use. Imagine you are running an ecommerce store, and an elite and valuable user asks you to alert them when a new product from a particular brand is released. For example, a shoe store may want to let users know when the new Air Jordans come out. Other users may want notifications when the price of those Air Jordans falls below a threshold. In both cases, percolation is the answer.

If your application is a two-sided market, percolation can help facilitate transactions. Say you have a real-estate search application. Percolation can help your followers know when a new property is available that matches their preferences for price, location, or any number of criteria.

To get our hands wet on percolation with the first use case we discussed, say we need to get notified when Apple releases a new product. Yes, we are aware that in the real world, Apple’s PR machine rarely makes notifications of new product releases necessary, but bear with us. Initially, we need to register our interest within the percolator as a query. For this purpose, we define a query on the ‘brand’ field in our ecomercedata index.

curl -XPUT 'localhost:9200/ecomercedata/gadgets/.percolator/1' -d '{<br">   "query" : { <br">       "match" : {<br">           "brand" : "Apple"<br">       }<br">   }
</br"></br"></br"></br"></br">}'

Registering this query will give you a response like this.

{
   "_index": "ecomercedata",
   "_type": ".percolator",
   "_id": "1",
   "_version": 1,
   "created": true
}

Having registered our query in the percolator, now we can percolate documents and the relevance can be found like this.

curl -XPOST "http://localhost:9200/ecomercedata/gadgets/_percolate/" -d '{<br">  "doc": {<br">          “name" : "ipad Air", 
      "category" : "Tablet",
      "brand" : "Apple",
      "rating" : 9,
      "prize" :  999.00,
      "piecesSold" : 200,
      "dateOfRelease" : "2014-02-01"
  }
}'</br"></br">

which gives the response that it matched like shown below.

{
   "took": 11,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "total": 1,
   "matches": [
      {
         "_index": "ecomercedata",
         "_id": "1"
      }
   ]
}

Now let us assume another use case where your user is only interested in products that cost less than a particular amount or that fall in a particular range. Let us see adding this constraint to the above use case..

PUT /ecomercedata/.percolator/2
{
"query" : { 
 "bool" : {
   "must" : {
    "term" : { "brand" : "Apple" }
        },
    "should" : {
      "range" : {
        "prize" : { "lte" : 1000 }
     }
    }
   }
  }
 }

Here we indexed another query with ‘id = 2‘ which also checks if the ‘prize’ constraint matches or not, along with the brand name.

Percolating the same document will give 2 matches as shown below since both the constraints we checked for matched.

{
   "took": 13,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "total": 2,
   "matches": [
      {
         "_index": "ecomercedata",
         "_id": "2"
      },
      {
         "_index": "ecomercedata",
         "_id": "1"
      }
   ]
}

Try changing the prize value to anything above 1000. We can see our percolator with ‘id = 2’ does not match.

The flexibility of Elasticsearch queries can be applied to create percolator queries that can match specific criterias. What we did with percolations is actually a reverse search. We normally run queries on documents and get relevant results back. Here we percolate documents against queries and find matches with respect to queries. Hope this can get you started with percolation. Stay tuned for more info on other exciting features of Elasticsearch soon.