[Elasticsearch] JSON 스트링으로 질의요청
Elastic/Elasticsearch 2018. 4. 3. 13:41예전에도 JSON 스트링을 가지고 setSource() 함수로 질의 했었는데요.
필요하신 분들이 계신것 같아 쉬운 샘플 코드 올려 봅니다.
[Source Code]
package org.elasticsearch.action.search;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
public class HelloSearchSourceBuilder {
public static void main (String[] args) {
Client client;
client = new PreBuiltTransportClient(Settings.EMPTY);
SearchRequestBuilder searchRequestBuilder = client.prepareSearch();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("field1","value1");
System.out.println(termQueryBuilder.toString());
searchSourceBuilder.query(termQueryBuilder).from(0).size(10);
System.out.println(searchSourceBuilder.toString());
searchRequestBuilder.setSource(searchSourceBuilder);
System.out.println(searchRequestBuilder.toString());
}
}
[Output]
[2018-04-03T13:36:38,466][INFO ][o.e.p.PluginsService ] [_client_] no modules loaded
[2018-04-03T13:36:38,470][INFO ][o.e.p.PluginsService ] [_client_] loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
[2018-04-03T13:36:38,470][INFO ][o.e.p.PluginsService ] [_client_] loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
[2018-04-03T13:36:38,470][INFO ][o.e.p.PluginsService ] [_client_] loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
[2018-04-03T13:36:38,470][INFO ][o.e.p.PluginsService ] [_client_] loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
[2018-04-03T13:36:38,470][INFO ][o.e.p.PluginsService ] [_client_] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
{
"term" : {
"field1" : {
"value" : "value1",
"boost" : 1.0
}
}
}
{"from":0,"size":10,"query":{"term":{"field1":{"value":"value1","boost":1.0}}}}
{"from":0,"size":10,"query":{"term":{"field1":{"value":"value1","boost":1.0}}}}
Process finished with exit code 0