简体   繁体   English

使用JSON与Java查询elasticsearch

[英]query elasticsearch with java with JSON

I'm using elasticsearch 1.3.1 and i want to query it with java SPRING & elastic java API. 我正在使用Elasticsearch 1.3.1,我想用Java SPRING和Elastic Java API查询它。

I use Angularjs for front-end and i can query elasticsearch very well. 我将Angularjs用于前端,我可以很好地查询elasticsearch。

What i want to do is : 我想做的是:

1) create the json query for elasticsearch with angularjs : ok 1)使用angularjs为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) to call a java SPRING rest web service and send it the json query created in 1) : ok 2)调用Java SPRING rest Web服务并将其在1)中创建的json查询发送给它:确定

3) the web service query elasticsearch with the query created in 1) and save the result ( list of ids) : i failed, i get an error ( nested exception is org.elasticsearch.indices.IndexMissingException: [donnees] missing ) with the follow code : 3)使用在1)中创建的查询的Web服务查询elasticsearch并保存结果(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();


this is how i create my elasticsearch index : 这就是我创建我的elasticsearch索引的方式:

 curl -XPOST 'localhost:9200/donnees'

this is the index mapping : 这是索引映射:

curl -XPUT 'localhost:9200/donnees/specimens/_mapping' -d '{
"specimens" : {
    "_all" : {"enabled" : true},
    "_index" : {"enabled" : true},
    "_id" : {"index": "not_analyzed", "store" : false},
    "properties" : { ... }}}'

the data import : 数据导入:

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"
 }}'

Example of query i try to do in java, working well in curl & JS : 我尝试在Java中执行查询的示例,在curl&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"}}
        ]
    }
}}'

I can't find what i should put for the "indice" who should be my index (donnees), i even try "donnee_s". 我找不到我应该为应该作为我的指数(公吨)的“指数”输入的内容,甚至尝试了“ donnee_s”。 Any advice are very very welcomed. 任何建议都非常欢迎。 And also if someone know who to query directly the full "myquery" 另外,如果有人知道直接向谁查询完整的“ myquery”

Thank you for your reading and help 感谢您的阅读和帮助

The error message is pretty clear : your index doesn't exist. 错误消息非常清楚:您的索引不存在。

Are you sure about your cluster name? 您确定集群名称吗? In your java code, it is set to elasticsearch noeud 1 , which is more like a node name. 在您的Java代码中,将其设置为elasticsearch noeud 1 ,它更像是节点名称。

You are maybe querying the wrong cluster : try to retrieve the cluster name with a curl on _cluster/state to compare. 您可能正在查询错误的集群:尝试通过_cluster/state上的curl检索集群名称以进行比较。

working code to query elasticsearch with a jsonquery made by angularjs ( the jsonquery work in java and JS elastic api) : 使用angularjs制作的jsonquery查询elasticsearch的工作代码(jsonquery在Java和JS Elastic api中起作用):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM