ElasticSearch 기본 질의(페이지 별 질의, 정렬, 데이터 필터링, 조건 질의, 강조 표시)

5239 단어 ElasticSearch

ElasticSearch 기본 질의(페이지 별 질의, 정렬, 데이터 필터링, 조건 질의, 강조 표시)


ElasticSearch Demo:

package org.ssgroup;

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

import org.elasticsearch.action.search.SearchRequestBuilder;
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.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class ElasticSearchQuery {
	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;
	
	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));
		
		// 
		//searchAll(client);
		
		// 
		//searchPage(client);
		
		// 
		//searchSort(client);
		
		// ( )
		//searchInclude(client);
		
		// 
		//searchByCondition(client);
		
		// 
		searchHighLight(client);
		
		client.close();
	}
	
	/**
	 *  
	 * @param client
	 */
	public static void searchAll(TransportClient client) {
		SearchRequestBuilder builder = client.prepareSearch("film").setTypes("dongzuo");
		SearchResponse response = builder.setQuery(QueryBuilders.matchAllQuery()).execute().actionGet();
		SearchHits searchHits = response.getHits();
		for(SearchHit searchHit : searchHits.getHits()) {
			System.out.println(searchHit.getSourceAsString());
		}
	}
	
	/**
	 *  
	 * @param client
	 */
	public static void searchPage(TransportClient client) {
		SearchRequestBuilder builder = client.prepareSearch("film").setTypes("dongzuo");
		SearchResponse response = builder.setQuery(QueryBuilders.matchAllQuery())
				.setFrom(0).setSize(2)
					.execute().actionGet();
		SearchHits searchHits = response.getHits();
		for(SearchHit searchHit : searchHits.getHits()) {
			System.out.println(searchHit.getSourceAsString());
		}
	}
	
	/**
	 *  
	 * @param client
	 */
	public static void searchSort(TransportClient client) {
		SearchRequestBuilder builder = client.prepareSearch("film").setTypes("dongzuo");
		SearchResponse response = builder.setQuery(QueryBuilders.matchAllQuery())
				.addSort("publishDate",SortOrder.DESC)
					.execute().actionGet();
		SearchHits searchHits = response.getHits();
		for(SearchHit searchHit : searchHits.getHits()) {
			System.out.println(searchHit.getSourceAsString());
		}
	}
	
	/**
	 *  
	 * @param client
	 */
	public static void searchInclude(TransportClient client) {
		SearchRequestBuilder builder = client.prepareSearch("film").setTypes("dongzuo");
		SearchResponse response = builder.setQuery(QueryBuilders.matchAllQuery())
				.setFetchSource(new String[] {"title","price"}, null)
					.execute().actionGet();
		SearchHits searchHits = response.getHits();
		for(SearchHit searchHit : searchHits.getHits()) {
			System.out.println(searchHit.getSourceAsString());
		}
	}
	
	/**
	 *  
	 * @param client
	 */
	public static void searchByCondition(TransportClient client) {
		SearchRequestBuilder builder = client.prepareSearch("film").setTypes("dongzuo");
		SearchResponse response = builder.setQuery(QueryBuilders.matchQuery("title", " "))
				.setFetchSource(new String[] {"title","price"}, null)
					.execute().actionGet();
		SearchHits searchHits = response.getHits();
		for(SearchHit searchHit : searchHits.getHits()) {
			System.out.println(searchHit.getSourceAsString());
		}
	}
	
	/**
	 *  
	 * @param client
	 */
	public static void searchHighLight(TransportClient client) {
		SearchRequestBuilder builder = client.prepareSearch("film").setTypes("dongzuo");
		HighlightBuilder highlightBuilder = new HighlightBuilder();
		highlightBuilder.preTags("

"); highlightBuilder.postTags("

"); highlightBuilder.field("title"); SearchResponse response = builder.setQuery(QueryBuilders.matchQuery("title", " ")) .highlighter(highlightBuilder) .setFetchSource(new String[] {"title","price"}, null) .execute().actionGet(); SearchHits searchHits = response.getHits(); for(SearchHit searchHit : searchHits.getHits()) { System.out.println(searchHit.getSourceAsString()); System.out.println(" :"+searchHit.getHighlightFields()); } } }

좋은 웹페이지 즐겨찾기