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

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 {

    @Autowired
    private 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 {

    @Autowired
    private 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.kler.cn/a/5491.html

相关文章:

  • CNStack 网络插件:hybridnet 的设计与实现
  • 程序员的天花板到底有多高?
  • 【数据分析之道-基础知识(八)】循环语句
  • Redis:redis通用命令;redis常见数据结构;redis客户端;redis的序列化
  • 209. 长度最小的子数组
  • 现在大专生转IT可行吗?
  • 枚举的使用
  • 数据结构详解
  • 一文读懂Can总线
  • 容器技术Docker
  • 手写vuex4源码(四)模块的状态的实现
  • Pyspark_结构化流2
  • 数据的存储--->【大小端字节序】(Big Endian)(Little Endian)
  • Python —— 给女儿写个雷霆战机
  • 命令行上的数据科学第二版:十、多语言数据科学
  • Leetcode.1849 将字符串拆分为递减的连续值
  • 第二十八章 变换坐标总结
  • C++模板基础(四)
  • 有了Bug,先看看类型
  • Activation Function激活函数