【问题标题】:ElasticSearch SearchRequest not returning must match on first element in the documentElasticSearch SearchRequest 不返回必须匹配文档中的第一个元素
【发布时间】:2021-07-04 20:07:29
【问题描述】:

我在 ElasticSearch 域中插入了以下文档。

{"depID:": "5656", "employeeID": "7777", "jobID": "2345"}
{"depID:": "6767", "employeeID": "9999", "jobID": "2345"}
{"depID:": "7676", "employeeID": "8888", "jobID": "2345"}

当我对 depID 执行 SearchRequest 时,我得到 0 次点击。但是当我在employeeIDs 或jobIDs 上执行它时,我得到了正确的响应。我不确定我在这里做错了什么。以下是我创建这些文档的方式。

public IndexResponse indexDocumentRequest(final String indexName, final String jsonDocument) throws IOException {
    IndexRequest indexRequest = new IndexRequest(indexName);
    indexRequest.source(jsonDocument, XContentType.JSON);
    return restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
}

IndexResponse indexResponse =
            esDao.indexDocumentRequest("employees", "{\"depID:\": \"5656\", \"employeeID\": \"7777\", \"jobID\": \"2345\"}");

下面是我运行 SearchRequests 的方法。

public SearchResponse searchDocumentRequest(final String indexName, final String jsonDocument) throws IOException {
        QueryBuilder queryBuilder = QueryBuilders.wrapperQuery(jsonDocument);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(queryBuilder);
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(indexName);
        searchRequest.source(searchSourceBuilder);
        return restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    }

以下是我如何调用上述 Search 方法。

 SearchResponse searchResponse = esDao.searchDocumentRequest("employees",
            "{\"bool\":{\"must\":[{\"match\":{\"depID\":\"5656\"}}]}}");
 SearchResponse searchResponse = esDao.searchDocumentRequest("employees",
            "{\"bool\":{\"must\":[{\"match\":{\"depID\":\"6767\"}}]}}");

以上调用均未返回任何命中!但是下面对其他字段的调用会返回正确的命中。

SearchResponse searchResponse = esDao.searchDocumentRequest("employees",
            "{\"bool\":{\"must\":[{\"match\":{\"employeeID\":\"7777\"}}]}}");
SearchResponse searchResponse = esDao.searchDocumentRequest("employees",
            "{\"bool\":{\"must\":[{\"match\":{\"jobID\":\"2345\"}}]}}");

我在这里做错了什么?为什么 depID 搜索不会返回任何命中?任何建议将不胜感激

更新: 我对文档的索引映射似乎如下。

{
  "properties": {
    "jobID": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      }
    },
    "depID:": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      }
    },
    "employeeID": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      }
    }
  }
}

【问题讨论】:

    标签: java elasticsearch aws-elasticsearch elasticsearch-7 resthighlevelclient


    【解决方案1】:

    您能否分享您在插入数据时创建的索引映射? 这可能有助于理解这两种情况下的查询结果。

    https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html

    更新:

    问题在于字段名称,查询中的 depID 中缺少 :

    【讨论】:

    • 当然,这就是我在获取索引映射时得到的。 { "properties": { "jobID": { "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } }, "depID:" : { "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } }, "employeeID": { "type": "text", “字段”:{“关键字”:{“ignore_above”:256,“类型”:“关键字”}}}}}
    • 我尝试使用您的文档并解决了问题。它在您要编制索引的文档中。它不是 depID,它是 depID: 最后带有一个 ':'。所以你的查询应该是SearchResponse searchResponse = esDao.searchDocumentRequest("employees", "{\"bool\":{\"must\":[{\"match\":{\"depID:\":\"5656\"}}]}}");
    • 这是一个愚蠢的错误。你能用这个更新答案吗?我会接受它作为答案
    【解决方案2】:

    您正在使用indexDocumentRequest 方法在employees 索引中对文档进行索引,并且您正在使用searchDocumentRequest2 方法在candidates 索引。

    由于两个索引不同,可能因此,您无法获得准确的搜索结果。

    【讨论】:

    • 抱歉,这是一个粘贴错误,我用正确的索引编辑了问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多