英文:
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": {
"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"
}
]
}
}
]
}
}
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 + ".placeId", placeIds)); //array of places
placeQueryWithType.must(termQuery(PLACES + ".type", 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.
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "places",
"query": {
"bool": {
"must_not": [
{
"terms": {
"places.placeId": [
3
]
}
},
{
"term": {
"places.type": {
"value": "MAIN"
}
}
}
]
}
}
}
}
]
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论