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());
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
spring-data-elasticsearch 페이지 조회
부록:
1. 이름에서 알 수 있듯이QueryBuilder는 검색 조건, 필터 조건을 구축하는 데 사용되고 SortBuilder는 정렬을 구축하는 데 사용된다.
예를 들어 우리는 어느 위치에서 100미터 범위 내의 모...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
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());
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
spring-data-elasticsearch 페이지 조회부록: 1. 이름에서 알 수 있듯이QueryBuilder는 검색 조건, 필터 조건을 구축하는 데 사용되고 SortBuilder는 정렬을 구축하는 데 사용된다. 예를 들어 우리는 어느 위치에서 100미터 범위 내의 모...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.