Context suggestor in elastic search applied on the nested fields

I came across a problem to implement the context suggester of elastic search for the auto-complete search. In my case i have the index document as: explained bellow-->>

I need to get the document which should have status active, and the object matched in the "nestedField" corresponding to the values of:"nestField1" and "nestField2" in the object:

  {
    "nestField1":"nestValue5",
    "nestField2":"nestValue6",
    ...
    }

I need to apply the context suggester for this scenario. I tried context mapping discussed down the page:

but this didn,t work for me. To implement this I mainly refer the links as: https://www.elastic.co/guide/en/elasticsearch/reference/current/suggester-context.html

The only link I got on the stack overfolw Which seems I guess similar, didnt give much idea about this though. Elasticsearch completion suggester context for nested fields

You may refer the link as: https://github.com/elastic/elasticsearch/issues/6444

as per suggestion I make some changes in the context mapping as:

      "suggest_field": {
    "type": "completion",
    "analyzer": "simple",
    "payloads": false,
    "preserve_separators": true,
    "preserve_position_increments": true,
    "max_input_length": 50,
    "context": {
      "netsedField_nestField1_ctx": {
        "type": "category",
        "path": "netsedField.nestField1",
        "default": [
          "unknown"
        ]
      },
      "netsedField_nestField2_ctx": {
        "type": "category",
        "path": "netsedField.nestField2",
        "default": [
          "Enable"
        ]
      },
      "status_ctx": {
        "type": "category",
        "path": "status",
        "default": [
          "Active"
        ]
      }
    }
  }

and my documents are as follows:

"hits": [
{
"_index": "my_index1",
"_type": "subscriber",
"_id": "2",
"_score": 1,
"_source": {
"device": {
"brand": "Brand one",
"model": "bRAND One"
},
"personal": {
"login": "login",
"firstName": "first name",
"lastName": "last name",
"address": "address",
"zipCode": "zip code",
"city": "city",
"country": "COUNTRY",
"language": "LanGuage"
},
"suggest_field": {
"input": [
"Brand",
"bRAND",
"One"
],
"output": "Brand One,1-3 disable,2-enable",
"weight": 10
},
"netsedField": [
{
"nestField1": 1,
"nestField2": "Disable"
},
{
"nestField1": 2,
"nestField2": "Enable"
},
{
"nestField1": 3,
"nestField2": "Disable"
}
],
"status": "Active"
}
},
{
"_index": "my_index1",
"_type": "subscriber",
"_id": "1",
"_score": 1,
"_source": {
"device": {
"brand": "Brand one",
"model": "bRAND One"
},
"personal": {
"login": "login",
"firstName": "first name",
"lastName": "last name",
"address": "address",
"zipCode": "zip code",
"city": "city",
"country": "COUNTRY",
"language": "LanGuage"
},
"suggest_field": {
"input": [
"Brand",
"bRAND",
"One"
],
"output": "Brand 1 ,1-2 enable,3 disable",
"weight": 10
},
"netsedField": [
{
"nestField1": 1,
"nestField2": "Enable"
},
{
"nestField1": 2,
"nestField2": "Enable"
},
{
"nestField1": 3,
"nestField2": "Disable"
}
],
"status": "Active"
}
}
]

and I am hitting the suggest request as: POST host:port/index/_suggest

{
"my_index1" : {
"text" : "b",
"completion" : {
"field" : "suggest_field",
"context":{
"status_ctx":"Active",
"netsedField_nestField2_ctx":"Enable",
"netsedField_nestField1_ctx":"1"
}
}
}
}

and the response I am getting are as

{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"my_index1": [
{
"text": "b",
"offset": 0,
"length": 1,
"options": [
{
"text": "Brand 1 ,1-2 enable,3 disable",
"score": 10
},
{
"text": "Brand One,1-3 disable,2-enable",
"score": 10
}
]
}
]
}

but in response i Dont need :

{
"text": "Brand One,1-3 disable,2-enable",
"score": 10
}

as part of options, as in the _suggest query i am giving the context values :

  "netsedField_nestField2_ctx":"Enable",
  "netsedField_nestField1_ctx":"1"

Please tell me where am I making the mistake.

1 Like

Hi,

Here is the link for a feature which solves your problem. Please go through and provide your feedback.

1 Like