[英]query elasticsearch with java with JSON
我正在使用Elasticsearch 1.3.1,我想用Java SPRING和Elastic Java API查询它。
我将Angularjs用于前端,我可以很好地查询elasticsearch。
我想做的是:
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)调用Java SPRING rest Web服务并将其在1)中创建的json查询发送给它:确定
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();
这就是我创建我的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中执行查询的示例,在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”
感谢您的阅读和帮助
错误消息非常清楚:您的索引不存在。
您确定集群名称吗? 在您的Java代码中,将其设置为elasticsearch noeud 1
,它更像是节点名称。
您可能正在查询错误的集群:尝试通过_cluster/state
上的curl检索集群名称以进行比较。
使用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.