elasticsearch java api A and (B or C) 查询
时间: 2024-02-28 14:52:40 浏览: 108
To perform an Elasticsearch Java API query that matches A and (B or C), you can use nested bool queries as follows:
```
SearchResponse response = client.prepareSearch("my_index")
.setQuery(QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("fieldA", "A"))
.must(QueryBuilders.boolQuery()
.should(QueryBuilders.termQuery("fieldB", "B"))
.should(QueryBuilders.termQuery("fieldC", "C"))))
.execute()
.actionGet();
```
This query will match documents that have "A" in "fieldA" and either "B" in "fieldB" or "C" in "fieldC".
The outer bool query uses the `must` clause to match documents that have "A" in "fieldA". The inner bool query uses the `should` clause to match documents that have either "B" in "fieldB" or "C" in "fieldC". By nesting the inner bool query inside the outer bool query, we ensure that both conditions are satisfied.
阅读全文