【问题标题】:query elasticsearch with java with JSON使用带有 JSON 的 java 查询 elasticsearch
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    错误信息很清楚:您的索引不存在。

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

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

    【讨论】:

    • 感谢您的帮助。这是我的 elasticsearch 服务器的捕获:img4.hostingpics.net/pics/510170Sanstitre.png 我还运行 curl:pastebin.com/RtMMWTCE 我更改了我的 java 代码,但仍然出错。我对如何做感到困惑: Node node = nodeBuilder().clusterName("elasticsearch").node();客户端客户端 = node.client(); SearchResponse searchResponse = client.prepareSearch("donnees").setTypes("specimens").setQuery("{\"term\": {\"L_CONTINENT\": \"europe\"}}").execute()。动作获取();她的完整错误:pastebin.com/sVc5HSz3 谢谢
    • 您现在正在查询同一个集群(名称elasticsearch)。您的新问题似乎与河流初始化有关。也许你应该为此打开另一个问题。
    猜你喜欢
    • 2022-10-14
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2014-10-21
    • 1970-01-01
    相关资源
    最近更新 更多