java - 弹性搜索不执行搜索命令
问题描述
我正在使用 Elastic Search(和 Kibana)v 6.3.0。我无法通过RestHighLevelClient
使用 Kibana 出现的搜索结果获得搜索结果。以下是按问题重现的步骤,请让我知道您的想法。
使用我提交的传输客户端
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();
然后我提交一个名为
key1
给它字段类型的字段名称的映射更新keyword
。使用 Kibana Dev Tools 选项卡和命令GET /phenotype/_mappings
,我可以验证步骤 1 和 2 是否成功。IndexResponse indexResponse = elasticSearchRepository.save(document1);
我使用仅包含信息的命令将文档保存到 Elastic Searchkey1: value1
。从 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"
}
}
]
}
}
但是当我通过 Java REST API 执行搜索命令时,我收到零搜索命中。我就是这样做的。
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder() .query( QueryBuilders.termQuery("key1", "value1") ); SearchRequest searchRequest = new SearchRequest("phenotype"); searchRequest.source(searchSourceBuilder); return restHighLevelClient.search(searchRequest);
为什么?!
解决方案
我已经使用了一点 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();
}
});
}
推荐阅读
- node.js - 当您不知道过滤器数组的大小时,如何在 mongoDB (nodeJS) 中实现逻辑查询参数?
- php - 如何缓存类文件的内容并随后包含它们?
- java - java - 如何检查15位数字是否包含java中的星号字符?
- asp.net - 如何找出在哪个中间件 POST 到 Razor 页面失败以及原因
- set - OCaml:在 mli 文件中表达“t 集”
- javascript - 如何在 Google Maps API 中创建图例?
- raspberry-pi4 - 使用 Jailhouse for Raspberry Pi 4 B 构建 Linux 映像时,在 exec_python_func() 中执行 python 函数时出错
- typescript - 为什么 TypeScript 不能推断出这种泛型类型?
- c# - 我无法在 discord.net 中使用 .ToCharArray 将 SocketMessage.Content 转换为 CharArray
- python - 不断得到 Nonetype has no attribute head