不带实体字段的 Spring Data 弹性搜索

发布于 2025-01-10 04:14:22 字数 1075 浏览 4 评论 0 原文

我正在使用 spring data elastic search,现在我的文档没有任何静态字段,并且它是每个季度累积的数据,我将获得〜6GB/qtr(我们将其称为版本)。假设我们在 2021 年 1 月获得 5GB 的数据,有 140 列,在下一个版本中我可能会获得 130 / 120 列,我们不知道,最终用户的要求是从数据库中获取信息并将其显示在表格中格式,他可以过滤数据。在 MongoDB 中,我们有 BasicDBObject,在 springboot elasticsearch 中我们有什么

可以提供的,比方说 4-5每个版本记录中都很常见的列,除此之外,我需要检索数据而不提及 pojo 中的列名称,并且我需要对它们使用过滤器,就像在 MongoDB 中一样

List<BaseClass> getMultiSearch(@RequestBody Map<String, Object>[] attributes) {
     Query orQuery = new Query();
     Criteria orCriteria = new Criteria();
     List<Criteria> orExpression =  new ArrayList<>();
     
    for (Map<String, Object> accounts : attributes) {
       Criteria expression = new Criteria();
       accounts.forEach((key, value) -> expression.and(key).is(value));
       orExpression.add(expression);
     }
     orQuery.addCriteria(orCriteria.orOperator(orExpression.toArray(new Criteria[orExpression.size()])));
     return mongoOperations.find(orQuery, BaseClass.class);

}

I'm using spring data elastic search, Now my document do not have any static fields, and it is accumulated data per qtr, I will be getting ~6GB/qtr (we call them as versions). Lets say we get 5GB of data in Jan 2021 with 140 columns, in the next version I may get 130 / 120 columns, which we do not know, The end user requirement is to get the information from the database and show it in a tabular format, and he can filter the data. In MongoDB we have BasicDBObject, do we have anything in springboot elasticsearch

I can provide, let say 4-5 columns which are common in every version record and apart from that, I need to retrieve the data without mentioning the column names in the pojo, and I need to use filters on them just like I can do in MongoDB

List<BaseClass> getMultiSearch(@RequestBody Map<String, Object>[] attributes) {
     Query orQuery = new Query();
     Criteria orCriteria = new Criteria();
     List<Criteria> orExpression =  new ArrayList<>();
     
    for (Map<String, Object> accounts : attributes) {
       Criteria expression = new Criteria();
       accounts.forEach((key, value) -> expression.and(key).is(value));
       orExpression.add(expression);
     }
     orQuery.addCriteria(orCriteria.orOperator(orExpression.toArray(new Criteria[orExpression.size()])));
     return mongoOperations.find(orQuery, BaseClass.class);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦慕雪 2025-01-17 04:14:22

您可以像这样定义一个实体类:

public class GenericEntity extends LinkedHashMap<String, Object> {
}

要在调用站点中返回该实体类:

public SearchHits<GenericEntity> allGeneric() {
    var criteria = Criteria.where("fieldname").is("value");
    Query query = new CriteriaQuery(criteria);
    return operations.search(query, GenericEntity.class, IndexCoordinates.of("indexname"));
}

但请注意:将数据写入 Elasticsearch 时,该索引中新字段/属性的映射将动态更新。映射可以拥有的条目数量是有限制的 (https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-settings-limit.html)。因此请注意不要遇到该限制。

You can define an entity class for example like this:

public class GenericEntity extends LinkedHashMap<String, Object> {
}

To have that returned in your calling site:

public SearchHits<GenericEntity> allGeneric() {
    var criteria = Criteria.where("fieldname").is("value");
    Query query = new CriteriaQuery(criteria);
    return operations.search(query, GenericEntity.class, IndexCoordinates.of("indexname"));
}

But notice: when writing data into Elasticsearch, the mapping for new fields/properties in that index will be dynamically updated. And there is a limit as to how man entries a mapping can have (https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-settings-limit.html). So take care not to run into that limit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文