2014-09-25 124 views
0

我正在使用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」

謝謝您的閱讀和幫助

+0

打招呼。這項工作對我來說是一個簡單的查詢: http://pastebin.com/51n6SxYP 讓我們去複雜布爾&必須查詢 – AlainIb 2014-09-29 08:17:49

回答

0

的錯誤信息是很清楚的:你的索引不存在。

您確定您的集羣名稱?在你的java代碼中,它被設置爲elasticsearch noeud 1,這更像是一個節點名稱。

您可能正在查詢錯誤的羣集:嘗試使用捲曲來檢索羣集名稱,以便與_cluster/state進行比較。

+0

感謝您的幫助。 – AlainIb 2014-09-26 07:10:08

+0

它解決了你的問題嗎? – ThomasC 2014-09-26 07:16:31

+0

感謝您的幫助。 這是我elasticsearch服務器的捕獲: http://img4.hostingpics.net/pics/510170Sanstitre.png 我也跑了捲曲: http://pastebin.com/RtMMWTCE 我改變我的Java代碼但仍然錯誤。我很困惑如何去做: Node node = nodeBuilder()。clusterName(「elasticsearch」)。node(); 客戶端客戶端= node.client(); (「samples」)。setQuery(「{\」term \「:{\」L_CONTINENT \「:\」europe \「}}」)。execute() .actionGet(); 完整錯誤她: http://pastebin.com/sVc5HSz3 謝謝 – AlainIb 2014-09-26 07:18:45

0

工作代碼查詢elasticsearch與angularjs取得了jsonquery(Java和JS彈性API的jsonquery工作):

import static org.elasticsearch.node.NodeBuilder.*; 
import org.elasticsearch.action.search.SearchRequestBuilder; 
import org.elasticsearch.action.search.SearchResponse; 
import org.elasticsearch.action.search.SearchType; 
import org.elasticsearch.client.Client; 
import org.elasticsearch.common.settings.ImmutableSettings; 
import org.elasticsearch.common.settings.Settings; 
import org.elasticsearch.node.Node; 
import org.elasticsearch.search.SearchHit; 
... 

Settings settings = ImmutableSettings.settingsBuilder() 
       .put("http.enabled", "false") 
       .put("transport.tcp.port", "9300-9400") 
       .put("discovery.zen.ping.multicast.enabled", "false") 
       .put("discovery.zen.ping.unicast.hosts", "localhost").build(); 

Node node = nodeBuilder().client(true).settings(settings) 
     .clusterName("elasticsearch").node(); 
Client client = node.client(); 

//jsonquery is made by angularjs like : "{\"bool\" : {\"must\" : [{\"query_string\" : {\"query\" : \"***\"}}],\"must_not\" : [{\"term\" : {\"O_SEX\" : \"m\"}}, {\"term\" : {\"L_CONTINENT\" : \"amerique\"}}, {\"term\" : {\"I_INSTITUTIONCODE\" : \"herbiertoulouse\"}}]}}"; 

SearchRequestBuilder searchRequestBuilder = client.prepareSearch("donnees").setSearchType(SearchType.DFS_QUERY_THEN_FETCH).addField("O_OCCURRENCEID").setQuery(jsonquery); 

if(limit != null){ searchRequestBuilder.setSize(limit); }else{ searchRequestBuilder.setSize(250000000); } 
if(offset !=null){ searchRequestBuilder.setFrom(offset); }else{ searchRequestBuilder.setFrom(0); } 

SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); 

SearchHit[] results = searchResponse.getHits().getHits(); 
System.out.println("Current results: " + results.length); 

int length = results.length; 
for (int rnum = 1; rnum<=length;rnum++){ 
    SearchHit hit = results[rnum-1]; 
    System.out.println(hit.getFields().get("O_OCCURRENCEID").getValue() ); 
}   
node.close();