1

My use-case is a bit complicated so I'm simplifying it by using products and purchases:

The application has a big database with varies tables, among them - products and purchases (many to many: user_id:product_id). Elastic has an index for the products only, as this is the only entity needed an advanced/high scale search.

What I'm trying to achieve is as following: The more times the current user bought a product, the more relevant I want it to be.

The tricky part is the fact that Elastic has an index of the products only, not the purchases. I can execute a query in the DB and get the info of how many times a user bought a product, and pass the results to Elastic, the question is how to do it.

Thanks.

David
  • 2,528
  • 1
  • 23
  • 29
  • How about adding a field named `nb_purchases` in your product documents that you can increment on each purchase? Then you could use that field for relevance, sorting, etc. – Val Dec 03 '15 at 15:38
  • The problem is that this field would be per user. In the DB there are thousands of users, so adding this field will increase the product document size dramatically. Also, it would affect the application performance, because it'll need to update the product document every purchase. – David Dec 03 '15 at 15:45
  • Oh, makes sense, I didn't realize it was meant to be per user. – Val Dec 03 '15 at 15:50

1 Answers1

2

If you can produce a reasonably-bounded purchase history for each searcher, you could implement this inside a bool query using a list of optional should block term queries

E.g.

"bool": {
  "must": [ <existing query logic> ],
  "should": [
    {
       "term": { "product_id": 654321 },
       "boost": 3 <e.g. Purchased 3 times>
    },
    {
       ...
    }
  ]
}

As a heads-up, evaluating large numbers of these optional Boolean clauses will degrade your query performance, so you might also consider using a rescore request to apply your boosting logic to only, say, the top 100 unboosted search hits, if that would satisfy your requirement.

Peter Dixon-Moses
  • 3,169
  • 14
  • 18