使用带JSON的java查询elasticsearch

时间:2014-09-25 14:58:44

标签: java json elasticsearch

我使用的是elasticsearch 1.3.1,我想用java SPRING&弹性java API。

我使用Angularjs作为前端,我可以很好地查询elasticsearch。

我想做的是:

1)使用angularjs:ok

创建elasticsearch的json查询
    {
    "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休息Web服务并向其发送1)中创建的json查询:ok

3)web服务查询elasticsearch与1)中创建的查询并保存结果(id列表):我失败了​​,我得到一个错误(嵌套异常是org.elasticsearch.indices.IndexMissingException:[donnees] missing )使用以下代码:

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();


这就是我创建弹性搜索索引的方式:

 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中做的查询示例,在curl&amp; 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"}}
        ]
    }
}}'

我无法找到我应该为&#34; indice&#34;谁应该是我的指数(donnees),我甚至尝试&#34; donnee_s&#34;。 任何建议都非常欢迎。 如果有人知道直接查询完整的&#34; myquery&#34;

感谢您的阅读和帮助

2 个答案:

答案 0 :(得分:0)

错误信息非常明确:您的索引不存在。

您确定您的群集名称吗?在您的Java代码中,它设置为elasticsearch noeud 1,这更像是节点名称。

您可能正在查询错误的群集:尝试使用_cluster/state上的卷曲检索群集名称进行比较。

答案 1 :(得分:0)

使用angularjs生成的jsonquery查询elasticsearch的工作代码(java和JS elastic 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();