Elasticsearch查询条款组合顺序

时间:2015-09-18 19:31:29

标签: search elasticsearch combinations combinatorics

在elasticsearch中,我搜索一些标签并从最匹配到最不匹配的排序。没关系。

然而,我的问题是关于相等匹配情况的顺序。

例如:

  1. 我存储了以下标签:" tag1"," tag2"," tag3"," tag4"," tag5&# 34;
  2. 我存储了这些标签:" tag6"," tag1"
  3. 我存储了这些标签:" tag4"," tag3"," tag1"
  4. 我存储了这些标签:" tag2"," tag1"," tag5"," tag7"
  5. 我的搜索查询:

    {
        "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 
    }
    

    它返回:

    1. " tag6"," tag1"
    2. " tag1"," tag2"," tag3"," tag4"," tag5"
    3. " tag4"," tag3"," tag1"
    4. 我的搜索顺序是tag4,然后是tag6。如何返回tag4首先包含行和?之后的tag6

1 个答案:

答案 0 :(得分:0)

我从Boosting Filtered Subsets

找到了解决问题的方法

我使用" function_score"对于我的过滤查询,并添加了"函数"和" score_mode"。除此之外,我指定了#34; weight"对于"函数"中的每个标记。

查询首先返回tag4s,然后tag6包含行:

  1. " tag1"," tag2"," tag3"," tag4"," tag5"
  2. " tag4"," tag3"," tag1"
  3. " tag6"," tag1"
  4. 我的新查询:

    {
        "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 
    }