首页 > 解决方案 > 弹性搜索不执行搜索命令

问题描述

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

  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);我使用仅包含信息的命令将文档保存到 Elastic Search key1: value1

  4. 从 Kibana 执行命令,

    GET /phenotype/_search
    {
      "query": {
        "term" : {
          "key1" : {
            "value" : "value1",
            "boost" : 1.0
          }
        }
      }
    }
    

我看到返回了正确的数据,即

    {
      "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);
    

为什么?!

标签: javaelasticsearch

解决方案


我已经使用了一点 REST 客户端,但没有太多搜索(还没有!)。我相信您已经看过了,但这里有一个使用 Search 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();
            }
        });
    }

推荐阅读