Elasticsearch Java APIのSearch API
5611 ワード
このAPIは非常に重要であり、内包的なインタフェースでもある.よく使うのが本当にいいです.
列のサブ1:
説明結果:
クラスタ全体を調べた場合:
JAVAでのScrollsの使用
MultiSearch API
Aggregations統計
クエリーテンプレートの使用(search Templates)
1.
2、javaプログラムで次のように宣言します.
3、実行:
または、
A.先に保存する.scriptsファイル
B.
列のサブ1:
SearchResponse response = client.prepareSearch("index1", "index2") // index1\index2
.setTypes("type1", "type2") // type1\type2
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termQuery("multi", "test")) // Query
.setPostFilter(FilterBuilders.rangeFilter("age").from(12).to(18)) // Filter
.setFrom(0).setSize(60).setExplain(true)
.execute()
.actionGet();
//
print(reponse);
説明結果:
public void print(SearchResponse response){
SearchHits hits = response.getHits();
SearchHit[] searchHists = hits.getHits();
if (searchHists.length > 0) {
for (SearchHit hit : searchHists) {
for(Map.Entry<String, SearchHitField> e : hit.fields().entrySet()){
System.out.println("fieldName="+e.getKey() +"\t fieldValue="+ e.getValue().getValues());
}
//
for(Map.Entry<String, HighlightField> e : hit.highlightFields().entrySet()){
StringBuilder highlight = new StringBuilder();
for(Text fragment : e.getValue().fragments()){
highlight.append(fragment).append("...");
}
System.out.println("fieldName="+e.getKey() +"\t fieldValue="+ highlight);
}
}
}
}
クラスタ全体を調べた場合:
SearchResponse response = client.prepareSearch().execute().actionGet();
JAVAでのScrollsの使用
import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
QueryBuilder qb = termQuery("multi", "test");
SearchResponse scrollResp = client.prepareSearch(test)
.setSearchType(SearchType.SCAN)
.setScroll(new TimeValue(60000))
.setQuery(qb).setSize(100).execute().actionGet();
//100 hits per shard will be returned for each scroll//Scroll until no hits are returned
while (true) {
for (SearchHit hit : scrollResp.getHits().getHits()) {
//Handle the hit...
}
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
//Break condition: No hits are returned
if (scrollResp.getHits().getHits().length == 0) {
break;
}
}
MultiSearch API
SearchRequestBuilder srb1 = node.client()
.prepareSearch().setQuery(QueryBuilders.queryString("elasticsearch")).setSize(1);
SearchRequestBuilder srb2 = node.client()
.prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);
MultiSearchResponse sr = node.client().prepareMultiSearch()
.add(srb1)
.add(srb2)
.execute().actionGet();
// You will get all individual responses from MultiSearchResponse#getResponses()
long nbHits = 0;
for (MultiSearchResponse.Item item : sr.getResponses()) {
SearchResponse response = item.getResponse();
nbHits += response.getHits().getTotalHits();
}
Aggregations統計
SearchResponse sr = node.client().prepareSearch()
.setQuery(QueryBuilders.matchAllQuery())
.addAggregation(
AggregationBuilders.terms("agg1").field("field")
)
.addAggregation(
AggregationBuilders.dateHistogram("agg2")
.field("birth")
.interval(DateHistogram.Interval.YEAR)
)
.execute().actionGet();// Get your facet results
Terms agg1 = sr.getAggregations().get("agg1");
DateHistogram agg2 = sr.getAggregations().get("agg2");
クエリーテンプレートの使用(search Templates)
1.
config/scripts
でテンプレートを追加します.例えば:config/scripts/template_gender.mustache
{
"template" : {
"query" : {
"match" : {
"gender" : "{{param_gender}}"
}
}
}}
2、javaプログラムで次のように宣言します.
Map<String, Object> template_params = new HashMap<>();
template_params.put("param_gender", "male");
3、実行:
SearchResponse sr = client.prepareSearch()
.setTemplateName("template_gender")
.setTemplateType(ScriptService.ScriptType.FILE)
.setTemplateParams(template_params)
.get();
または、
A.先に保存する.scriptsファイル
client.preparePutIndexedScript("mustache", "template_gender",
"{
" +
" \"template\" : {
" +
" \"query\" : {
" +
" \"match\" : {
" +
" \"gender\" : \"{{param_gender}}\"
" +
" }
" +
" }
" +
" }
" +
"}").get();
B.
ScriptService.ScriptType.INDEXED
声明を用いて実行する:SearchResponse sr = client.prepareSearch()
.setTemplateName("template_gender")
.setTemplateType(ScriptService.ScriptType.INDEXED)
.setTemplateParams(template_params)
.get();