Java Elasticsearch -使用And查询嵌套元素字段

w8biq8rn  于 2023-04-20  发布在  ElasticSearch
关注(0)|答案(1)|浏览(182)

我在ES中有以下结构(也是本例的数据集)

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "hotels",
        "_type": "_doc",
        "_id": "7148",
        "_score": 1.0,
        "_source": {
         
          "places": [
            {
              "placeId": 3,
              "type": "MAIN",
              "name": "pruggern"
            }
          ]
        }
      },
      {
        "_index": "hotels",
        "_type": "_doc",
        "_id": "7147",
        "_score": 1.0,
        "_source": {
         
          "places": [
            {
              "placeId": 3,
              "type": "MAIN",
              "name": "pruggern"
            }
          ]
        }
      },
      {
        "_index": "hotels",
        "_type": "_doc",
        "_id": "7146",
        "_score": 1.0,
        "_source": {
          "places": [
            {
              "placeId": 3,
              "type": "AROUND",
              "name": "pruggern"
            },
            {
              "placeId": 1,
              "type": "MAIN",
              "name": "schladming"
            }
          ]
        }
      }
    ]
  }
}

出了这个数据集为一个给定的地方和类型我需要所有的酒店
例如,如果我查询placeId3并输入MAIN,我应该得到id7148和7147的酒店
我查询placeId1和3和类型主然后所有3提供应返回
目前为止我所尝试的

BoolQueryBuilder placeQueryWithType = boolQuery();
    placeQueryWithType.minimumShouldMatch(1);

    placeQueryWithType.should(termsQuery(PLACES + ".placeId", placeIds)); //array of places

    placeQueryWithType.must(termQuery(PLACES + ".type", placeType.name())); //Either MAIN or AROUND
    mainQuery.must(placeQueryWithType);

这并没有工作,因为它是给我所有的报价。我调试代码,看看查询,它看起来像这样

{
  "query": {
            "bool" : {
              "must" : [
                {
                  "bool" : {
                    "must" : [
                      {
                        "term" : {
                          "places.type" : {
                            "value" : "MEETING_POINT",
                            "boost" : 1.0
                          }
                        }
                      }
                    ],
                    "should" : [
                      {
                        "terms" : {
                          "places.placeId" : [
                            3
                          ],
                          "boost" : 1.0
                        }
                      }
                    ],
                    "adjust_pure_negative" : true,
                    "minimum_should_match" : "1",
                    "boost" : 1.0
                  }
                }
              ],
              "adjust_pure_negative" : true,
              "boost" : 1.0
      }
    }
}

可能是查询只满足其中一个条件而失败
我还尝试生成一个类似这样的查询,其中must子句有一个should和must,但仍然不起作用

{
  "query":{
  "bool" : {
    "must" : [
      {
        "bool" : {
          "should" : [
            {
              "bool" : {
                "must" : [
                  {
                    "terms" : {
                      "places.placeId" : [
                        3
                      ],
                      "boost" : 1.0
                    }
                  },
                  {
                    "term" : {
                      "places.type" : {
                        "value" : "MEETING_POINT",
                        "boost" : 1.0
                      }
                    }
                  }
                ],
                "adjust_pure_negative" : true,
                "boost" : 1.0
              }
            }
          ],
          "adjust_pure_negative" : true,
          "minimum_should_match" : "1",
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
}

谢谢你

x0fgdtte

x0fgdtte1#

做了这个查询,它的工作按照要求的问题。

{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "places",
            "query": {
              "bool": {
                "must_not": [
                  {
                    "terms": {
                      "places.placeId": [
                        3
                      ]
                    }
                  },
                  {
                    "term": {
                      "places.type": {
                        "value": "MAIN"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

相关问题