弹性搜索完成建议多场

时间:2014-09-19 11:15:40

标签: elasticsearch search-suggestion

我试图从多字段中获取建议。我无法找到这样的例子,所以也许这不是最好的主意,但我对你的意见感兴趣。

映射:

POST /authors
    {
       "mappings": {
          "author": {
             "properties": {
                "name": {
                   "type": "multi_field",
                   "fields": {
                      "name": {
                         "type": "string",
                         "index": "analyzed"
                      },
                      "ac": {
                         "type": "completion",
                         "index_analyzer": "simple",
                         "search_analyzer": "simple",
                         "payloads": true
                      }
                   }
                }
             }
          }
       }
    }

数据:


POST /authors/author/1
    {
       "name": "Fyodor Dostoevsky"
    }

查询:

POST /authors/_suggest

    {
       "authorsAutocomplete": {
          "text": "fyodor",
          "completion": {
             "field": "name.ac"
          }
       }
    }

要求是:

  • 让查询适用于文本" fyodor"以及" dostoevsky",此示例仅适用于" fyodor"
  • 启用过滤建议

任何想法如何实现这些目标?

1 个答案:

答案 0 :(得分:3)

首先,建议者在多领域不能很好地工作,所以你可能想把它放在外面。 其次,为了使您使用名称和名字进行查询工作,您必须在发布数据时选择输出/输入。

SENSE的工作代码示例:

POST authors

PUT authors/_mapping/author
{
    "properties" : {
        "name" : { "type" : "string" },
        "suggest" : { "type" : "completion"}
    }
}

POST authors/author/1
{
    "name": "Fyodor Dostoevsky",
    "suggest": {
        "input": ["Dostoevsky", "Fyodor"],
        "output": "Fyodor Dostoevsky"
    }
}

POST authors/_suggest
{
    "authorsAutocomplete": {
        "text": "d",
        "completion": {
            "field": "suggest"
        }
    }
}

DELETE authors

结果:

{
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "authorsAutocomplete": [
        {
            "text": "d",
            "offset": 0,
            "length": 1,
            "options": [
                {
                    "text": "Fyodor Dostoevsky",
                    "score": 1
                }
            ]
        }
    ]
}

过滤器不适用于建议。要实现某种过滤,您可以在建议中查看有关上下文使用的blog post