文章目录
1步骤
1.创建一个client对象
2.使用client的search方法实现查询操作
3.需要SearchRequest参数
4.request对象中需要有索引的名称
5.request对象中需要包含请求体
6.请求体中包含查询条件,分页条件、过滤条件、高亮条件等
7.执行查询
8.从查询结果中取数据
2返回结果
total().value属性:命中的所有记录数
默认是有分页的,默认返回前10条数据
文档是包含在searchHits数组中
3查询条件
封装到SearchRequest对象中,其中查询条件需要封装到SearchSourceBuilder中
其中包含:
查询条件:QueryBuilders工具类创建
分页信息:from、size
过滤条件:filter实现过滤,过滤条件可以有多个
java
package com.xd.cubemall.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
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.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.stream.Stream;
public class IndexSearchTest {
private RestHighLevelClient client;
@BeforeEach
public void init() {
//创建一个client对象
client = new RestHighLevelClient(RestClient.builder(
new HttpHost("1.1.1.1",9200),
new HttpHost("2.2.2.2",9200),
new HttpHost("3.3.3.3",9200)
));
}
@Test
public void searchIndex1() throws Exception {
//http://1.1.1.1:9200/hello1/_search
//{
//"query":{"match_all":{}}
//}
SearchRequest request = new SearchRequest("hello1")
.source(new SearchSourceBuilder()
.query(QueryBuilders.matchAllQuery())
);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//从response对象中取结果
SearchHits hits = response.getHits();
long total = hits.getTotalHits().value;
System.out.println("总记录数:" + total);
SearchHit[] searchHits = hits.getHits();
Stream.of(searchHits).forEach(e-> System.out.println(e.getSourceAsString()));
}
}
java
package com.xd.cubemall.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.stream.Stream;
public class IndexSearchTest {
private RestHighLevelClient client;
@BeforeEach
public void init() {
//创建一个client对象
client = new RestHighLevelClient(RestClient.builder(
new HttpHost("1.1.1.1",9200),
new HttpHost("2.2.2.2",9200),
new HttpHost("3.3.3.3",9200)
));
}
@Test
public void searchIndex1() throws Exception {
//http://1.1.1.1:9200/hello1/_search
//{
//"query":{"match_all":{}}
//}
SearchRequest request = new SearchRequest("hello1")
.source(new SearchSourceBuilder()
.query(QueryBuilders.matchAllQuery())
);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//从response对象中取结果
SearchHits hits = response.getHits();
long total = hits.getTotalHits().value;
System.out.println("总记录数:" + total);
SearchHit[] searchHits = hits.getHits();
Stream.of(searchHits).forEach(e-> System.out.println(e.getSourceAsString()));
}
@Test
public void termQuery() throws Exception {
SearchRequest request = new SearchRequest("hello1")
.source(new SearchSourceBuilder()
.query(QueryBuilders.termQuery("title","品牌"))
);
printResult(request);
}
private void printResult(SearchRequest request) throws Exception {
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//从response对象中取结果
//从response对象中取结果
SearchHits hits = response.getHits();
long total = hits.getTotalHits().value;
System.out.println("总记录数:" + total);
SearchHit[] searchHits = hits.getHits();
Stream.of(searchHits).forEach(e-> {
System.out.println(e.getSourceAsString());
System.out.println(e.getHighlightFields());
});
}
@Test
public void searchPage() throws Exception {
SearchRequest request = new SearchRequest("hello1")
.source(new SearchSourceBuilder()
//查询条件
.query(QueryBuilders.matchQuery("title","明星开饭店频翻车奔跑吧"))
//过滤
.postFilter(QueryBuilders.termQuery("title","明星"))
.postFilter(QueryBuilders.termQuery("content","围城"))
//高亮显示
.highlighter(new HighlightBuilder()
.field("title")
.field("content")
.preTags("<em>")
.postTags("</em>")
)
//分页条件
.from(0)
.size(30)
);
printResult(request);
}
}