基本查询

先来熟QueryBuilders这个类封装的基本查询

  • QueryBuilders.matchQuery() 会根据分词器进行分词,分词之后去查询
  • QueryBuilders.termQuery() 不会进行分词,且完全等于才会匹配
  • QueryBuilders.termsQuery() 一个字段匹配多个值,where name = ‘A’ or name = ‘B’
  • QueryBuilders.multiMatchQuery() 会分词 一个值对应多个字段 where username = ‘zs’ or password = ‘zs’
  • QueryBuilders.matchPhraseQuery() 不会分词,当成一个整体去匹配,相当于 %like%
  • 如果想使用一个字段匹配多个值,并且这多个值是and关系,如下 要求查询的数据中必须包含北京‘和’天津QueryBuilders.matchQuery(“address”,“北京 天津”).operator(Operator.AND)
  • 如果想使用一个字段匹配多个值,并且这多个值是or关系,如下 要求查询的数据中必须包含北京‘或’天津
    QueryBuilders.matchQuery(“address”,“北京 天津”).operator(Operator.OR)

复合查询

通常都是将多个查询条件组合在一起,常用的有must、must_not、should

  • 测试数据
    有这样一段测试数据
{"address":"鸿星尔克0","codes":"中国人民银行0","id":0,"name":"平安富士康0"}
	 {"address":"鸿星尔克1","codes":"中国人民银行1","id":1,"name":"平安富士康1"}
	 {"address":"鸿星尔克2","codes":"中国人民银行2","id":2,"name":"平安富士康2"}
	 {"address":"鸿星尔克3","codes":"中国人民银行3","id":3,"name":"平安富士康3"}
	 {"address":"鸿星尔克4","codes":"中国人民银行4","id":4,"name":"平安富士康4"}
	 {"address":"汽车家族0","id":0,"name":"快递收货0"}
	 {"address":"汽车家族1","id":1,"name":"快递收货1"}
	 {"address":"汽车家族2","id":2,"name":"快递收货2"}
	 {"address":"汽车家族3","id":3,"name":"快递收货3"}
	 {"address":"汽车家族4","id":4,"name":"快递收货4"}
	 {"address":"新鲜水果-徐鲸鱼0","id":0,"name":"东方不败0"}
	 {"address":"新鲜水果-徐鲸鱼1","id":1,"name":"东方不败1"}
	 {"address":"新鲜水果-徐鲸鱼2","id":2,"name":"东方不败2"}
	 {"address":"新鲜水果-徐鲸鱼3","id":3,"name":"东方不败3"}
	 {"address":"新鲜水果-徐鲸鱼4","id":4,"name":"东方不败4"}
  1. 查询1:查询address 必须包含’汽车’ 或者’水果’的,查询id必须不能包含’2’和’0’的。
    此时需要构造两个条件,类似mysql中的 where (address like %汽车% or
    address like %水果% ) and (id != 1 or id != 2)
@Test
	public void testBoolQuery() throws IOException {
		SearchRequest searchRequest = new SearchRequest(indexName);
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

		BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
		boolQuery.must(QueryBuilders.matchQuery("address","水果 汽车").operator(Operator.OR));
		boolQuery.mustNot(QueryBuilders.termsQuery("id","0","2"));
		searchSourceBuilder.query(boolQuery);

		searchRequest.source(searchSourceBuilder);

		SearchResponse search = client.search(searchRequest, ElasticSearchConfig.COMMON_OPTIONS);
		for (SearchHit documentFields : search.getHits().getHits()) {
			System.out.println(documentFields.getScore() + ":::"  +documentFields.getSourceAsString());
		}
	}

查看日志打印

AggregationBuilder 设置分组字段_数据


为什么address中包含汽车的得分高于包含水果的,因为包含汽车的address字段的值有5个字符,包含水果的address字段值包含9个字符,字符越少的,得分越高。之前在must条件中是这么写的

AggregationBuilder 设置分组字段_Test_02


后来发现一直查不到数据,发现这样写不对,我们查询需求是:address必须包含‘汽车’或者‘水果的’,但如果这样写的话就成了address中既要匹配‘水果’又要匹配,显然没有哪个address字段值既包含水果又包含汽车的,所以查询不到。

  1. 查询2:查询address 必须包含’汽车’ 或者’水果’的,查询id必须不能包含’2’和’0’的。name最好是包含 '东方’的。这个最好包含是有最好,没有也无所谓,此时需要用should来封装。
@Test
	public void testBoolQuery() throws IOException {
		SearchRequest searchRequest = new SearchRequest(indexName);
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

		BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
		boolQuery.must(QueryBuilders.matchQuery("address","水果 汽车").operator(Operator.OR));
		boolQuery.mustNot(QueryBuilders.termsQuery("id","0","2"));
		boolQuery.should(QueryBuilders.matchQuery("name","东方"));
		searchSourceBuilder.query(boolQuery);
		
		searchRequest.source(searchSourceBuilder);

		SearchResponse search = client.search(searchRequest, ElasticSearchConfig.COMMON_OPTIONS);
		for (SearchHit documentFields : search.getHits().getHits()) {
			System.out.println(documentFields.getScore() + ":::"  +documentFields.getSourceAsString());
		}
	}

查看日志

AggregationBuilder 设置分组字段_es_03


此时,在should中指定的条件name中包含东方的,分数就提高了。如果我们将must条件修改为一下,则还是会查询不到数据,因为没有一条文档的address包含‘汽车’或‘水果’同时codes包含‘中国’

AggregationBuilder 设置分组字段_Test_04

复合查询嵌套

boolQuery.must(QueryBuilders.matchQuery("address","水果 汽车").operator(Operator.OR));

在复合查询时,我们使用must构建了 address必须包含‘汽车’或‘水果’的查询条件,但我们也可以给这个must再嵌套一个布尔查询,这个bool查询应该是一个should关系,如下

private BoolQueryBuilder buildBoolQuery(){
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
		boolQueryBuilder.should(QueryBuilders.matchQuery("address","水果"));
		boolQueryBuilder.should(QueryBuilders.matchQuery("address","汽车"));
		return boolQueryBuilder;
	}

修改must构造即可。

AggregationBuilder 设置分组字段_数据_05