当前位置: 首页 > news >正文

ElasticSearch序列 - SpringBoot整合ES:根据指定的 ids 查询

文章目录

      • 1. ElasticSearch 根据 ids 查询文档
      • 2. SpringBoot整合ES实现 ids 查询

1. ElasticSearch 根据 ids 查询文档

① 索引文档,构造数据

PUT /my_index/_doc/1
{"price":10
}PUT /my_index/_doc/2
{"price":20
}PUT /my_index/_doc/3
{"price":30
}

② 查询文档 id 为 1 或者 2 的文档:

GET /my_index/_search
{"query": {"ids": {"values": [1,2]}}
}
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 2,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "my_index","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"price" : 10}},{"_index" : "my_index","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"price" : 20}}]}
}

我们索引文档时,文档的id为整型,为什么查询出来的文档 id为字符串类型呢?如果我们使用字符串类型的文档id查询呢?

GET /my_index/_search
{"query": {"ids": {"values": ["1","2"]}}
}
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 2,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "my_index","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"price" : 10}},{"_index" : "my_index","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"price" : 20}}]}
}

可以看到仍然可以查询到匹配的文档。

在Elasticsearch中,文档ID可以是任何字符串类型,包括数字、字母、符号等。即使您在索引文档时使用了整数作为ID,Elasticsearch也会将其转换为字符串类型,并将其存储在内部索引中。当您查询文档时,Elasticsearch会将文档ID作为字符串类型返回。

如果您需要将查询结果中的文档ID转换为整数类型,可以在查询结果中进行转换。例如,在使用Java API执行查询时,您可以使用以下代码将文档ID转换为整数类型:

SearchResponse response = client.prepareSearch("my_index").setQuery(QueryBuilders.matchAllQuery()).get();for (SearchHit hit : response.getHits().getHits()) {int id = Integer.parseInt(hit.getId());// do something with the integer ID
}

在上面的代码中,我们首先执行一个查询,并使用getHits方法获取查询结果。然后,我们遍历查询结果中的每个文档,并使用Integer.parseInt方法将文档ID转换为整数类型。

2. SpringBoot整合ES实现 ids 查询

GET /my_index/_search
{"query": {"ids": {"values": [1,2]}}
}
@Slf4j
@Service
public class ElasticSearchImpl {@Autowiredprivate RestHighLevelClient restHighLevelClient;public void searchUser() throws IOException {SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();// ids 查询IdsQueryBuilder idsQueryBuilder = new IdsQueryBuilder();// IdsQueryBuilder addIds(String... ids) idsQueryBuilder.addIds("1", "2");searchSourceBuilder.query(idsQueryBuilder);SearchRequest searchRequest = new SearchRequest(new String[]{"my_index"},searchSourceBuilder);SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);System.out.println(searchResponse);}
}

如果我们想根据 ids 去查询文档的话,一般会使用 terms 查询:

GET /my_index/_search
{"query": {"terms": {"_id": ["1","2"]}}
}

需要注意的是主键为_id,而不是id,如果使用id则查询结果会为空。

@Slf4j
@Service
public class ElasticSearchImpl {@Autowiredprivate RestHighLevelClient restHighLevelClient;public void searchUser() throws IOException {SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();// terms 查询List<String> ids = Arrays.asList("1","2");TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder("_id",ids);searchSourceBuilder.query(termsQueryBuilder);SearchRequest searchRequest = new SearchRequest(new String[]{"my_index"},searchSourceBuilder);SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);System.out.println(searchResponse);}
}
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 2,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "my_index","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"price" : 10}},{"_index" : "my_index","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"price" : 20}}]}
}
http://www.lryc.cn/news/44902.html

相关文章:

  • Spark SQL实战(08)-整合Hive
  • 堆(数据结构系列11)
  • 算法训练第四十二天|01背包问题 二维 、01背包问题 一维、416. 分割等和子集
  • Java-如何使用Java将图片和文字拼接在一起(并非是给图片加水印)
  • Metasploit入门到高级【第三章】
  • 枚举的使用
  • Python进阶语法
  • Pyspark_结构化流4
  • Linux cmp 命令
  • Python入门到高级【第五章】
  • C语言中(i++)+ (i++)真的每次都等于3吗?
  • Cursor,程序员的 AI 代码编辑助手
  • 基于XML的自动装配~
  • 完全二叉树的4种遍历方式
  • 【vue2】使用elementUI进行表单验证实操(附源码)
  • JUC之阻塞队列解读(BlockingQueue)
  • LCHub:ChatGPT4和低代码来临,程序员面临下岗?
  • 【Node.js】Express框架的基本使用
  • 使用docker 和 kubnernetes 部署单节点/多节点 kafka 环境
  • Linux使用:环境变量指南和CPU和GPU利用情况查看
  • 深入浅出 SSL/CA 证书及其相关证书文件(pem、crt、cer、key、csr)
  • Compose(1/N) - 概念 基本使用
  • 2023高质量Java面试题集锦:高级Java工程师面试八股汇总
  • MySQL多表查询 子查询效率(DQL语句)
  • Linux中 ps命令详解
  • 【Python语言基础】——Python 关键字
  • Java SE 基础(8)关键字和保留字
  • Thinkphp 6.0响应输出和重定向
  • Centos html 中文 显示为乱码
  • Helm学习笔记