【发布时间】:2014-11-20 09:59:07
【问题描述】:
我正在使用 elasticsearch 1.3.1,我想使用 java SPRING 和弹性 java API 来查询它。
我使用 Angularjs 作为前端,我可以很好地查询 elasticsearch。
我想做的是:
1) 使用 angularjs 为 elasticsearch 创建 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 服务并将 1) 中创建的 json 查询发送给它:好的
3) 使用在 1) 中创建的查询的 Web 服务查询 elasticsearch 并保存结果(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 和 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”
感谢您的阅读和帮助
【问题讨论】:
-
你好。这项工作对我来说是一个简单的查询:pastebin.com/51n6SxYP 让我们进行一个复杂的 bool&must 查询
标签: java json elasticsearch