matchAll查询:查询所有文档
package com.yy.esdemo;
import com.alibaba.fastjson.JSON;
import com.yy.esdemo.domain.Person;
import com.yy.esdemo.domain.User;
import com.yy.esdemo.mapper.UserMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
public class demo4 {
@Autowired
private RestHighLevelClient client;
/**
* 查询所有
* 1. matchALL
* 2.将查询结果封装为Person对象,装载到List中
* 3.分页。默认显示10条
*/
@Test
public void matchAllTest() throws IOException {
//2 构建查询请求对象,指定查询的索引名称
SearchRequest searchRequest = new SearchRequest("person");
//4 创建查询条件构建器 SearchSourceBuilder
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//6 查询条件
QueryBuilder query = QueryBuilders.matchAllQuery();
// 5 指定查询条件
sourceBuilder.query(query);
//3 添加查询条件构建器SearchSourceBuilder
searchRequest.source(sourceBuilder);
//1 查询,获取查询结果
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
// 7 获取命中对象 SearchHits
SearchHits searchHits = searchResponse.getHits();
//7.1 获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:"+value);
List<Person> lists = new ArrayList<>();
//7.2 获取Hits数据,这个是一个数组
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits){
//获取json字符串格式的数据
String sourceAsString = hit.getSourceAsString();
//转为java对象
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person person : lists){
System.out.println(person);
}
}
}
/**
* 查询所有
* 1. matchALL
* 2.将查询结果封装为Person对象,装载到List中
* 3.分页。默认显示10条
*/
@Test
public void matchAllTest() throws IOException {
//2 构建查询请求对象,指定查询的索引名称
SearchRequest searchRequest = new SearchRequest("person");
//4 创建查询条件构建器 SearchSourceBuilder
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//6 查询条件
QueryBuilder query = QueryBuilders.matchAllQuery();
// 5 指定查询条件
sourceBuilder.query(query);
//3 添加查询条件构建器SearchSourceBuilder
searchRequest.source(sourceBuilder);
//8 添加分页信息
sourceBuilder.from(0);
sourceBuilder.size(3);
//1 查询,获取查询结果
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
// 7 获取命中对象 SearchHits
SearchHits searchHits = searchResponse.getHits();
//7.1 获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:"+value);
List<Person> lists = new ArrayList<>();
//7.2 获取Hits数据,这个是一个数组
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits){
//获取json字符串格式的数据
String sourceAsString = hit.getSourceAsString();
//转为java对象
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person person : lists){
System.out.println(person);
}
}
term查询:不会对查询条件进行分词。
/**
* termQuery:词条查询
*/
@Test
public void testTermQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
QueryBuilder query = QueryBuilders.termQuery("address","北京");
sourceBuilder.query(query);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:"+value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits){
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
}
match查询:
Test
public void testMatchQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
QueryBuilder query = QueryBuilders.matchQuery("address","北京外滩");
sourceBuilder.query(query);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:"+value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits){
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
}
@Test
public void testMatchQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
MatchQueryBuilder query = QueryBuilders.matchQuery("address","北京外滩");
query.operator(Operator.AND);
sourceBuilder.query(query);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:"+value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits){
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
}
wildcard查询:会对查询条件进行分词。还可以使用通配符?(任意单个字符)和*(0个或多个字符)
@Test
public void testWildcardQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
WildcardQueryBuilder query = QueryBuilders.wildcardQuery("address", "w*");
sourceBuilder.query(query);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:"+value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits){
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
}
regexp查询:正则查询
@Test
public void testRegexpdQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
RegexpQueryBuilder query = QueryBuilders.regexpQuery("address", "\\w+(.)*");
sourceBuilder.query(query);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:" + value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
}
prefix查询:前缀查询
@Test
public void testPrefixQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
PrefixQueryBuilder query = QueryBuilders.prefixQuery("name", "王");
sourceBuilder.query(query);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:" + value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
}
range范围查询:查找指定字段在指定范围内包含值
@Test
public void testERangeQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
RangeQueryBuilder query = QueryBuilders.rangeQuery("age");
//指定下限
query.gte(25);
//指定上限
query.lte(45);
sourceBuilder.query(query);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:" + value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
}
@Test
public void testERangeQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
RangeQueryBuilder query = QueryBuilders.rangeQuery("age");
//指定下限
query.gte(25);
//指定上限
query.lte(45);
sourceBuilder.query(query);
//排序
sourceBuilder.sort("age", SortOrder.DESC);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:" + value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
}
queryString:
@Test
public void testQueryStringQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
QueryStringQueryBuilder query = QueryBuilders
.queryStringQuery("北京外滩")
.field("address").field("name")
.defaultOperator(Operator.AND);
sourceBuilder.query(query);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:" + value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
}
布尔查询
boolQuery:对多个查询条件连接。
连接方式:
/**
* 查询姓名以王开头,地址包含北京,年龄在45岁以下,
* @throws IOException
*/
@Test
public void testBooleanQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 1 构建boolQuery
BoolQueryBuilder query = QueryBuilders.boolQuery();
// 2 构建各个查询条件
// 2.1 地址包含北京
TermQueryBuilder termQuery = QueryBuilders.termQuery("address", "北京");
query.must(termQuery);
// 2.2 查询姓名以王开头
WildcardQueryBuilder wildcardQuery = QueryBuilders.wildcardQuery("name", "王*");
query.must(wildcardQuery);
// 3 年龄在45岁以下
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
rangeQuery.gte(18);
rangeQuery.lte(45);
query.filter(rangeQuery);
sourceBuilder.query(query);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:" + value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
}
指标聚合:相当于MySQL的聚合函数。max、min、avg、sum等
桶聚合:相当于MySQL的group by操作。不要对text类型的数据进行分组,会失败。
/**
* 查询姓名包含王的地址列表
* @throws IOException
*/
@Test
public void testAggQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
MatchQueryBuilder query = QueryBuilders.matchQuery("address", "外滩");
sourceBuilder.query(query);
//查询地址列表
/*
参数:
1:自定义的名称,将来用于获取数据
2.分组的字段
*/
AggregationBuilder agg = AggregationBuilders.terms("ages").field("age").size(10);
sourceBuilder.aggregation(agg);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:" + value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
//获取聚会结果
Aggregations aggregations = searchResponse.getAggregations();
//将结果转为map
Map<String, Aggregation> aggregationMap = aggregations.asMap();
//获取结果中上面定义的ages里面的数据,并将结果转为Term类型
Terms ages = (Terms) aggregationMap.get("ages");
//获取桶里面的数据
List<? extends Terms.Bucket> buckets = ages.getBuckets();
//将桶中的age数据获取到
List ageLists = new ArrayList();
for (Terms.Bucket bucket : buckets) {
Object key = bucket.getKey();
ageLists.add(key);
}
for (Object ageList : ageLists) {
System.out.println(ageList);
}
}
/**
*高亮查询:
* 1。设置高亮
* 高亮字段
* 前缀
* 后缀
* 2.将高亮了的字段数据,替换原有数据
*
* @throws IOException
*/
@Test
public void testHighLightQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
MatchQueryBuilder query = QueryBuilders.matchQuery("address", "北京");
sourceBuilder.query(query);
//设置高亮
HighlightBuilder highLight = new HighlightBuilder();
//设置三要素
highLight.field("address");//字段名
highLight.preTags("");
highLight.postTags("");
sourceBuilder.highlighter(highLight);
//查询地址列表
/*
参数:
1:自定义的名称,将来用于获取数据
2.分组的字段
*/
AggregationBuilder agg = AggregationBuilders.terms("ages").field("age").size(10);
sourceBuilder.aggregation(agg);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
//获取总记录数
long value = searchHits.getTotalHits().value;
System.out.println("总记录数:" + value);
List<Person> lists = new ArrayList<>();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
//转为json
Person person = JSON.parseObject(sourceAsString, Person.class);
Map<String, SearchHits> innerHits = hit.getInnerHits();
//获取高亮结果,替换address中的结果
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
HighlightField highlightField = highlightFields.get("address");
Text[] fragments = highlightField.fragments();
//替换
person.setAddress(fragments[0].toString());
lists.add(person);
}
for (Person list : lists) {
System.out.println(list);
}
//获取聚会结果
Aggregations aggregations = searchResponse.getAggregations();
//将结果转为map
Map<String, Aggregation> aggregationMap = aggregations.asMap();
//获取结果中上面定义的ages里面的数据,并将结果转为Term类型
Terms ages = (Terms) aggregationMap.get("ages");
//获取桶里面的数据
List<? extends Terms.Bucket> buckets = ages.getBuckets();
//将桶中的age数据获取到
List ageLists = new ArrayList();
for (Terms.Bucket bucket : buckets) {
Object key = bucket.getKey();
ageLists.add(key);
}
for (Object ageList : ageLists) {
System.out.println(ageList);
}
}