简体   繁体   中英

Elastic Spring Data OR Java High Level REST Client?

I'm new to both Elasticsearch and Spring. I've written a Javascript POC that converts a JSON string into an Elasticsearch query (and performs the request). 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. Each trip to Elasticsearch may return millions of results, so it does paging using the "search_after" mechanism. I need to convert this POC to a Spring application.

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? Any suggestions are be much appreciated. Thanks!

Spring Data Elasticsearch uses the high level client provided by Elasticsearch for the non-reactive implementation.

You can use the query builders from Elasticsearch together with Spring Data Elasticsearch too, this gives you the greatest flexibility.

Spring Data Elasticsearch puts on top of that the entity mapping (POJO to JSON), repository functions and the other stuff from Spring Data. 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.

Edit:

When using Spring Data Elasticsearch, you configure the used RestHighLevelClient (see the documentation ) and then have it injected into your other Spring beans. So you can even mix access to ES using Spring Data ElasticsearchOperations or Repositories and access by using the RestHighLevelClient directly.

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 ).

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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM