我想按用户输入的顺序获取产品。假设用户搜索 “ Z1234”,“ S1234”,“ T2344”,那么我想要按该特定顺序排列的产品。其中Z1234将是第一条记录。 如何在弹性搜索中实现。我尝试使用“脚本”和其他方式,但是它不起作用。下面是我的脚本用法示例。
'_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
)
)
)
这可以在弹性搜索中实现吗?得到结果后我需要排序吗?
答案 0 :(得分:1)
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"
]
}
]
}
}