ElasticSearch 组合查询(must not_must should filter)

ElasticSearch 组合查询(must not_must should filter)

ElasticSearch Demo:

package org.ssgroup;

import java.io.IOException;
import java.net.InetAddress;

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.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class ElasticSearchMultiQuery {
	private static String ES_HOST1 = "192.168.10.252";
	private static String ES_HOST2 = "192.168.10.254";
	private static String ES_HOST3 = "192.168.10.255";
	private static int ES_PORT = 9300;
	
	/**
	 * 实际开发中,基本都是组合多条件查询
	 * elasticsearch提供Boolean来实现这种需求
	 * 主要参数:
	 * 	must:文档必须匹配这些条件才能被包含进来。
	 *  must_not:文档必须不匹配这些条件才能被包含就来。
	 *  should:如果满足这些语句中的任意语句,将增加_score,否则无任何影响,
	 *  	主要用于修正每个文档的相关性得分。
	 *  filter:必须匹配,但他以不评分、过滤模式来进行,这些语句对评分没有贡献,
	 *  	只是根据过滤标准来排除或包含文档。
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		Settings settings = Settings.builder()
		        .put("cluster.name", "my-application").build();
		
		TransportClient client = new PreBuiltTransportClient(settings)
		        .addTransportAddress(new TransportAddress(InetAddress.getByName(ES_HOST1), ES_PORT))
		        .addTransportAddress(new TransportAddress(InetAddress.getByName(ES_HOST2), ES_PORT))
				.addTransportAddress(new TransportAddress(InetAddress.getByName(ES_HOST3), ES_PORT));
		
		//多条件查询(即包含... 并且包含...) must and must
		//multiSearchMust(client);
		
		//多条件查询(即包含... 并且不包含...) must and must_not
		//multiSearchMustNot(client);
		
		//多条件查询(加权显示)
		//multiSearchShould(client);
		
		//多条件查询(过滤)
		multiSearchFilter(client);
		client.close();
	}
	
	/**
	 * 多条件查询(即包含、并且包含)
	 * @param client
	 */
	public static void multiSearchMust(TransportClient client) {
		SearchResponse response = client.prepareSearch("film").setTypes("dongzuo")
			.setQuery(QueryBuilders.boolQuery()
					.must(QueryBuilders.matchPhraseQuery("title", "战"))
					.must(QueryBuilders.matchPhraseQuery("content", "星球"))
			).execute().actionGet();
		SearchHits searchHits = response.getHits();
		for(SearchHit searchHit : searchHits.getHits()) {
			System.out.println(searchHit.getSourceAsString());
		}
	}
	
	/**
	 * 多条件查询(即包含、并且不包含)
	 * @param client
	 */
	public static void multiSearchMustNot(TransportClient client) {
		SearchResponse response = client.prepareSearch("film").setTypes("dongzuo")
			.setQuery(QueryBuilders.boolQuery()
					.must(QueryBuilders.matchPhraseQuery("title", "战"))
					.mustNot(QueryBuilders.matchPhraseQuery("content", "武士"))
			).execute().actionGet();
		SearchHits searchHits = response.getHits();
		for(SearchHit searchHit : searchHits.getHits()) {
			System.out.println(searchHit.getSourceAsString());
		}
	}
	
	/**
	 * 多条件查询(加权显示)
	 * @param client
	 */
	public static void multiSearchShould(TransportClient client) {
		SearchResponse response = client.prepareSearch("film").setTypes("dongzuo")
			.setQuery(QueryBuilders.boolQuery()
					.must(QueryBuilders.matchPhraseQuery("title", "战"))
					.should(QueryBuilders.matchPhraseQuery("content", "星球"))
					.should(QueryBuilders.rangeQuery("publishDate").gte("2018-01-01"))
			).execute().actionGet();
		SearchHits searchHits = response.getHits();
		for(SearchHit searchHit : searchHits.getHits()) {
			System.out.println("搜索得分:"+searchHit.getScore());
			System.out.println(searchHit.getSourceAsString());
		}
	}
	
	/**
	 * 多条件查询(过滤)
	 * @param client
	 */
	public static void multiSearchFilter(TransportClient client) {
		SearchResponse response = client.prepareSearch("film").setTypes("dongzuo")
			.setQuery(QueryBuilders.boolQuery()
					.must(QueryBuilders.matchPhraseQuery("title", "战"))
					.filter(QueryBuilders.rangeQuery("price").lte("40"))
			).execute().actionGet();
		SearchHits searchHits = response.getHits();
		for(SearchHit searchHit : searchHits.getHits()) {
			System.out.println(searchHit.getSourceAsString());
		}
	}
}

 

你可能感兴趣的:(ElasticSearch)