我是Elasticsearch和Spring的新手。我编写了一个Javascript POC,它将JSON字符串转换为Elasticsearch查询(并执行请求)。 它需要这样的字符串:
{
"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"
]
}
]
}
]
}
}
(注意:此查询可能具有任何嵌套级别)
...并将其转换为此:
{
"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
}
}
}
}
}
}
它首先为第一次去Elasticsearch的旅行创建一个查询(与上面类似),以提取构建此查询所需的一些信息(“链接”)。 每次访问Elasticsearch都可能返回数百万个结果,因此它使用“ search_after”机制进行分页。 我需要将此POC转换为Spring应用程序。
问题:哪种情况最适合这种情况-Spring Data Elasticsearch或Elasticsearch Java高级REST客户端? Spring数据Elasticsearch似乎在创建简单查询方面做得很好,而无需付出太多努力,但是在这种情况下对我有帮助吗? 任何建议,将不胜感激。 谢谢!
答案 0 :(得分:1)
Spring Data Elasticsearch将Elasticsearch提供的高级客户端用于非反应式实施。
您也可以将Elasticsearch的查询构建器与Spring Data Elasticsearch一起使用,这为您提供了最大的灵活性。
Spring Data Elasticsearch放在实体映射(POJO到JSON),存储库功能以及Spring Data的其他内容之上。 因此,您是否应该做一个或另一个不是问题,而是您是否需要或想要使用Spring Data Elasticsearch提供的附加功能。
编辑:
使用Spring Data Elasticsearch时,请配置使用过的RestHighLevelClient
(请参阅the documentation),然后将其注入其他Spring Bean中。因此,您甚至可以混合使用Spring Data ElasticsearchOperations
或存储库对ES的访问,以及直接使用RestHighLevelClient
进行的访问。
答案 1 :(得分:0)
我建议您使用official Java-high-level rest-client,它正在Elastic上得到积极开发,您也可以查看它支持的所有the queries builders(它为几乎所有查询提供了查询构建器)。 / p>
以前,Elasticsearch还没有JAVA的正式客户,但是现在,他们有了并积极地进行改进和开发,恕我直言,您应该继续使用它们,因为它还提供了许多现成的选择,并且他们比Elasticsearch更了解它背后的公司:)