我正在使用elasticsearch 1.3.1,我想用java彈性&彈性java API查詢它。使用JSON查詢elasticsearch使用
我使用Angularjs作爲前端,我可以很好地查詢elasticsearch。
我想要做的是:
1)創建elasticsearch與angularjs的JSON查詢:OK
{
"bool" : {
"must" : [{"query_string" : { "query" : "***"}}],
"must_not" : [
{"term" : {"O_LIFESTAGE" : "lymphe" }},
{"term" : {"O_SEX" : "m"}},
{"term" : {"L_CONTINENT" : "europe" }},
{"term" : {"I_INSTITUTIONCODE" : "herbierparis"}},
{"term" : {"I_INSTITUTIONCODE" : "herbiercaen"}},
{"term" : {"I_INSTITUTIONCODE" : "herbiertoulouse"}}
]
}
}
2)調用Java SPRING REST Web服務,並將其發送的JSON查詢創建於1):ok
3)web服務查詢elasticsearch與在1中創建的查詢並保存結果(id列表):我失敗了,我得到一個錯誤(嵌套異常是org.elasticsearch.indices .IndexMissingException:[donnees]缺少),其代碼如下:
Node node = nodeBuilder().clusterName("elasticsearch noeud 1").node();
Client client = node.client();
String myquery = "{\"bool\": {\"must\": [{\"query_string\": {\"query\": \"***\"}}],\"must_not\": [{\"term\": {\"O_LIFESTAGE\": \"lymphe\"}},{\"term\": {\"O_SEX\": \"m\"}},{\"term\": {\"L_CONTINENT\": \"europe\"}},{\"term\": {\"I_INSTITUTIONCODE\": \"herbierparis\"}},{\"term\": {\"I_INSTITUTIONCODE\": \"herbiercaen\"}},{\"term\": {\"I_INSTITUTIONCODE\": \"herbiertoulouse\"}}]}}";
//WrapperQueryBuilder querybuilder= new WrapperQueryBuilder(query) ;
//try to get it work with an easy query before trying with "myquery"
SearchResponse searchResponse = client.prepareSearch("donnees").setTypes("specimens").setQuery("{\"term\": {\"L_CONTINENT\": \"europe\"}}").execute().actionGet();
SearchHit[] results = searchResponse.getHits().getHits();
System.out.println("Current results: " + results.length);
for (SearchHit hit : results) {
System.out.println("------------------------------");
Map<String,Object> result = hit.getSource();
System.out.println(result);
}
node.close();
這是我創造我的elasticsearch指數:
curl -XPOST 'localhost:9200/donnees'
這是該指數映射:
curl -XPUT 'localhost:9200/donnees/specimens/_mapping' -d '{
"specimens" : {
"_all" : {"enabled" : true},
"_index" : {"enabled" : true},
"_id" : {"index": "not_analyzed", "store" : false},
"properties" : { ... }}}'
數據導入:
curl -XPUT 'localhost:9200/_river/donnees_s/_meta' -d '{
"type" : "jdbc",
"jdbc" : {
"index" : "donnees",
"type" : "specimens",
"url" : "jdbc:oracle:thin:@localhost:1523:recolnat",
"user" : "user",
"password" : "pass",
"sql" : "select * from all_specimens_data"
}}'
查詢的例子我嘗試在java做的,在捲曲& JS運行良好:
curl -XGET 'http://localhost:9200/donnees/specimens/_search?size=100000000' -d '
{
"fields" : ["O_OCCURRENCEID"],
"query" : {
"bool" : {
"must" : [{"query_string" : { "query" : "***"}}],
"must_not" : [
{"term" : {"O_LIFESTAGE" : "lymphe" }},
{"term" : {"O_SEX" : "m"}},
{"term" : {"L_CONTINENT" : "europe" }},
{"term" : {"I_INSTITUTIONCODE" : "herbierparis"}},
{"term" : {"I_INSTITUTIONCODE" : "herbiercaen"}},
{"term" : {"I_INSTITUTIONCODE" : "herbiertoulouse"}}
]
}
}}'
我無法找到我應該把爲「指數之」誰應該是我的索引(donnees),我甚至嘗試「donnee_s」。 任何建議都非常受歡迎。 並且還,如果有人知道是誰直接查詢完整的「更改爲MyQuery」
謝謝您的閱讀和幫助
打招呼。這項工作對我來說是一個簡單的查詢: http://pastebin.com/51n6SxYP 讓我們去複雜布爾&必須查詢 – AlainIb 2014-09-29 08:17:49