public void test() throws IOException {
#创建一个Search对象
SearchRequest searchRequest = new SearchRequest();
#创建一个Builder对象,对条件进行封装
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
#查询条件:在上面条件的基础上加上字段jiage 内容含有329的数据
searchSourceBuilder.query(QueryBuilders.matchQuery("jiage", "329"));
#从搜索结果中取第0条开始的10条数据,数据量最多不超过10000会报错,有解决方案百度
searchSourceBuilder.from(0);
searchSourceBuilder.size(10);
#排序字段id按照正序排序
searchSourceBuilder.sort(new FeildSortBuilder("id").order(Sortorder.ASC));
searchSourceBuilder.fetchSource(false);
#参数,用于是否需要过滤
String[ ] includeFields = new String[ ]{"id","dizhi",,"text","jiage"};
#第一个参数是需要显示的字段,第2个参数是需要过滤的字段
searchSourceBuilder.fectchSource(includeFields,null);
#进行构建
searchRequest.source(searchSourceBuilder);
searchRequest.scroll(TimeValue.timeValueMinutes(1L));
SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
# 获取数据,数据可以是多种类型的
SearchHits hits = searchResponse.getHits();
SearchHit[ ] hits1 = hits.getHits();
List list = new ArrayList();
for (SearchHit hit : hits1) {
list.add(hit.getSourceAsString());
}
System.out.print(list);
}
}
SearchHit[ ] searchHits = hits.getHits( );
for (SearchHit hit : searchHits ){
//结果的Index
String index = hit.getIndex();
//结果的type
String type = hit.getType();
//结果的ID
String id = hit.getId();
//结果的评分
float score = hit.getScore();
//查询结果 JSON字符串形式
String sourceAsString = hit.getSourceAsString();
//查询结果Map的形式
Map<String , Object> sourceMap = hit.getSourceAsMap();
//Document的title
String documentTitle = (String)sourceAsMap.get("title");
//结果中的某个list
List<Object> users = (List<Object>)sourceAsMap.get("user");
//结果中的某个Map
Map<String, Object> innerObject = (Map<String, Object>)sourceAsMap.get("innerObject");
}
QueryBuilder 常用来配合Search查询来使用
boolQuery() 布尔查询,可以用来组合多个查询条件
fuzzyQuery()相似度查询
matchAllQuery()查询所有数据
regexpQuery()正则表达式查询
termQuery()词条查询
wildcardQuery()模糊查询
1.matchQuery匹配查询:
matchQuery可以简单理解为mysql中的like,当我们进行查询的这个field的mapping必须是text类型。
public void test() throws IOException {
SearchRequest searRequest = new SearchSourceBuilder();
#matchQuery 匹配查询
searchSourceBuilder.query(QueryBuilders.matchQuery("text", "19"));
# multiMatchQuery(Object text, String...fieldNames) 多个字段匹配某一个值
# 搜索name中或interest中包含有music的文档(必须与music一致)
public void test() throws IOException{
SearchRequest searRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.multiMatchQuery("music","name","interest"));
}
# wildcardQuery()模糊查询,?匹配单个字符,*匹配多个字符
#搜索名字中含有jack文档(name中只要包含jack即可)
public void test() throws IOException {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.wildcardQuery("name", "*jack?*"));
}
2. matchAllQuery查询所有
查询指定Index和type中的所有记录,相当于sql:select* from sales
public void test( ) throws IOException {
SearchRequest searchRequest = new SearchRequst();
SearchSourceBuilder searchSourceBuilder = new SerachSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
3.termQuery等值搜索
相当于sql中的=,termQuery数据过滤查询
public void test() throws IOException {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery("name","张三"));
#terms 是多值判断
public void test() throws IOException {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termsQuery("name","张三“,"李四","王五"));
4.matchPhraseQuery短语搜索
直接以一个短语的形式进行查询
public void test() throws IOException {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuider.query(QueryBuilders.matchPhraseQuery("name","张三“));
5.prefixQuery前缀搜索
如果我们需要查询title中有“大话西游电影", "大话西游小说",使用prefixQuery查询“大话西游”,那么那两条数据就会出来
public void test( )throws IOException {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.prefixQuery("title","大话西游"));
}
6.disMaxQuery
disMaxQuery适用于多个field的进行搜索,我们在进行多个field搜索时候,可能会遇到多个field匹配到了更多的词会在前面,而一个field匹配了更多词的话就会排名靠后。disMax就是解决这个问题,disMax使搜索到的结果,应该是某一个field中匹配到了尽可能多的关键词,被排在前面;而不是尽可能多的field匹配到了少数的关键词,排在了前面
public void test() throws IOException {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.disMaxQuery().add(QueryBuilders.matchQuery("name", "张三“)));
}
7.boolQuery组合查询条件
boolQuery用来将搜索的条件进行组合,即将多个组合条件组合在一起,常用的几种组合方式有must、should、mustNot,我们拿下面对应的sql语句举例子
#sql: select * from sales where brand = '小米' and color ='红色‘ ,通过两个bool将两个查询条件组合,
#must相当于sql中de=必须匹配的意思
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("brand","小米“)
.must(QueryBuilders.termQuery("color","红色”));
#sql: select * from sales where brand = '小米' or color = '红色‘,使用should相当于sql语句中的or
BoolQueryBuilder boolQuery2 = QueryBuilders.boolQuery();
boolQuery2.should(QueryBuilders.termQuey("brand","小米"))
.should(QueyBuilders.termQuery("color","红色“));
#sql: select * from sales where brand = ‘小米' and color != '红色’ mustNot相当与 !=必须不匹配
BoolQueryBuilder boolQuery3 = QueryBuilders.boolQuery();
boolQuery3.must =(QueryBuilders.termQuery("brand","小米"))
.mustNot(QueryBuilders.termQuery("color","红色"));
#sql: select * from sales where (brand = '小米' or color = ‘红色') and brand != ’长虹'
BoolQueryBuilder boolQuery4 = QueryBuilders.boolQuery();
BoolQueryBuilder boolQuery5 =QueryBuilders.boolQuery();
boolQuerys.should(QueryBuilders.termQuery("brand","小米"))
.should(QueyBuilders.termQuery("color“,”红色"));
boolQuery4.must(boolQuery5)
.mustNot(QueryBuilders.termQuery("brand","长虹"));
//设置查询条件
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
if(StringUtils.isNotEmpty(text)){
//查询条件:字段名为 text,内容含有text的数据
boolQuery.must(QueyBuilders.matchQuery("text", text));
}
if (StringUtils.isNotEmpty(keywords)) {
//查询条件: 字段名为keywords,含有keywords的数据
boolQuery.must(QueryBuilders.matchQuery("keywords", keywords));
}
if(StringUtils.isNotEmpty(topic)) {
//查询条件:字段名为topic,内容含有topic的数据
boolQuery.must(QueryBuilders.matchQuery("topic",topic));
}
8.rangeQuery数据过滤器查询
是范围查询,有时候范围查询比精确查询更有用,比如我们想知道价格在20到40之间的商品;
rangeQuery可以处理数字,日期,以及字符串,不过字符串还是不要使用范围查询的好,效率会很低;对数字取范围没啥好说的,就大于,大于等于、小于、小于等于四个符号加数字就可以;
#闭区间查询
QueryBuilder qb1 = QueryBuilders.rangeQuery("${fieldName}").from(${fieldValue1}).to($fieldValues2});
#开区间查询
QueryBuilder qb1 =
QueryBuilsers.rangeQuery("${fieldName}").from(${fieldValue1},false).to(${fieldValue2}.false};
#大于
QueryBuilder qb1 =
QueryBuilders.rangeQuery("${fieldName}").gt(${fieldValue});
#大于等于
QueryBuilder qb2 =
QueryBuilders.rangeQuery("${fieldName}“).gte($feildValue});
#小于
QueryBuilder qb1 =
QueryBuilders.rangeQuery("${feildName}).lt($fieldValue});
#小于等于
QueryBuilder qb1 = QueryBuilders.rangeQuery("${feildName}").lte(${fieldValue});
#多条件案例
QueryBuilder qb1 =
QueryBuilders.moreLikeThisQuery(new String[ ]{"${feildName1}",new String[ ]{"${feildValue}",null);
QueryBuilder qb2 = Querybuilders.rangeQuery("${fieldName2").gt("${fieldValue2});
QueyBuilder qb3 = QueryBuilders.boolQuery().must(qb1).must(qb2);