简体   繁体   English

Elasticsearch查询词组合顺序

[英]Elasticsearch Query Terms Combination Order

In elasticsearch, I search some tags and sort them from the most matching to the least matching. 在elasticsearch中,我搜索一些标签并将它们从最匹配到最不匹配进行排序。 It's ok. 没关系。

However, my problem is about order of equal matching situations. 但是,我的问题是关于相等匹配情况的顺序。

For example: 例如:

  1. I stored these tags: "tag1", "tag2", "tag3", "tag4", "tag5" 我存储了以下标签:“ tag1”,“ tag2”,“ tag3”,“ tag4”,“ tag5”
  2. I stored these tags: "tag6", "tag1" 我存储了以下标签:“ tag6”,“ tag1”
  3. I stored these tags: "tag4", "tag3", "tag1" 我存储了以下标签:“ tag4”,“ tag3”,“ tag1”
  4. I stored these tags: "tag2", "tag1", "tag5", "tag7" 我存储了以下标签:“ tag2”,“ tag1”,“ tag5”,“ tag7”

My search query: 我的搜索查询:

{
    "query" : { 
        "bool" : { 
            "must" : [ 
                { 
                    "terms" : { 
                        "my_field" : ["tag4", "tag6"],
                        minimum_should_match : 1
                    } 
                }, 
                {"term" : {"my_cityId" : 1}}, 
                {"term" : {"my_townId" : 8}} 
            ] 
        } 
    }, 
    "sort" : [ 
        {"_score" : "desc"}, 
        {"my_topTime" : "asc"} 
    ], 
    "from" : 0, 
    "size" : 5 
}

It returns: 它返回:

  1. "tag6", "tag1" “ tag6”,“ tag1”
  2. "tag1", "tag2", "tag3", "tag4", "tag5" “标签1”,“标签2”,“标签3”,“标签4”,“标签5”
  3. "tag4", "tag3", "tag1" “ tag4”,“ tag3”,“ tag1”

My search order is tag4 and then tag6. 我的搜索顺序是tag4,然后是tag6。 How can it be returned tag4 contained rows first and tag6 after? 如何返回tag4首先包含行,tag6之后包含行?

I found a solution to my problem from Boosting Filtered Subsets 我从增强过滤子集中找到了解决问题的方法

I used "function_score" for my filter query, and added "functions" and "score_mode". 我在过滤器查询中使用了“ function_score”,并添加了“ functions”和“ score_mode”。 Beside this, I specified "weight" for each tag in "functions". 除此之外,我为“功能”中的每个标签指定了“重量”。

The query returned tag4s first and then tag6 contained rows: 该查询首先返回tag4s,然后tag6包含以下行:

  1. "tag1", "tag2", "tag3", "tag4", "tag5" “标签1”,“标签2”,“标签3”,“标签4”,“标签5”
  2. "tag4", "tag3", "tag1" “ tag4”,“ tag3”,“ tag1”
  3. "tag6", "tag1" “ tag6”,“ tag1”

My new query: 我的新查询:

{
    "query" : { 
        "function_score" : { 
            "filter" : { 
                "query" : { 
                    "bool" : { 
                        "must" : [
                            { 
                                "terms" : { 
                                    "my_field" : ["tag4", "tag6"],                                          
                                    minimum_should_match : 1
                                } 
                            } , 
                            {"term" : {"my_cityId" : 1}}, 
                            {"term" : {"my_townId" : 8}} 
                        ] 
                    } 
                } 
            }, 
            "functions" : [ 
                {"filter" : {"term" : { "my_field" : "tag4" }}, "weight" : 2},
                {"filter" : { "term" : { "my_field" : "tag6" }}, "weight" : 1} 
            ], 
            "score_mode": "sum" 
        } 
    }, 
    "sort" : [ 
        {"_score" : "desc"}, 
        {"my_topTime" : "asc"} 
    ], 
    "from" : 0, 
    "size" : 5 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM