Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch 5.x context suggester with multiple contexts

I want to use the context suggester from elasticSearch, but my suggestion results need to match 2 context values.

Expanding the example from the docs, i want to do something like:

POST place/_search?pretty
{
    "suggest": {
        "place_suggestion" : {
            "prefix" : "tim",
            "completion" : {
                "field" : "suggest",
                "size": 10,
                "contexts": {
                    "place_type": [ "cafe", "restaurants" ],
                    "rating": ["good"]
                }
            }
        }
    }
}

I would like to have results that have a context 'cafe' or 'restaurant' for place_type AND that have the context 'good' for rating.

When I try something like this, elastic performs an OR operation on the contexts, giving me all suggestions with the context 'cafe', restaurant' OR 'good'.

Can I somehow specify what BOOL operator elastic needs to use for combining multiple contexts?

like image 983
Dries Cleymans Avatar asked Sep 22 '17 12:09

Dries Cleymans


1 Answers

It looks like this functionality isn't supported from Elasticsearch 5.x onwards: https://github.com/elastic/elasticsearch/issues/21291#issuecomment-375690371

Your best bet is to create a composite context, which seems to be how Elasticsearch 2.x achieved multiple contexts in a query: https://github.com/elastic/elasticsearch/pull/26407#issuecomment-326771608

To do this, I guess you'll need a new field in your mapping. Let's call it cat-rating:

PUT place
{
  "mappings": {
    "properties": {
      "suggest": {
        "type": "completion",
        "contexts": [
          {
            "name": "place_type-rating",
            "type": "category",
            "path": "cat-rating"
          }
        ]
      }
    }
  }
}

When you index new documents you'll need to concantenate the fields place_type and rating together, separated by -, for the cat-rating field. Once that's done your query will need to look something like this:

POST place/_search?pretty
{
  "suggest": {
    "place_suggestion": {
      "prefix": "tim",
      "completion": {
        "field": "suggest",
        "size": 10,
        "contexts": {
          "place_type-rating": [
            {
              "context": "cafe-good"
            },
            {
              "context": "restaurant-good"
            }
          ]
        }
      }
    }
  }
}

That'll return suggestions of good cafe's OR good restaurants.

like image 75
ssjoleary Avatar answered Oct 02 '22 14:10

ssjoleary