簡體   English   中英

彈性 Spring 數據或 Java 高級 REST 客戶端?

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

我是 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 應用程序。

Question : Which one is most appropriate for this case - Spring Data Elasticsearch or Elasticsearch Java High Level REST Client? Spring 數據 elasticsearch 似乎在創建簡單查詢方面做得很好,但在這種情況下對我有幫助嗎? 任何建議都將不勝感激。 謝謝!

Spring 數據 Elasticsearch 使用 Elasticsearch 提供的高級客戶端進行非反應式實現。

您也可以將 Elasticsearch 中的查詢構建器與 Spring 數據 Elasticsearch 一起使用,這為您提供了最大的靈活性。

Spring 數據 Elasticsearch 將實體映射(POJO 到 JSON)、存儲庫函數和 Spring 數據中的其他內容放在首位。 因此,您是否應該做其中一個不是問題,但如果您需要或想要使用 Spring Data Elasticsearch 提供的附加功能。

編輯:

使用 Spring 數據 Elasticsearch 時,您配置使用的RestHighLevelClient (請參閱文檔),然后將其注入您的其他 Spring bean。 因此,您甚至可以混合使用 Spring Data ElasticsearchOperations或 Repositories 訪問 ES,並直接使用RestHighLevelClient進行訪問。

我建議您使用 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它:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM