简体   繁体   English

弹性 Spring 数据或 Java 高级 REST 客户端?

[英]Elastic Spring Data OR Java High Level REST Client?

I'm new to both Elasticsearch and Spring.我是 Elasticsearch 和 Spring 的新手。 I've written a Javascript POC that converts a JSON string into an Elasticsearch query (and performs the request).我编写了一个 Javascript POC,它将 JSON 字符串转换为 Elasticsearch 查询(并执行请求)。 It takes a string like this:它需要一个这样的字符串:

{
    "period": "years",
    "format": "xml",
    "criteria": {
        "operator": "OR",
        "operands": [
            {
                "operator": "AND",
                "operands": [
                    {
                        "operator": "exists",
                        "field": "def"
                    },
                    {
                        "operator": "includes",
                        "field": "keywords",
                        "value": [
                            "abcd"
                        ]
                    }
                ]
            },
            {
                "operator": "AND",
                "operands": [
                    {
                        "operator": "from",
                        "field": "links",
                        "value": 1
                    },
                    {
                        "operator": "includes",
                        "field": "keywords",
                        "value": [
                            "abcd",
                            "efgh"
                        ]
                    }
                ]
            }
        ]
    }
}

(Note: This query may have any levels of nesting) (注意:此查询可能有任何级别的嵌套)

... and converts it into this: ...并将其转换为:

