ElasticSearch BoolQuery API
本案例都采用mysql 和 elasticsearch 做对比来描叙elasticsearch API
package org.ssgroup;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
/**
* Bool Query 用于组合多个叶子或复合查询子句的默认查询
* must 相当于 与 & =
* must not 相当于 非 ~ !=
* should 相当于 或 | or
* filter 过滤
*/
public class TestElasticsearch {
public static String[] CLUSTER_IP = new String[] {"192.168.10.252","192.168.10.254","192.168.10.255"};
public static int CLUSTER_PORT = 9300;
public static String CLUSTER_NAME = "my-application";
@SuppressWarnings({ "resource", "unchecked" })
public static void main(String[] args) throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME).build();
TransportClient client = new PreBuiltTransportClient(settings);
for(String ip : CLUSTER_IP) {
client.addTransportAddress(new TransportAddress(InetAddress.getByName(ip),CLUSTER_PORT));
}
//boolQuery:复合查询
//1.搜索发帖日期为2017-01-01,或者帖子ID为XHDK-A-1293-#fJ3的帖子,同时要求帖子的发帖日期绝对不为2017-01-02
//类似mysql:select * from forum.article where (postDate=2017-01-01
// or articleID=XHDK-A-1293-#fJ3 ) and postDate != 2017-01-02
BoolQueryBuilder queryBuilders = QueryBuilders.boolQuery();
List<QueryBuilder> lists = queryBuilders.should();
lists.add(QueryBuilders.termQuery("postDate", "2017-01-01"));
lists.add(QueryBuilders.termQuery("articleID", "XHDK-A-1293-#fJ3"));
queryBuilders.mustNot(QueryBuilders.termQuery("postDate", "2017-01-02"));
SearchResponse response = client.prepareSearch("forum").setTypes("article")
.setQuery(queryBuilders).get();
for(SearchHit searchHit : response.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
System.out.println("***********************************************************");
//2.搜索帖子ID为XHDK-A-1293-#fJ3,或者是帖子ID为JODL-X-1937-#pV7而且发帖日期为2017-01-01的帖子
//类似mysql:select * from forum.article where articleID=XHDK-A-1293-#fJ3
// or (articleID=JODL-X-1937-#pV7 and postDate = 2017-01-01)
queryBuilders = QueryBuilders.boolQuery();
lists = queryBuilders.should();
lists.add(QueryBuilders.termQuery("articleID.keyword", "XHDK-A-1293-#fJ3"));
BoolQueryBuilder childBuilder = QueryBuilders.boolQuery();
List<QueryBuilder> mustList = childBuilder.must();
mustList.add(QueryBuilders.termQuery("articleID.keyword", "JODL-X-1937-#pV7"));
mustList.add(QueryBuilders.termQuery("postDate", "2017-01-01"));
lists.add(childBuilder);
response = client.prepareSearch("forum").setTypes("article")
.setQuery(queryBuilders).get();
for(SearchHit searchHit : response.getHits().getHits()) {
System.out.println(searchHit.getSourceAsString());
}
}
}