Elasticsearch使用嵌套查询提升分数

时间:2014-10-08 18:08:18

标签: elasticsearch

我在Elasticsearch 1.3.4版中有以下查询:

{
   "filtered": {
      "query": {
         "bool": {
            "should": [
               {
                  "bool": {
                     "should": [
                        {
                           "match_phrase": {
                              "_all": "java"
                           }
                        },
                        {
                           "bool": {
                              "should": [
                                 {
                                    "match_phrase": {
                                       "_all": "adobe creative suite"
                                    }
                                 }
                              ]
                           }
                        }
                     ]
                  }
               },
               {
                  "bool": {
                     "should": [
                        {
                           "nested": {
                              "path": "skills",
                              "query": {
                                 "bool": {
                                    "must": [
                                       {
                                          "term": {
                                             "skills.name.original": "java"
                                          }
                                       },
                                       {
                                          "bool": {
                                             "should": [
                                                {
                                                   "match": {
                                                      "skills.source": {
                                                         "query": "linkedin",
                                                         "boost": 5
                                                      }
                                                   }
                                                }, 
                                                {
                                                   "match": {
                                                      "skills.source": {
                                                         "query": "meetup",
                                                         "boost": 5
                                                      }
                                                   }
                                                }                                                
                                             ]
                                          }
                                       }
                                    ],
                                    "minimum_should_match": "100%"
                                 }
                              }
                           }
                        }
                     ]
                  }
               }
            ],
            "minimum_should_match": "100%"
         }
      },
      "filter": {
         "and": [
            {
               "bool": {
                  "should": [
                     {
                        "term": {
                           "skills.name.original": "java"
                        }
                     }
                  ]
               }
            },
            {
               "bool": {
                  "should": [
                     {
                        "term": {
                           "skills.name.original": "ajax"
                        }
                     },
                     {
                        "term": {
                           "skills.name.original": "html"
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

映射看起来像这样:

  skills: {
    type: "nested", 
    include_in_parent: true, 
    properties: {                 
      name: {
        type: "multi_field",
        fields: {
          name: {type: "string"},
          original: {type : "string", analyzer : "string_lowercase"} 
        }              
      }                                                       
    }
  }

最后,技能(排除其他部分)的文档结构如下所示:

  "skills": 
  [
    {
      "name": "java",
      "source": [
         "linkedin", 
         "facebook"
      ]
    },
    {
      "name": "html",
      "source": [
         "meetup"
      ]
    }
  ]

我对此查询的目标是首先使用过滤器(查询的底部)过滤掉一些不相关的匹配,然后通过在整个文档中搜索match_phrase" java"来获得一个人,额外提升如果它还包含match_phrase" adobe creative suit",那么请检查我们在"技能"中获得命中的嵌套值。看看有什么样的"来源"技能来自。然后根据嵌套对象的源或源提供查询。

这类作品,至少我没有得到任何错误,但最终得分很奇怪,很难看出它是否有效。如果我给出一个小的提升,让我们说2,得分略微下降,我的最高点此刻得分为32.176407,提升= 1.增加5,它下降到31.637703。我希望它会上升,而不是下降?随着1000的提升,得分降至2.433376。

这是正确的方法,还是有更好/更简单的方法?我可以改变结构和映射等。为什么我的分数会下降?

编辑:我已经简化了一下查询,只处理了一个"技巧":

{
   "filtered": {
      "query": {
         "bool": {
            "must": [
               {
                  "bool": {
                     "must": [
                        {
                           "bool": {
                              "should": [
                                 {
                                    "match_phrase": {
                                       "_all": "java"
                                    }
                                 }
                              ],
                              "minimum_should_match": 1
                           }
                        }
                     ]
                  }
               }
            ],
            "should": [
               {
                  "nested": {
                     "path": "skills",
                     "score_mode": "avg",
                     "query": {
                        "bool": {
                           "must": [
                              {
                                 "term": {
                                    "skills.name.original": "java"
                                 }
                              }
                           ],
                           "should": [
                              {
                                 "match": {
                                    "skills.source": {
                                       "query": "linkedin",
                                       "boost": 1.2
                                    }
                                 }
                              },
                              {
                                 "match": {
                                    "skills.source": {
                                       "query": "meetup",
                                       "boost": 1.2
                                    }
                                 }
                              }
                           ]
                        }
                     }
                  }
               }
            ]
         }
      },
      "filter": {
         "and": [
            {
               "bool": {
                  "should": [
                     {
                        "term": {
                           "skills.name.original": "java"
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

现在的问题是,我期待两个类似的文件,唯一的区别是"来源"技能的价值" java"。他们是" linkedin"和"聚会"分别。在我的新查询中,它们都获得相同的提升,但最终的_score对于这两个文档来说是非常不同的。

来自doc 1的查询说明:

"value": 3.82485,
"description": "Score based on child doc range from 0 to 125"

和doc doc:

"value": 2.1993546,
"description": "Score based on child doc range from 0 to 125"

这些值是唯一不同的值,我不明白为什么。

1 个答案:

答案 0 :(得分:0)

我无法回答有关提升的问题,但您对索引有多少谢谢? 每个分片计算TF和IDF而不是每个索引,这可能会产生分数差异。 https://groups.google.com/forum/#!topic/elasticsearch/FK-PYb43zcQ

如果仅使用1个分片重新索引,确实会改变结果吗?

编辑:此外,文档范围是分片中每个文档的文档范围,您可以使用它来计算每个文档的IDF以验证分数。