elasticsearch组合多条件查询实现restful api以及java代码实现
实际开发中,基本都是组合多条件查询。
elasticsearch提供bool来实现这种需求;
主要参数:
must
文档 必须 匹配这些条件才能被包含进来。
must_not
文档 必须不 匹配这些条件才能被包含进来。
should
如果满足这些语句中的任意语句,将增加 _score ,否则,无任何影响。它们主要用于修正每个文档的相关性得分。
filter
必须 匹配,但它以不评分、过滤模式来进行。这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档。
OK我们这里多搞几个实例让大伙体验下;
最简单的 模糊查询标题含有“战”:
{
"query": {
"bool": {
"must":{"match":{"title":"战"}}
}
}
}
多条件的话
{
"query": {
"bool": {
"must":[
{"match":{"title":"战"}},
{"match":{"content":"星球"}}
]
}
}
}
就一条结果了;
java代码实现:/**
* 多条件查询
* @throws Exception
*/
@Test
public void searchMutil()throws Exception{
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
QueryBuilder queryBuilder=QueryBuilders.matchPhraseQuery("title", "战");
QueryBuilder queryBuilder2=QueryBuilders.matchPhraseQuery("content", "星球");
SearchResponse sr=srb.setQuery(QueryBuilders.boolQuery()
.must(queryBuilder)
.must(queryBuilder2))
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
运行结果:
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
{"title":"星球大战8:最后的绝地武士","publishDate":"2018-01-05","content":"《星球大战:最后的绝地武士》承接前作《星球大战:原力觉醒》的剧情,讲述第一军团全面侵袭之下,蕾伊(黛西·雷德利 Daisy Ridley 饰)、芬恩(约翰·博耶加 John Boyega 饰)、波·达默龙(奥斯卡·伊萨克 Oscar Isaac 饰)三位年轻主角各自的抉 择和冒险故事。前作中觉醒强大原力的蕾伊独自寻访隐居的绝地大师卢克·天行者(马克·哈米尔 Mark Hamill 饰),在后者的指导下接受原力训练。芬恩接受了一项几乎不可能完成的任务,为此他不得不勇闯敌营,面对自己的过去。波·达默龙则要适应从战士向领袖的角色转换,这一过程中他也将接受一些血的教训。","director":"莱恩·约翰逊","price":"55"}
must_not使用
内容里不含有“武士”
{
"query": {
"bool": {
"must":{"match":{"title":"战"}},
"must_not":{"match":{"content":"武士"}}
}
}
}
结果:
java代码实现:
/**
* 多条件查询
* @throws Exception
*/
@Test
public void searchMutil2()throws Exception{
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
QueryBuilder queryBuilder=QueryBuilders.matchPhraseQuery("title", "战");
QueryBuilder queryBuilder2=QueryBuilders.matchPhraseQuery("content", "武士");
SearchResponse sr=srb.setQuery(QueryBuilders.boolQuery()
.must(queryBuilder)
.mustNot(queryBuilder2))
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
结果一致;
should使用 提高得分;
前面:
{
"query": {
"bool": {
"must":[
{"match":{"title":"战"}}
]
}
}
}
得分情况我们看下:
我们加下should
{
"query": {
"bool": {
"must":{"match":{"title":"战"}},
"should":[
{"match":{"content":"星球"}},
{"range":{"publishDate":{"gte":"2018-01-01"}}}
]
}
}
}
执行下:
自己看得分 不解释;
java代码实现:/**
* 多条件查询
* @throws Exception
*/
@Test
public void searchMutil3()throws Exception{
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
QueryBuilder queryBuilder=QueryBuilders.matchPhraseQuery("title", "战");
QueryBuilder queryBuilder2=QueryBuilders.matchPhraseQuery("content", "星球");
QueryBuilder queryBuilder3=QueryBuilders.rangeQuery("publishDate").gt("2018-01-01");
SearchResponse sr=srb.setQuery(QueryBuilders.boolQuery()
.must(queryBuilder)
.should(queryBuilder2)
.should(queryBuilder3))
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getScore()+":"+hit.getSourceAsString());
}
}
执行:
省略
filter过滤;
票价必须少于40
{
"query": {
"bool": {
"must": {
"match": {"title": "战"}
},
"filter": {
"range": {"price": {"lte":"40"}}
}
}
}
}
执行:
java代码实现:/**
* 多条件查询
* @throws Exception
*/
@Test
public void searchMutil4()throws Exception{
SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
QueryBuilder queryBuilder=QueryBuilders.matchPhraseQuery("title", "战");
QueryBuilder queryBuilder2=QueryBuilders.rangeQuery("price").lte(40);
SearchResponse sr=srb.setQuery(QueryBuilders.boolQuery()
.must(queryBuilder)
.filter(queryBuilder2))
.execute()
.actionGet();
SearchHits hits=sr.getHits();
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}
}
运行结果一致;