嵌套域ElasticSearch中基于属性的优先级搜索

o7jaxewo  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(163)

下面是some_index的Map。

{
  "some_index":
    "properties": {
        "people": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "keyword"
            },
            "job": {
              "type": "keyword"
            }
          }
        }
    }
}

我试图做的是提高分数时,搜索查询包括属性的人,使列表返回的ElasticSearch,使人与水平和工作匹配显示在顶部的名单。

"should": [
                {
                    "constant_score": {
                        "filter": {
                            "terms": {
                                "people": [
                                    {
                                        "level": "l2"
                                        "job": "programmer",

                                    },
                                    {
                                        "level": "l3",
                                        "job": "cs"
                                    }
                                ]
                            }
                        },
                        "boost": 100
                    }
                }
            ]
        }
    } ...

这个查询似乎没有任何效果。我已经尝试了一些其他的方法与匹配和其他人,但没有得到我想要的。
可能的替代解决方案?

ckocjqey

ckocjqey1#

people的DataType为“嵌套”,需要使用nested query
根据问题,使用constant_score的目的不明确。对于嵌套字段,您的查询应如下所示。

{
  "query": {
    "constant_score": {
      "filter": {
        "nested": {
          "path": "people",
          "query": {
            "bool": {
              "should": [
                {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "people.level": {
                            "value": "l2"
                          }
                        }
                      },
                      {
                        "term": {
                          "people.job": {
                            "value": "programmer"
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "people.level": {
                            "value": "l3"
                          }
                        }
                      },
                      {
                        "term": {
                          "people.job": {
                            "value": "cs"
                          }
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      },
      "boost": 1.2
    }
  }
}

相关问题