Java Elasticsearch – 使用AND查询嵌套元素字段

huangapple go评论66阅读模式
英文:

Java Elasticsearch - Querying nested element fields with And

问题

Here is the translation of the code and text you provided:

我在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"
            }
          ]
        }
      }
    ]
  }
}

从这个数据集中,对于给定的地点和类型,我需要所有的酒店。

例如,如果我查询地点ID为3且类型为MAIN,我应该得到ID为7148和7147的酒店。

如果我查询地点ID为1和3且类型为MAIN,那么应该返回所有3个报价。

我尝试了以下方法:

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

placeQueryWithType.should(termsQuery(PLACES + ".placeId", placeIds)); //地点数组

placeQueryWithType.must(termQuery(PLACES + ".type", placeType.name())); //要么是MAIN要么是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
    }
  }
}

谢谢你!


<details>
<summary>英文:</summary>

I have the following structure in ES (Also dataset for this example)

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

      &quot;places&quot;: [
        {
          &quot;placeId&quot;: 3,
          &quot;type&quot;: &quot;MAIN&quot;,
          &quot;name&quot;: &quot;pruggern&quot;
        }
      ]
    }
  },
  {
    &quot;_index&quot;: &quot;hotels&quot;,
    &quot;_type&quot;: &quot;_doc&quot;,
    &quot;_id&quot;: &quot;7147&quot;,
    &quot;_score&quot;: 1.0,
    &quot;_source&quot;: {
     
      &quot;places&quot;: [
        {
          &quot;placeId&quot;: 3,
          &quot;type&quot;: &quot;MAIN&quot;,
          &quot;name&quot;: &quot;pruggern&quot;
        }
      ]
    }
  },
  {
    &quot;_index&quot;: &quot;hotels&quot;,
    &quot;_type&quot;: &quot;_doc&quot;,
    &quot;_id&quot;: &quot;7146&quot;,
    &quot;_score&quot;: 1.0,
    &quot;_source&quot;: {
      &quot;places&quot;: [
        {
          &quot;placeId&quot;: 3,
          &quot;type&quot;: &quot;AROUND&quot;,
          &quot;name&quot;: &quot;pruggern&quot;
        },
        {
          &quot;placeId&quot;: 1,
          &quot;type&quot;: &quot;MAIN&quot;,
          &quot;name&quot;: &quot;schladming&quot;
        }
      ]
    }
  }
]

}
}


Out of this dataset for a given place and type I need all the hotels

For example if I query for placeId 3 and type MAIN, I should get hotels with id 7148 and 7147

I query for placeId 1 and 3 and type MAIN then all the 3 offers should be returned

What  I tried so far

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

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

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

This did not work as it was giving me all the offers. I debugged the code to see the query and it looked something like this

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

Could be that the query just satisfies one of the condition and it fails

I also tried to generate a query something like this where must clause has a should and must but still dont work

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


Thank you

答案1

得分: 1

这个查询已经按照问题的要求工作正常。

英文:

Did this query and it works as per the requirement of the question.

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

huangapple
  • 本文由 发表于 2023年4月13日 18:27:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定