在某些情况下,我需要根据给定的文本查找建议。 我的基本需求是获取建议,以便首先需要完全匹配的对象才是模糊的。我正在使用下面描述的脚本索引我的name字段作为建议的输入。我已附上我选择的解决方案。
例如搜索 amazon仅返回以amazon开头的文本,而不返回“ theamazon”或“ welcome to amazon”。 我该怎么做才能得到这样的结果?另外,诸如theamazon或cometoamazon之类的文本呢?我正在使用两个不同的完成建议器来获得准确和模糊的结果。任何帮助深表感谢。我所有的代码如下。请随时询问有关代码的任何不清楚之处。
PUT search_queries_sug/
{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 30
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
}
}
映射看起来像-
PUT search_queries_sug/_mapping/realtime
{
"realtime": {
"properties": {
"suggest_exact" : {
"type" : "completion",
"analyzer":"autocomplete",
"preserve_position_increments":false,
"preserve_separators" : false,
"search_analyzer":"standard"
},
"suggest_fuzzy" : {
"type" : "completion",
"analyzer":"autocomplete",
"preserve_position_increments":false,
"preserve_separators" : false,
"search_analyzer":"standard"
},
"datetime": {
"type": "date"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"search_count": {
"type": "long"
}
}
}
}
这就是我对建议编制索引的方式-
POST /search_queries_sug/realtime/_update_by_query?
{
"query": {
"match_all":{}
},
"script": "ctx._source.suggest_exact = ctx._source.name;ctx._source.suggest_fuzzy = ctx._source.name "
}
这是查询之前的文档外观-
{
"_index": "search_queries_sug",
"_type": "realtime",
"_id": "40d3b294ed2ef49f12185d698b2f9e3d",
"_score": 1,
"_source": {
"datetime": "2018-07-19T15:09:55Z",
"name": "Sing along with Goutam",
"suggest_fuzzy": "Sing along with Goutam",
"search_count": 1,
"suggest_exact": "Sing along with Goutam"
}
}
查询后,结果为-
"hits": {
"total": 0,
"max_score": 0,
"hits": []
},
"suggest": {
"text-suggest-exact": [
{
"text": "amazon",
"offset": 0,
"length": 6,
"options": [
{
"text": "Amazon",
"_index": "search_queries_sug",
"_type": "realtime",
"_id": "b3b3a6ac74ecbd56bcdbefa4799fb9df",
"_score": 6,
"_source": {
"datetime": "2018-07-24T20:58:40Z",
"name": "Amazon",
"suggest_fuzzy": "Amazon",
"search_count": 1,
"suggest_exact": "Amazon"
}
],
"text-suggest-fuzzy": [
{
"text": "amazon",
"offset": 0,
"length": 6,
"options": [
{
"text": "Amazon",
"_index": "search_queries_sug",
"_type": "realtime",
"_id": "b3b3a6ac74ecbd56bcdbefa4799fb9df",
"_score": 5,
"_source": {
"datetime": "2018-07-24T20:58:40Z",
"name": "Amazon",
"suggest_fuzzy": "Amazon",
"search_count": 1,
"suggest_exact": "Amazon"
}
},
{
"text": "Amazing dance",
"_index": "search_queries_sug",
"_type": "realtime",
"_id": "6a32e5fb887e6ab362ab00c74ea62e07",
"_score": 4,
"_source": {
"datetime": "2018-07-27T09:48:41Z",
"name": "Amazing dance",
"suggest_fuzzy": "Amazing dance",
"search_count": 1,
"suggest_exact": "Amazing dance"
}
}