在弹性搜索中过滤无分数查询

时间:2013-07-18 19:51:01

标签: elasticsearch

我需要修改此查询,以便弹性搜索给它一个分数。我希望我的自定义过滤器分数是唯一给出任何结果得分的东西。我该如何做到这一点?

每条记录应该只有0,100或1000的分数。

{
  "size":50, 
  "from":0, 
  "query" : {
    "custom_filters_score" : {
      "query" : {
        "filtered" : {
          "query" : {
            "bool" : {
              "must" : [
                {"term":{"type":"alpha"}}, 
                {"field":{"sector":"exists"}}, 
                {"field":{"sector.sub":"exists"}}, 
                {"field":{"alpha_sector.sub.categories":"second"}}, 
                {"field":{"beta_sector.sub.columns":"first"}}, 
                {"term":{"beta_type":"beta"}}, 
                {"term":{"area":"624"}}
              ]
            }
          }, 
          "filter" : {
            "or" : [
              {
                "and" : [
                  {"term":{"area":"624"}}, 
                  {"term":{"start":"07242013"}}
                ]
              }, 
              {
                "and" : [
                  {"term":{"area":"624"}}, 
                  {"term":{"start":"blank"}}
                ]
              }
            ]
          }
        }
      }, 
      "filters" : [
        {"filter":{"term":{"resource":5726}}, "boost":"1000"}, 
        {"filter":{"term":{"alpha_resource":5726}}, "boost":"100"}
      ], 
      "score_mode":"sum"
    }
  }
}

2 个答案:

答案 0 :(得分:3)

我不太确定你在这里想要实现的目标

            {"field":{"sector":"exists"}}, 
            {"field":{"sector.sub":"exists"}},

但一般情况下,如果您不希望部分查询影响分数,只需将其设为过滤器即可。使用“bool”和“term”过滤器而不是“and”/“或”/“not”更好“

{
  "size":50, 
  "from":0, 
  "query" : {
    "custom_filters_score" : {
      "query" : {
        "filtered" : {
          "query" : {
            "match_all": {}
          }, 
          "filter" : {
            "bool" : {
              "must" : [
                {"term":{"type":"alpha"}}, 
                {"query":{"field":{"sector":"exists"}}}, 
                {"query":{"field":{"sector.sub":"exists"}}}, 
                {"query":{"field":{"alpha_sector.sub.categories":"second"}}}, 
                {"query":{"field":{"beta_sector.sub.columns":"first"}}}, 
                {"term":{"beta_type":"beta"}}, 
                {"term":{"area":"624"}}
              ],
              "should" : [
                {
                  "bool" : {
                    "must" : [
                      {"term":{"area":"624"}}, 
                      {"term":{"start":"07242013"}}
                    ]
                  }
                }, 
                {
                  "bool" : {
                    "must": [
                      {"term":{"area":"624"}}, 
                      {"term":{"start":"blank"}}
                    ]
                  }
                }
              ]
            }
          }
        }
      }, 
      "filters" : [
        {"filter":{"term":{"resource":5726}}, "boost":"1000"}, 
        {"filter":{"term":{"alpha_resource":5726}}, "boost":"100"}
      ], 
      "score_mode":"total"
    }
  }
}

答案 1 :(得分:0)

您应该订购_doc得分字段:

 {
   "sort": [
    "_doc"
   ]    
}

在本文档的第一页中,它被描述为您的用例的最佳实践  https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html