弹性搜索完成建议多场

Ins*_*Ins 6 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"
  • 可以过滤建议

任何想法我怎样才能实现这些?

Hes*_*oon 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
Run Code Online (Sandbox Code Playgroud)

结果:

{
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "authorsAutocomplete": [
        {
            "text": "d",
            "offset": 0,
            "length": 1,
            "options": [
                {
                    "text": "Fyodor Dostoevsky",
                    "score": 1
                }
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

过滤器不适用于建议。要实现某种过滤,您可以查看这篇关于在建议中使用上下文的博客文章。