Elasticsearch根据字符串在数组中出现的次数进行排序 [英] Elasticsearch sort based on the number of occurrences a string appears in an array
问题描述
我有一个包含一个字符串列表的数组字段:即:["NY","CA"]
I have an array field containig a list of strings: ie.: ["NY", "CA"]
在搜索时,我有一个匹配数组中任何字符串的过滤器.
At search time I have a filter which matches any of the strings in the array.
我想根据搜索字符串出现次数最多的文档"NY"对结果进行排序
I would like to sort the results based on documents that have the most number of appearances of the searched string: "NY"
结果应包括:文件1:["CA","NY","NY"]文件2:["NY",FL]文件3:["NY",CA," NY," NY]
Results should include: document 1: ["CA", "NY", "NY"] document 2: ["NY", FL"] document 3: ["NY", CA", "NY", "NY"]
结果应这样排序
用户3,用户1,用户2
User 3, User 1, User 2
这可能吗?如果可以,怎么办?
Is this possible? If so, how?
推荐答案
对于那些好奇的人,我无法根据该单词在数组中出现的次数来进行增强.但是,我确实做到了以下几点:
For those curious, I was not able to boost based on how many occurrences of the word happen in the array. I did however accomplished what I needed with the following:
curl -X POST "http://localhost:9200/index/document/1" -d '{"id":1,"states_ties":["CA"],"state_abbreviation":"CA","worked_in_states":["CA"],"training_in_states":["CA"]}'
curl -X POST "http://localhost:9200/index/document/2" -d '{"id":2,"states_ties":["CA","NY"],"state_abbreviation":"FL","worked_in_states":["NY","CA"],"training_in_states":["NY","CA"]}'
curl -X POST "http://localhost:9200/index/document/3" -d '{"id":3,"states_ties":["CA","NY","FL"],"state_abbreviation":"NY","worked_in_states":["NY","CA"],"training_in_states":["NY","FL"]}'
curl -X GET 'http://localhost:9200/index/_search?per_page=10&pretty' -d '{
"query": {
"custom_filters_score": {
"query": {
"terms": {
"states_ties": [
"CA"
]
}
},
"filters": [
{
"filter": {
"term": {
"state_abbreviation": "CA"
}
},
"boost": 1.03
},
{
"filter": {
"terms": {
"worked_in_states": [
"CA"
]
}
},
"boost": 1.02
},
{
"filter": {
"terms": {
"training_in_states": [
"CA"
]
}
},
"boost": 1.01
}
],
"score_mode": "multiply"
}
},
"sort": [
{
"_score": "desc"
}
]
}'
results: id: score
1: 0.75584483
2: 0.73383
3: 0.7265643
这篇关于Elasticsearch根据字符串在数组中出现的次数进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!