gpt4 book ai didi

Elasticsearch 分层排序

转载 作者:行者123 更新时间:2023-11-29 02:55:42 27 4
gpt4 key购买 nike

我希望能够按特定顺序返回预先输入的项目。例如,搜索 Para 应该返回:

 Paracetamol

Parafin

LIQUID PARAFFIN

ISOMETHEPTENE WITH PARACETAMOL

1) 以搜索词para开头的建议应该按字母顺序排在最前面

2) 其余项目应出现在下方并按字母顺序排列

这可以用 Elasticsearch 实现吗?

更新

如果我希望输出是这样的怎么办:

 Paracetamol

Parafin

Amber Paraffin

ISOMETHEPTENE WITH PARACETAMOL

LIQUID PARAFFIN

所以所有包含前缀的术语都在顶部,其他所有内容都按字母顺序排列。

最佳答案

这是我的建议(另外,您需要启用脚本):

PUT /test
{
"settings": {
"analysis": {
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"standard",
"lowercase",
"ngram"
]
},
"search_ngram": {
"type": "custom",
"tokenizer": "keyword",
"filter": "lowercase"
}
},
"filter": {
"ngram": {
"type": "ngram",
"min_gram": 2,
"max_gram": 15
}
}
}
},
"mappings": {
"test": {
"properties": {
"text": {
"type": "string",
"index_analyzer": "autocomplete",
"search_analyzer": "search_ngram",
"index_options": "positions",
"fields": {
"not_analyzed_sorting": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}

POST test/test/_bulk
{"index":{"_id":1}}
{"text":"Paracetamol"}
{"index":{"_id":2}}
{"text":"Paracetamol xxx yyy zzz"}
{"index":{"_id":3}}
{"text":"Parafin"}
{"index":{"_id":4}}
{"text":"LIQUID PARAFFIN"}
{"index":{"_id":5}}
{"text":"ISOMETHEPTENE WITH PARACETAMOL"}

GET /test/test/_search
{
"query": {
"match": {
"text": "Para"
}
},
"sort": [
{
"_script": {
"type": "number",
"script": "termInfo=_index[field_to_search].get(term_to_search.toLowerCase(),_POSITIONS);if (termInfo) {for(pos in termInfo){return pos.position}};return 0;",
"params": {
"term_to_search": "Para",
"field_to_search": "text"
},
"order": "asc"
}
},
{
"text.not_analyzed_sorting": {
"order": "asc"
}
}
]
}

更新

对于您更新的问题,即使我更愿意有另一篇文章,请使用以下查询:

{
"query": {
"match": {
"text": "Para"
}
},
"sort": [
{
"_script": {
"type": "number",
"script": "termInfo=_index[field_to_search].get(term_to_search.toLowerCase(),_POSITIONS);if (termInfo) {for(pos in termInfo){if (pos.position==0) return pos.position; else return java.lang.Integer.MAX_VALUE}};return java.lang.Integer.MAX_VALUE;",
"params": {
"term_to_search": "Para",
"field_to_search": "text"
},
"order": "asc"
}
},
{
"text.not_analyzed_sorting": {
"order": "asc"
}
}
]
}

关于Elasticsearch 分层排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33435670/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com