gpt4 book ai didi

php - 根据用户输入的值进行 Elasticsearch 排序

转载 作者:行者123 更新时间:2023-12-03 01:36:37 32 4
gpt4 key购买 nike

我想按用户输入的顺序获取产品。假设用户搜索
“Z1234”,“S1234”,“T2344”,那么我想要按特定顺序排列的产品。其中Z1234将是第一条记录。
如何在 Elasticsearch 中实现这一目标。我尝试使用“脚本”和其他方式,但是它不起作用。以下是我的脚本用法示例。

'_script' => [
'script' => "for(i:scoring) { if(doc[\"custom_44\"].value == i.id) return i.score; } return 0;",
"type" => "number",
'params' => [
'scoring' => [
['id'=>'3MS01',"score" => 1],
['id'=>'29xcs',"score" => 2],
]
],
"order" => "asc"
]

我的工作查询正文如下
Array
(
[size] => 20
[from] => 0
[query] => Array
(
[filtered] => Array
(
[filter] => Array
(
[bool] => Array
(
[must] => Array
(
[0] => Array
(
[bool] => Array
(
[should] => Array
(
[0] => Array
(
[query] => Array
(
[span_near] => Array
(
[clauses] => Array
(
[0] => Array
(
[span_multi] => Array
(
[match] => Array
(
[regexp] => Array
(
[custom_44] => .*3MS01.*
)

)

)

)

)

[slop] => 0
[in_order] => 1
)

)

)

[1] => Array
(
[query] => Array
(
[span_near] => Array
(
[clauses] => Array
(
[0] => Array
(
[span_multi] => Array
(
[match] => Array
(
[regexp] => Array
(
[custom_44] => .*29xcs.*
)

)

)

)

)

[slop] => 0
[in_order] => 1
)

)

)

)

)

)

[1] => Array
(
[term] => Array
(
[deleted] => 0
)

)

[2] => Array
(
[terms] => Array
(
[publication_id] => Array
(
[0] => 35627
)

)

)

[3] => Array
(
[term] => Array
(
[custom_61] => 1
)

)

)

)

)

)

)

[sort] => Array
(
[0] => Array
(
[custom_44] => asc
)

)

)

可以在 Elasticsearch 中实现吗?得到结果后我需要排序吗?

最佳答案

ElasticSearch将根据特定字段的得分(与您的输入进行比较)对结果进行排序。

例如,如果您搜索“Z1234 S1234 T2344”,它将尝试查找尽可能靠近各个输入和分析器的文档,然后它将根据接近度得分对结果进行相应排名。

例如,考虑以下索引:

POST test/doc/
{
"product_name" : "Z1234"
}

POST test/doc/
{
"product_name" : "Z1235"
}


POST test/doc
{
"product_name" : "S1234"
}

POST test/doc
{
"product_name" : "T2344"
}

如果搜索“Z1234 S1234 T2344”,它将基于得分对结果进行排序(结果更接近您的输入),然后将对其余的波纹管进行排序(例如“Z1235”之类的值)
GET test/doc/_search
{
"query": {
"match" : {
"product_name" : {
"query" : "Z1234 S1234 T2344",
"operator" : "OR",
"fuzziness": 1
}
}
}
}





"hits": {
"total": 5,
"max_score": 1.2476649,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "AWViqiCOOSSVvlNCAXVu",
"_score": 1.2476649,
"_source": {
"product_name": "Z1234"
}
},
{
"_index": "test",
"_type": "doc",
"_id": "AWViqlZ2OSSVvlNCAXV7",
"_score": 0.6931472,
"_source": {
"product_name": "T2344"
}
},
{
"_index": "test",
"_type": "doc",
"_id": "AWViqj9UOSSVvlNCAXV2",
"_score": 0.51782775,
"_source": {
"product_name": "S1234"
}
},
{
"_index": "test",
"_type": "doc",
"_id": "AWVir1UeOSSVvlNCAXpB",
"_score": 0.23014566,
"_source": {
"product_name": "Z1235"
}
},
{
"_index": "test",
"_type": "doc",
"_id": "AWVirhwlOSSVvlNCAXkQ",
"_score": 0.23014566,
"_source": {
"product_name": "Z1235"
}
}
]
}
}

现在,如果您不想对分数进行排序,而是对字段名称进行排序,则排序的字段应包含doc值(默认为“文本”字段)
GET test/doc/_search
{
"query": {
"match": {
"product_name": {
"query": "Z1234 S1234 T2344",
"operator": "OR",
"fuzziness": 1
}
}
},
"sort": [
{
"product_name.keyword": {
"order": "desc"
}
}
]
}



{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "AWVitbfmOSSVvlNCAYA8",
"_score": null,
"_source": {
"product_name": "Z1235"
},
"sort": [
"Z1235"
]
},
{
"_index": "test",
"_type": "doc",
"_id": "AWVita_pOSSVvlNCAYA7",
"_score": null,
"_source": {
"product_name": "Z1234"
},
"sort": [
"Z1234"
]
},
{
"_index": "test",
"_type": "doc",
"_id": "AWVitcPKOSSVvlNCAYBA",
"_score": null,
"_source": {
"product_name": "T2344"
},
"sort": [
"T2344"
]
},
{
"_index": "test",
"_type": "doc",
"_id": "AWVitb1GOSSVvlNCAYA_",
"_score": null,
"_source": {
"product_name": "S1234"
},
"sort": [
"S1234"
]
}
]
}
}

关于php - 根据用户输入的值进行 Elasticsearch 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51969494/

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