2

我编写了查询 Elastic Search 索引的 JAVA 代码(由 Elastic Cloud 提供服务——尽管我认为这与这个问题无关)。

如果没有查询词,该函数会按预期返回索引中的所有文档。

当我使用 Elastic 的 QueryBuilder 语法(这是 Elastic 的 Java 高级 REST 客户端的一部分)添加搜索查询时,找不到匹配项。

        RestHighLevelClient client = createHighLevelRestClient();
    int numberOfSearchHitsToReturn = 100; // defaults to 10

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.size(numberOfSearchHitsToReturn);
    sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); 
    String[] includeFields = colNames.toArray(new String[colNames.size()]);
    String[] excludeFields = new String[] {}; // just need an exclude field in order to call
                                                // fetchSource
    sourceBuilder.fetchSource(includeFields, excludeFields);
    sourceBuilder.from(offset);
    sourceBuilder.size(limitParam);

    sourceBuilder.query(QueryBuilders.termQuery("firstname", query));

    SearchRequest searchRequest = new SearchRequest("contacts_" + list_id).source(sourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    SearchHit[] hits = searchResponse.getHits().getHits();

即使 query = "RICARDO" 并且我确实在此索引中有一个文档,其 firstname 字段中的值为 "ricardo",hits 也是空的。大小写无关紧要......将查询设置为“ricardo”也不会带回任何匹配项。

为什么是这样?

这是我实现高级 REST 客户端库的问题,因为查询在 POSTMAN 中按预期工作......

GET https://elastic:hWWVNZEk<hidden>7a6620ba18623.us-east-1.aws.found.io:9243/contacts_6/_search
{
    "query": 
    {
        "term":
        {
            "firstname":
            {
                "value": "ricardo"
            }
        }
    }
}

确实回来了……

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 3.5263605,
        "hits": [
            {
                "_index": "contacts_6_twtoatx8yv",
                "_type": "_doc",
                "_id": "2098",
                "_score": 3.5263605,
                "_source": {
                    "list_id": "6",
                    "contact_id": "2098",
                    "firstname": "RICARDO",
                    "middlename": "",
                    "lastname": "SMITH"
                }
            }
        ]
    }
}
4

1 回答 1

0

我刚刚尝试了您的示例并且它有效,尽管我删除了几行代码,几个字段,例如我认为这是不必要的,应该可以帮助您调试代码问题。

首先让我向您展示 JSON 格式的索引映射、示例文档和搜索查询,然后我将展示如何使用 ES REST 高级客户端编写相同的搜索查询。

索引映射

{
    "mappings": {
        "properties": {
            "firstname": {
                "type": "text"
            },
            "lastname": {
                "type": "text"
            }
        }
    }
}

索引示例文档

{
   "firstname" : "RICARDO",
   "lastname" : "SMITH"
}

JSON 搜索查询

{
    "query": {
        "term": {
            "firstname": {
                "value": "ricardo"
            }
        }
    }
}

上述查询的 REST 高级客户端代码

        int numberOfSearchHitsToReturn = 100; // defaults to 10
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.size(numberOfSearchHitsToReturn);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        sourceBuilder.from(0);
        sourceBuilder.size(10);

        sourceBuilder.query(QueryBuilders.termQuery("firstname", "ricardo"));

        SearchRequest searchRequest = new SearchRequest("so_60628247").source(sourceBuilder);
        SearchResponse searchResponse = elasticsearchClient.search(searchRequest, RequestOptions.DEFAULT);

        // parse hit

        SearchHit[] searchHits = searchResponse.getHits().getHits();

调试器显示它返回结果

显示命中数组不为空

非常重要的注意事项:出于调试目的,您可以打印searchRequest.source.toString()将显示从您的 java 代码构建的搜索 JSON,在我的情况下,它如下所示:

 {
  "from": 0,
  "size": 10,
  "timeout": "60s",
  "query": {
    "term": {
      "firstname": {
        "value": "ricardo",
        "boost": 1.0
      }
    }
  }
}
于 2020-03-19T04:26:44.607 回答