search java api
package com.my.search;
import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.script.mustache.SearchTemplateRequestBuilder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.my.srv.Server;
/*
*
* @ 작성일 : 2019-01-29
* @ 주제 : Search
* @ 작성자 : JunHyeon Kim
* @ 이메일 : junhyeon.kim@ezfarm.co.kr
*/
public class ElasticSearch extends Server {
// ###########################################
SearchResponse response = null;
JSONParser parser = new JSONParser();
JSONObject json = null;
JSONArray jsonArry = null;
// ###########################################
public void doSearch() {
// server
Server.ServerSetting();
Map<String, Object> template_params = new HashMap<String, Object>();
template_params.put("local", "경북");
// Search Test
response = new SearchTemplateRequestBuilder(client)
.setScript("{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"area\" : \"{{local}}\"\n" +
" }\n" +
" }\n" +
"}")
.setScriptType(ScriptType.INLINE)
.setScriptParams(template_params)
.setRequest(new SearchRequest("tb_frip_dayshow"))
.get()
.getResponse();
}
public void doJson() {
// Json file 로 변환 ==============================
JSONObject jsnObj = null;
JSONArray jsnArr = new JSONArray();
// ==============================================
try {
json = (JSONObject) parser.parse(response.toString());
json = (JSONObject) json.get("hits");
jsonArry = (JSONArray) json.get("hits");
//System.out.println(jsonArry);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < jsonArry.size(); i++) {
JSONObject tmpnode = (JSONObject)jsonArry.get(i);
tmpnode = (JSONObject)tmpnode.get("_source");
jsnObj = new JSONObject(); // 객체 생성
jsnObj.put("title", tmpnode.get("title"));
jsnObj.put("place", tmpnode.get("place"));
jsnObj.put("startDate", tmpnode.get("startDate"));
jsnObj.put("gpsX", tmpnode.get("gpsX"));
jsnObj.put("gpsY", tmpnode.get("gpsY"));
jsnArr.add(jsnObj);
}
System.out.println(jsnArr);
}
}
'ELK > elasticsearch' 카테고리의 다른 글
python + elasticsearch (0) | 2019.02.16 |
---|---|
python + elasticsearch + 조회/삽입/생성 (0) | 2019.02.03 |
python + elasticsearch api (0) | 2019.01.29 |
java api elasticsearch aggregation (0) | 2019.01.28 |
java api2 (0) | 2019.01.25 |