Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch Suggest API -Can't search numbers

I'm using the Suggest API to create an autocomplete for restaurant names, but I've run into a small problem. Some restaurant names start with numbers, for example:

68 - 86 Bar & Restaurant

I want to be able to type 68 and get the restaurant back. I've tried using the whitespace analyzer, but it doesn't fix my issue.

Here is the analyze output 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 issue:

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 don't get any results from _suggest. Any help would be appreciated.

like image 632
user1283225 Avatar asked Jan 29 '14 11:01

user1283225


1 Answers

I've solved it, simple really, but maybe a bug?

Instead of:

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

I now have:

PUT restaurants/restaurant/_mapping
{
    "location": {
        "properties": {
            "name_suggest": {
                "type": "completion",
                "index_analyzer": "whitespace",
                "search_analyzer": "whitespace",
                "payloads": true
            }
        }
    }
}
like image 90
user1283225 Avatar answered Sep 27 '22 21:09

user1283225