弹性搜索未执行搜索命令

时间:2018-06-15 15:50:10

标签: java elasticsearch

我正在使用Elastic Search(和Kibana)v 6.3.0。我无法通过使用Kibana显示的RestHighLevelClient获取搜索结果。以下是按问题复制的步骤,请让我知道您的想法。

  1. 使用我提交的传输客户端

    CreateIndexRequest createIndexRequest = new 
    CreateIndexRequest("phenotype");
    Settings settings = Settings.builder()
                .put("index.number_of_replicas", 2)
                .put("index.number_of_shards", 3)
                .build();
    createIndexRequest.settings(settings);
    CreateIndexResponse createIndexResponse = 
    transportClient.admin().indices().create(createIndexRequest).actionGet();
    
  2. 然后,我为名为key1的字段名称提交映射更新,为其提供字段类型keyword。使用Kibana Dev Tools选项卡和命令GET /phenotype/_mappings,我可以验证步骤1和2是否成功。

  3. 我使用仅包含IndexResponse indexResponse = elasticSearchRepository.save(document1);信息的key1: value1命令将文档保存到Elastic Search。

  4. 从Kibana执行命令,

    GET /phenotype/_search
    {
      "query": {
        "term" : {
          "key1" : {
            "value" : "value1",
            "boost" : 1.0
          }
        }
      }
    }
    
  5. 我看到正确的数据是

        {
          "took": 1,
          "timed_out": false,
          "_shards": {
            "total": 3,
            "successful": 3,
            "skipped": 0,
            "failed": 0
          },
          "hits": {
            "total": 1,
            "max_score": 0.2876821,
            "hits": [
              {
                "_index": "phenotype",
                "_type": "phenotype",
                "_id": "685c3d59-4315-4f63-bf6a-17ad8a20aede",
                "_score": 0.2876821,
                "_source": {
                  "key1": "value1"
                }
              }
            ]
          }
        }
    
    1. 但是当我通过Java REST API执行搜索命令时,我收到零搜索命中。我就是这样做的。

      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
          .query(
                QueryBuilders.termQuery("key1", "value1")
          );
      SearchRequest searchRequest = new SearchRequest("phenotype");
      searchRequest.source(searchSourceBuilder);
      return restHighLevelClient.search(searchRequest);
      
    2. 为什么?!

1 个答案:

答案 0 :(得分:0)

我已经使用了REST客户端,但搜索量不多(还有!)。我确定您已经查看了,但here是使用搜索API的指南。

我发现有时HighLevel客户端的功能并不像我发送JSON数据那么多。如果您在使用高级客户端时遇到问题,则可以通过highlevelclient.getLowLevelClient()访问低级客户端。然后,您可以拨打performRequestAsync(method, endpoint, params, entity, responseListener)

这是一个未经测试的版本,使用您发布的原始JSON,希望它能够正常运行(或者给您一个好主意):

    public void search(String index)
    {
        String json = "{\n" +
                "  \"query\": {\n" +
                "    \"term\" : {\n" +
                "      \"key1\" : {\n" +
                "        \"value\" : \"value1\",\n" +
                "        \"boost\" : 1.0\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";

        HttpEntity entity = new NStringEntity(json, ContentType.APPLICATION_JSON);

        // I'm not sure if you'll need '/phenotype/_search' or if just 'phenotype/_search' will suffice
        _highLevelClient.getLowLevelClient().performRequestAsync("GET", "phenotype/_search", 
                                     Collections.emptyMap(), entity, new ResponseListener() {
            @Override
            public void onSuccess (Response response) {
                // Get your data from the response
            }

            @Override
            public void onFailure (Exception exception) {
                exception.printStackTrace();
            }
        });
    }