我在ElasticSearch域中插入了以下文档。
{"depID:": "5656", "employeeID": "7777", "jobID": "2345"}
{"depID:": "6767", "employeeID": "9999", "jobID": "2345"}
{"depID:": "7676", "employeeID": "8888", "jobID": "2345"}
当我在depID上执行SearchRequest时,我得到0次点击。但当我在employeeID或jobID上执行它时,我会得到正确的响应。我不确定我在这里做错了什么。以下是我如何创建这些文档。
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"
}
}
}
}
}
您可以共享插入数据时创建的索引映射吗?这可能有助于理解这两种情况下的查询结果。
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html
更新:
问题在于字段名,查询中的depID中缺少:
。
您在employees
索引中使用indexDocumentRequest
方法对文档进行索引,并且在candidates
索引来使用searchDocumentRequest2
方法运行搜索请求。
由于两个索引不同,可能正因为如此,您无法获得准确的搜索结果。