Elasticsearchクエリー構成(Elasticsearch+springboot)
12339 ワード
このコードの機能はesの中のデータをクエリーしてページングして展示して、使用するjarバージョンは2.4です
おおよその流れ
maven依存の追加
propertiesプロパティファイル構成
Springのプロファイル、TransportClientオブジェクトの初期化
データのクエリー操作の実装クラス
添付:
クエリーのタイプ
1、Query and fetchはインデックスのすべてのスライスにクエリー要求を発行し、各スライスが戻るときに要素ドキュメントを(document)計算されたランキング情報とともに返されます.この検索方式が一番速いです.次のいくつかの検索方式に比べて、この検索方法はshardに行って1回だけクエリーする必要があります.しかし、各shardが返す結果の数の和は、ユーザーが要求するsizeのn倍かもしれません.2、query then fetch(デフォルトの検索方法)検索時に検索方法が指定されていない場合は、この検索方法を使用します.この検索方法は、2つのステップに分かれています.最初のステップは、すべてのshardに要求し、各スライスはソートとランキングに関する情報のみを返します(ドキュメントdocumentは含まれません).次に、各スライスで返されたスコアに従って並べ替え、ランキングし、前のsizeドキュメントを取得します.次に2ステップ目を行い、関連するshardからdocumentを取ります.このようにして返されるdocumentは,ユーザが要求するsizeと等しい.3、DFS query and fetchこの方式は第1の方式より1つの初期化配布(initial scatter)ステップが多く、このステップがあれば、検索採点と順位をより正確に制御できるという.4、DFS query then fetchは第2の方式より1つの初期化配布(initial scatter)ステップが多い.
おおよその流れ
maven依存の追加
org.elasticsearch
elasticsearch
2.4.0
propertiesプロパティファイル構成
spring.elasticsearch.host=192.168.101.123
spring.elasticsearch.port=9300
Springのプロファイル、TransportClientオブジェクトの初期化
package com.unioncast.ssp.front.config;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* ElasticsearchConfig
*
* @author zhangzhe
* @date 2017/2/21 16:33
*/
@Configuration
@PropertySource(value = "classpath:/elasticsearch.properties")
public class ElasticsearchConfig {
@Value("${spring.elasticsearch.host}")
private String host;
@Value("${spring.elasticsearch.port}")
private int port;
private static final Logger LOG = LogManager.getLogger(ElasticsearchConfig.class);
@Bean
public TransportClient elasticsearchClient(){ // spring es
TransportClient transportClient = null;
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "bigData-cluster").build();
try {
transportClient = TransportClient
.builder()
.settings(settings)
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName(host), port)
);
} catch (UnknownHostException e) {
LOG.error(" elasticsearch ");
}
LOG.info(" elasticsearch ");
return transportClient;
}
}
データのクエリー操作の実装クラス
package com.unioncast.ssp.front.service.ssp.elasticsearchData;
import com.unioncast.common.page.Pagination;
import com.unioncast.common.restClient.RestResponse;
import com.unioncast.common.ssp.model.SspAdvertiser;
import com.unioncast.common.ssp.model.SspCreative;
import com.unioncast.ssp.front.service.ssp.SspAdvertiserService;
import com.unioncast.ssp.front.service.ssp.SspCreativeService;
import com.unioncast.ssp.front.service.ssp.SspOrderService;
import com.unioncast.ssp.front.service.ssp.SspPlanService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.UnknownHostException;
import java.util.*;
/**
* @author zhangzhe
* @date 2017/2/21 10:25
*/
@Component
public class ElasticsearchADReportImpl implements ElasticsearchADReport {
private static final Logger LOG = LogManager.getLogger(ElasticsearchADReportImpl.class);
private static final String INDEX = "hehe"; //es
private static final String TYPE = "kafka_type"; //es
private static final Integer PAGESIZE = 10; //
private static final String ESC = "ip"; //
@Resource
TransportClient transportClient; // es
@Resource
SspAdvertiserService sspAdvertiserService;
@Resource
SspOrderService sspOrderService;
@Resource
SspPlanService sspPlanService;
@Resource
SspCreativeService sspCreativeService;
//es
public Pagination
添付:
クエリーのタイプ
1、Query and fetchはインデックスのすべてのスライスにクエリー要求を発行し、各スライスが戻るときに要素ドキュメントを(document)計算されたランキング情報とともに返されます.この検索方式が一番速いです.次のいくつかの検索方式に比べて、この検索方法はshardに行って1回だけクエリーする必要があります.しかし、各shardが返す結果の数の和は、ユーザーが要求するsizeのn倍かもしれません.2、query then fetch(デフォルトの検索方法)検索時に検索方法が指定されていない場合は、この検索方法を使用します.この検索方法は、2つのステップに分かれています.最初のステップは、すべてのshardに要求し、各スライスはソートとランキングに関する情報のみを返します(ドキュメントdocumentは含まれません).次に、各スライスで返されたスコアに従って並べ替え、ランキングし、前のsizeドキュメントを取得します.次に2ステップ目を行い、関連するshardからdocumentを取ります.このようにして返されるdocumentは,ユーザが要求するsizeと等しい.3、DFS query and fetchこの方式は第1の方式より1つの初期化配布(initial scatter)ステップが多く、このステップがあれば、検索採点と順位をより正確に制御できるという.4、DFS query then fetchは第2の方式より1つの初期化配布(initial scatter)ステップが多い.