Elasticsearch Suggest Search APIs

I use the Suggest API to create autocomplete for restaurant names, but I have a little problem. Some restaurant names start with numbers, for example:

68 - 86 Bar & Restaurant

I want to recruit 68and return the restaurant. I tried using a space analyzer, but this does not fix my problem.

Here is the analysis result for the restaurant name:

{
  "tokens": [
    {
      "token": "68",
      "start_offset": 0,
      "end_offset": 2,
      "type": "<NUM>",
      "position": 1
    },
    {
      "token": "86",
      "start_offset": 5,
      "end_offset": 7,
      "type": "<NUM>",
      "position": 2
    },
    {
      "token": "bar",
      "start_offset": 8,
      "end_offset": 11,
      "type": "<ALPHANUM>",
      "position": 3
    },
    {
      "token": "restaurant",
      "start_offset": 14,
      "end_offset": 24,
      "type": "<ALPHANUM>",
      "position": 4
    }
  ]
}

Here are the commands to reproduce my problem:

PUT restaurants
{ }

PUT restaurants/restaurant/_mapping
{
    "location": {
        "index_analyzer": "whitespace",
        "search_analyzer": "whitespace", 
        "properties": {
            "name_suggest": {
                "type": "completion",
                "payloads": true
            }
        }
    }
}

POST restaurants/restaurant/1
{
    "name_suggest": {
        "input": [
            "68 - 86 Bar & Restaurant"
        ],
        "output": "68 - 86 Bar & Restaurant",
        "payload": { 
            "id": 1067
        }
    }
}

POST restaurants/_suggest
{
    "suggestions": {
        "text": "68 - 86",
        "completion": {
            "field": "name_suggest"
        }
    }
}

I am not getting any results from _suggest. Any help would be appreciated.

+4
source share
1 answer

I solved it, simple, but maybe a mistake?

Instead:

PUT restaurants/restaurant/_mapping
{
    "location": {
        "index_analyzer": "whitespace",
        "search_analyzer": "whitespace", 
        "properties": {
            "name_suggest": {
                "type": "completion",
                "payloads": true
            }
        }
    }
}

Now I have:

PUT restaurants/restaurant/_mapping
{
    "location": {
        "properties": {
            "name_suggest": {
                "type": "completion",
                "index_analyzer": "whitespace",
                "search_analyzer": "whitespace",
                "payloads": true
            }
        }
    }
}
+5
source

Source: https://habr.com/ru/post/1524270/


All Articles