java代码实现ElasticSearch中多值查询
时间: 2024-01-10 17:04:24 浏览: 40
下面是一个Java代码示例,用于执行ElasticSearch中的多值查询:
```java
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.SearchHit;
import java.util.Arrays;
import java.util.List;
public class MultiValueQueryExample {
public static void main(String[] args) throws Exception {
// 创建ElasticSearch客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 定义查询条件
List<String> values = Arrays.asList("value1", "value2", "value3");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
for (String value : values) {
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("field_name", value);
boolQuery.should(matchQuery);
}
// 构建搜索请求
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(boolQuery);
SearchRequest searchRequest = new SearchRequest("index_name");
searchRequest.source(sourceBuilder);
// 执行搜索请求
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
// 处理搜索结果
SearchHit[] searchHits = searchResponse.getHits().getHits();
for (SearchHit hit : searchHits) {
// 处理每个搜索结果
System.out.println(hit.getSourceAsString());
}
// 关闭ElasticSearch客户端
client.close();
}
}
```
在这个示例中,我们首先创建了一个ElasticSearch客户端,然后定义了一个包含多个值的查询条件。我们使用布尔查询(BoolQueryBuilder)来组合这些查询条件,使用should()方法将每个查询条件添加到布尔查询中。
接下来,我们使用SearchSourceBuilder构建搜索请求,并将布尔查询添加到搜索请求中。我们还指定了要搜索的索引名称("index_name")。
最后,我们通过调用RestHighLevelClient.search()方法来执行搜索请求,并处理搜索结果。我们使用SearchHit对象遍历搜索结果,并打印每个结果的源字符串(getSourceAsString()方法)。
请注意,我们使用了try-with-resources语句来自动关闭ElasticSearch客户端。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)