Java _ elasticsearch
package forest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import static org.elasticsearch.index.query.QueryBuilders.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
// =================================================================
/*
*
* @ author ( junhyeon.kim@ezfarm.co.kr )
* @
*/
class ElasticNode {
// =================================
// 속성
public Client client;
public SearchResponse response;
// =================================
ElasticNode() {
// 생성자
// TODO Auto-generated constructor stub
try {
this.client = new PreBuiltTransportClient(
Settings.builder().put("client.transport.sniff", true)
.put("cluster.name","elasticsearch").build()) // 192.168.2.10:9200 으로 cluster.name 확인
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.2.10"), 9200));
} catch (UnknownHostException e) {
System.out.println(e.getMessage());
}
}
void Search() {
// 메서드
this.response = this.client.prepareSearch("tourareacode") // Index
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termQuery("multi", "test")) // Query
.setPostFilter(QueryBuilders.rangeQuery("age").from(12).to(18)) // Filter
.setFrom(0).setSize(60).setExplain(true)
.get();
}
}
public class forest_search {
public static void main(String[] args) {
// TODO Auto-generated method stub
ElasticNode enode = new ElasticNode();
enode.Search();
}
}
'ELK > elasticsearch' 카테고리의 다른 글
java api2 (0) | 2019.01.25 |
---|---|
java api (0) | 2019.01.25 |
java api _search (0) | 2019.01.25 |
java elasticsearch api 인덱스 생성 (0) | 2019.01.24 |
임시 - 주말간 정리 (0) | 2019.01.08 |