Elasticsearch建议API - 不能搜索数字

时间:2014-01-29 11:58:53

标签: autocomplete elasticsearch autosuggest

我正在使用Suggest API为餐馆名称创建自动完成功能,但我遇到了一个小问题。一些餐馆名称以数字开头,例如:

68 - 86 Bar & Restaurant

我希望能够输入68并让餐馆回来。我尝试过使用空白分析器,但它并没有解决我的问题。

以下是餐馆名称的分析输出:

{
  "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
    }
  ]
}

以下是重现我的问题的命令:

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"
        }
    }
}

我没有从_suggest获得任何结果。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:6)

我已经解决了,很简单,但也许是一个错误?

而不是:

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

我现在有:

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