Reputation: 126
I want to get the products in the order of user input. Suppose if user search "Z1234","S1234","T2344", then I want products in that particular order. Where Z1234 will be the first record. How this can be achieved in the elastic search. I have tried using "script" and other ways but it does not work. Below are the example of my script usage.
'_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"
]
and my working query body is below
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
)
)
)
Is this can be achieved in the elastic search? Do I need to sort after getting the results?
Upvotes: 0
Views: 427
Reputation: 935
ElasticSearch will sort the results based on the scoring of the specific field in comparison to your input.
For example , if you search for "Z1234 S1234 T2344" it will try to find documents as close as possible to the individual inputs and the analyzer and then it will rank the results accordingly based on the proximity score.
For example , consider the following index :
POST test/doc/
{
"product_name" : "Z1234"
}
POST test/doc/
{
"product_name" : "Z1235"
}
POST test/doc
{
"product_name" : "S1234"
}
POST test/doc
{
"product_name" : "T2344"
}
If you search for "Z1234 S1234 T2344" , it will sort the results based on the score(results closer to your input) and then it will sort the remaining bellow (values such as "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"
}
}
]
}
}
Now , if you do not want to sort on the scores , but on the field name , the field of the sorting should contain doc values (default by default of Text fields)
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"
]
}
]
}
}
Upvotes: 1