使用带有JSON的Java查询Elasticsearch [英] query elasticsearch with java with JSON

查看:99
本文介绍了使用带有JSON的Java查询Elasticsearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用elasticsearch 1.3.1,我想用Java SPRING&来查询它.弹性的Java API.

I'm using elasticsearch 1.3.1 and i want to query it with java SPRING & elastic java API.

我将Angularjs用于前端,我可以很好地查询elasticsearch.

I use Angularjs for front-end and i can query elasticsearch very well.

我想做的是:

1)使用angularjs为elasticsearch创建json查询:确定

1) create the json query for elasticsearch with angularjs : 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网络服务并向其发送在1)中创建的json查询:确定

2) to call a java SPRING rest web service and send it the json query created in 1) : ok

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

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 :

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索引的方式:



this is how i create my elasticsearch index :

 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&中运行良好JS:

Example of query i try to do in java, working well in 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"}}
        ]
    }
}}'


我找不到我应该为应该作为我的指数(公吨)的指数"输入的内容,我什至尝试使用"donnee_s".任何建议都非常欢迎.如果有人知道直接向谁查询完整的"myquery"


I can't find what i should put for the "indice" who should be my index (donnees), i even try "donnee_s". Any advice are very very welcomed. And also if someone know who to query directly the full "myquery"

感谢您的阅读和帮助

推荐答案

工作代码,以使用由angularjs制作的jsonquery(在Java和JS弹性api中工作的jsonquery)来查询elasticsearch:

working code to query elasticsearch with a jsonquery made by angularjs ( the jsonquery work in java and 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();   

这篇关于使用带有JSON的Java查询Elasticsearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