Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch context suggester, bool on contexts

I'm using context suggester and am wondering if we can set the scope of the context to be used for suggestions rather that using all contexts.

Currently the query needs to match all contexts. Can we add an "OR" operation on the contexts and/or specify which context to use for a particular query?

Taking the example from here : Mapping :

PUT /venues/poi/_mapping
{
  "poi" : {
    "properties" : {
      "suggest_field": {
        "type": "completion",
        "context": {
          "type": { 
            "type": "category"
          },        
          "location": { 
            "type": "geo",
            "precision" : "500m"
          }
        }
      }
    }
  }
}

Then I index a document :

 {
  "suggest_field": {
    "input": ["The Shed", "shed"],
    "output" : "The Shed - fresh sea food",
    "context": {
      "location": {
        "lat": 51.9481442,
        "lon": -5.1817516
      },      
      "type" : "restaurant"
    }
  }
}

Query:

{
  "suggest" : {
    "text" : "s",
    "completion" : {
      "field" : "suggest_field",
      "context": {
        "location": {
          "value": {
            "lat": 51.938119,
            "lon": -5.174051
          }
        }
      }
    }
  }
}

If I query using only one Context ("location" in the above example) it gives an error, I need to pass both the contexts, is it possible to specify which context to use? Or pass something like a "Context_Operation" parameter set to "OR".

like image 992
Partinder Singh Avatar asked Apr 24 '15 08:04

Partinder Singh


1 Answers

You have 2 choices:

First you add all available type values as default in your mapping (not scalable)

{
  "poi" : {
    "properties" : {
      "suggest_field": {
        "type": "completion",
        "context": {
          "type": { 
            "type": "category",
            "default": ["restaurant", "pool", "..."]
          },        
          "location": { 
            "type": "geo",
            "precision" : "500m"
          }
        }
      }
    }
  }
}

Second option, you add a default value to every indexed document, and you add only this value as default

Mapping:

{
  "poi" : {
    "properties" : {
      "suggest_field": {
        "type": "completion",
        "context": {
          "type": { 
            "type": "category",
            "default": "any"
          },        
          "location": { 
            "type": "geo",
            "precision" : "500m"
          }
        }
      }
    }
  }
}

Document:

{
  "suggest_field": {
    "input": ["The Shed", "shed"],
    "output" : "The Shed - fresh sea food",
    "context": {
      "location": {
        "lat": 51.9481442,
        "lon": -5.1817516
      },      
      "type" : ["any", "restaurant"]
    }
  }
}
like image 101
Julien C. Avatar answered Sep 17 '22 15:09

Julien C.