使用完成建议程序获取除匹配前缀以外的其他文档

时间:2018-07-25 15:49:27

标签: elasticsearch autocomplete autosuggest

我需要对索引实施完成建议。我需要这样的结果:首先输入以文本开头的文档,然后是其他建议。顺序很重要。我的字段“名称”被索引为建议输入。

例如,如果我搜索“ am”,则安利应该是第一个结果,然后是相机,依此类推,尽管我得到的唯一结果是以“ am”开头的建议。另外,我只需要使用完成提示器,而不需要其他查询。我只获取其建议输入以前缀开头的文档。有没有一种方法可以调整分析仪以产生这样的结果?还是有其他方法?

这就是我的映射的样子-

    "realtime": {
        "properties": {
          "suggest" : {
          "type" : "completion",
            "preserve_position_increments":false,
            "analyzer":"autocomplete"

          },
          "datetime": {
            "type": "date"
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "search_count": {
            "type": "long"
          }
        }
      }
}

自动完成分析器定义为-

"settings": {
    "analysis": {
        "filter": {
            "autocomplete_filter": { 
                "type":     "edge_ngram",
                "min_gram": 2,
                "max_gram": 10
            }
        },
        "analyzer": {
            "autocomplete": {
                "type":      "custom",
                "tokenizer": "standard",

                "filter": [
                    "lowercase",
                    "autocomplete_filter" 
                ]
            }
        }
    }
}

使用脚本索引数据以建议字段-

     POST /search_queries_sug/realtime/_update_by_query
{
  "query": { 
    "match_all":{}
  },
  "script": "ctx._source.suggest = ctx._source.name"
} 

这是示例结果文档的样子-

"suggest": {
"text-suggest": [
  {
    "text": "am",
    "offset": 0,
    "length": 2,
    "options": [
      {
        "text": "Ambuja mall raipur",
        "_index": "search_queries_sug",
        "_type": "realtime",
        "_id": "f7ff11f72abf2a106a31e09d34596cff",
        "_score": 1,
        "_source": {
          "suggest": "Ambuja mall raipur"
        }
      },
      {
        "text": "Amit kumar singer",
        "_index": "search_queries_sug",
        "_type": "realtime",
        "_id": "9a8704c123cbff8bc772227fa3c6d662",
        "_score": 1,
        "_source": {
          "suggest": "Amit kumar singer"
        }
      },
      {
        "text": "Amitbhadana",
        "_index": "search_queries_sug",
        "_type": "realtime",
        "_id": "b968556dc3b77b5cf2f4ef8f85fc8f68",
        "_score": 1,
        "_source": {
          "suggest": "Amitbhadana"
        }
      }..........

0 个答案:

没有答案