1

I want to implement autocomplete feature in ES I have followed this tutorial from ES documentations

my Indexing is:

{
    "properties" : {
            "suggest" : {
                "type" : "completion"
            },
            "titleKeyword" : {
                "type": "keyword"
            }
        }
    }
}

I am putting text value in titleKeyword field

then I queried like follows

{
  "suggest" : {
    "my-suggestion" : {
      "text" : "iphone",
      "term" : {
        "field" : "titleKeyword"
      }
    }
  }
}

The result is:

...
    "suggest": {
        "my-suggestion": [
            {
                "text": "iphone",
                "offset": 0,
                "length": 6,
                "options": []
            }
        ]
    }

expected result is

options:[iphone x, iphone 11, iphone 11 pro, iphone 7]

Note: titles are complete sentences like "iPhone x 64gb black"

as far as I understood from the documentation that I have to give the suggestions in "advance" when creating the document which is impossible in my case is there any way to implement this feature without putting suggestions manually?

Thanks

Community
  • 1
  • 1
  • so basically you have documents conatining iphone x, iphone 11, iphone 11 pro, iphone 7 and when searched for `iphone` you want all of them to appear in search result ? – Amit Apr 02 '20 at 13:05
  • @OpsterElasticsearchNinja exactly like that –  Apr 02 '20 at 13:46
  • Provided answer according to your requirement :) – Amit Apr 02 '20 at 14:17
  • did you get a chance to go through my answer – Amit Apr 02 '20 at 16:32
  • @OpsterElasticsearchNinja Thank you very much for your answer but unfortunately, it doesn't address my case exactly I could do regex query and fetch the documents that contain the queried text (since the title may contains more tahn one word like iphone x 32gb storage silver ) so when i write iphone i want to suggest to me iphone x, iphone 7 and so on i.e not the document title but a keyword from the document title –  Apr 02 '20 at 19:10

1 Answers1

1

For this simple requirement, no need to use the Elasticsearch suggestor which is difficult to understand and some part of it is still under development, More info on what all to take care when building advance autocomplete is in my this SO answer.

Coming to your requirements which can be easily solved using the text field(looks like you are using keyword type) and simple match query as shown below:

Index Def

{
    "mappings": {
        "properties": {
            "model_name": {
                "type": "text"
            }
        }
    }
}

Index all 4 types of iphone

{
   "model_name" : "iphone x"
}

{
   "model_name" : "iphone 11"
}

{
   "model_name" : "iphone 11 pro"
}

{
   "model_name" : "iphone 7"
}

Search query (iphone will bring all four docs)

{
    "query": {
        "match": {
            "model_name": {
                "query": "iphone"
            }
        }
    }
}

Search result

{
            "_index": "so_auto",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.110377684,
            "_source": {
               "model_name": "iphone x"
            }
         },
         {
            "_index": "so_auto",
            "_type": "_doc",
            "_id": "2",
            "_score": 0.110377684,
            "_source": {
               "model_name": "iphone 11"
            }
         },
         {
            "_index": "so_auto",
            "_type": "_doc",
            "_id": "4",
            "_score": 0.110377684,
            "_source": {
               "model_name": "iphone 7"
            }
         },
         {
            "_index": "so_auto",
            "_type": "_doc",
            "_id": "3",
            "_score": 0.09271726,
            "_source": {
               "model_name": "iphone 11 pro"
            }
         }
      ]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Amit
  • 30,756
  • 6
  • 57
  • 88