{
    "query": {
      "constant_score": {
        "filter": {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "bool": {
                        "must": [
                          {
                            "exists": {
                              "field": "def"
                            }
                          },
                          {
                            "range": {
                              "effectiveDate": {
                                "gte": 1543982400,
                                "lt": 1575518400
                              }
                            }
                          }
                        ]
                      }
                    },
                    {
                      "bool": {
                        "must": [
                          {
                            "terms": {
                              "keywords.name": [
                                "abcd",
                                "efgh"
                              ]
                            }
                          },
                          {
                            "range": {
                              "effectiveDate": {
                                "gte": 1543982400,
                                "lt": 1575518400
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "bool": {
                        "must": {
                          "terms": {
                            "links": [
                              11048,
                              34618,
                              34658
                            ]
                          }
                        }
                      }
                    },
                    {
                      "bool": {
                        "must": [
                          {
                            "terms": {
                              "keywords.name": [
                                "abcd",
                                "efgh"
                              ]
                            }
                          },
                          {
                            "range": {
                              "effectiveDate": {
                                "gte": 1543982400,
                                "lt": 1575518400
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    },
    "size": 0,
    "aggs": {
      "by_id": {
        "composite": {
          "sources": [
            {
              "agg_on_id": {
                "terms": {
                  "field": "id"
                }
              }
            }
          ],
          "size": 10000,
          "after": {
            "agg_on_id": -1
          }
        },
        "aggs": {
          "latest_snapshot": {
            "top_hits": {
              "sort": [
                {
                  "effectiveDate": "desc"
                }
              ],
              "_source": true,
              "size": 1
            }
          }
        }
      }
    }
  }

It first creates a query (similar to above) for a first trip to Elasticsearch to extract some info ('links') needed for building this query.它首先为第一次访问 Elasticsearch 创建一个查询(类似于上面),以提取构建此查询所需的一些信息(“链接”)。 Each trip to Elasticsearch may return millions of results, so it does paging using the "search_after" mechanism.每次到 Elasticsearch 的行程可能会返回数百万个结果,因此它使用“search_after”机制进行分页。 I need to convert this POC to a Spring application.我需要将此 POC 转换为 Spring 应用程序。

Question : Which one is most appropriate for this case - Spring Data Elasticsearch or Elasticsearch Java High Level REST Client? Question : Which one is most appropriate for this case - Spring Data Elasticsearch or Elasticsearch Java High Level REST Client? Spring data elasticsearch seems to do a good job at creating simple queries without much effort, but would it help me in this case? Spring 数据 elasticsearch 似乎在创建简单查询方面做得很好,但在这种情况下对我有帮助吗? Any suggestions are be much appreciated.任何建议都将不胜感激。 Thanks!谢谢!

Spring Data Elasticsearch uses the high level client provided by Elasticsearch for the non-reactive implementation. Spring 数据 Elasticsearch 使用 Elasticsearch 提供的高级客户端进行非反应式实现。

You can use the query builders from Elasticsearch together with Spring Data Elasticsearch too, this gives you the greatest flexibility.您也可以将 Elasticsearch 中的查询构建器与 Spring 数据 Elasticsearch 一起使用,这为您提供了最大的灵活性。

Spring Data Elasticsearch puts on top of that the entity mapping (POJO to JSON), repository functions and the other stuff from Spring Data. Spring 数据 Elasticsearch 将实体映射(POJO 到 JSON)、存储库函数和 Spring 数据中的其他内容放在首位。 So it's not a question if you should do the one or the other, but if you need or want to use the additional functionality that Spring Data Elasticsearch offers.因此,您是否应该做其中一个不是问题,但如果您需要或想要使用 Spring Data Elasticsearch 提供的附加功能。

Edit:编辑:

When using Spring Data Elasticsearch, you configure the used RestHighLevelClient (see the documentation ) and then have it injected into your other Spring beans.使用 Spring 数据 Elasticsearch 时,您配置使用的RestHighLevelClient (请参阅文档),然后将其注入您的其他 Spring bean。 So you can even mix access to ES using Spring Data ElasticsearchOperations or Repositories and access by using the RestHighLevelClient directly.因此,您甚至可以混合使用 Spring Data ElasticsearchOperations或 Repositories 访问 ES,并直接使用RestHighLevelClient进行访问。

I would suggest you use the official Java-high-level rest-client which is being worked on actively at Elastic and you can also look at all the queries builders it supports(it has got query builders for almost all the queries ).我建议您使用 Elastic 正在积极开发的官方 Java 高级别的休息客户端,您还可以查看它支持的所有查询构建器(它为几乎所有查询提供了查询构建器)。

Also previously Elasticsearch didn't have an official client for JAVA but now as they have and actively improving and developing, IMHO you should go ahead with them as it also provides a lot of out of box options and who understand Elasticsearch better than the company behind it:) Also previously Elasticsearch didn't have an official client for JAVA but now as they have and actively improving and developing, IMHO you should go ahead with them as it also provides a lot of out of box options and who understand Elasticsearch better than the company behind它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 数据弹性搜索与 Java 高级 REST 客户端 - Spring Data Elastic Search vs Java High Level REST Client 弹性 - 使用 Java 高级 Rest 客户端执行字符串查询 - Elastic - Use Java High Level Rest Client To Perform a String Query 为什么在弹性搜索中引入 Java 高级 REST 客户端? - Why Java High Level REST Client got introduced in Elastic search? 使用 Java 高级 REST 客户端动态更改弹性文档 - Change elastic documents dynamically using Java High Level REST client 弹性搜索:将客户端传输到高级其余客户端 - Elastic Search: Transport Client to High Level Rest Client 如何使用 JAVA 高级 REST 客户端创建弹性搜索索引? - How can I create an elastic search index with the JAVA high level REST client? 为什么 filterQuery 在用于 JAVA 的 Elastic Search 的高级 REST 客户端中不起作用? - Why does filterQuery not work in Elastic Search's high level REST client for JAVA? 如何通过 Java 高级 rest 客户端在 Elastic Search 中使用多个字段进行搜索 - How to search using multiple fields in Elastic Search through Java high level rest client 将映射与 Elastic Search 的高级 REST JAVA 客户端异步放置 - 不推荐使用的错误 - Put mapping with Elastic Search's High level REST JAVA client asynchronously - deprecated error 如何通过 Java 高级 Rest 客户端访问安全弹性搜索 - How to hit Secure Elastic Search through Java High Level Rest Client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM