简体   繁体   中英

Construct QueryBuilder from JSON DSL when using Java API in ElasticSearch?

I have an Elastic Search Query in JSON format (mentioned below) that I'm reading from a database and I will have to update this query in a Java spring program. How do I convert the JSON query object to a QueryBuilder object so that I can manipulate it before executing it to get the hits?

{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "after.values": {
              "query": "Beach Soccer",
              "operator": "and"
            }
          }
        },
        {
          "match": {
            "after.values": {
              "query": "Football",
              "operator": "and"
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "after.values": "Australian Open"
          }
        },
        {
          "match": {
            "after.values": "Golf Austra"
          }
        },
        {
          "match": {
            "after.values": "Australasia"
          }
        }
      ]
    }
  }
}

Same problem as How to construct QueryBuilder from JSON DSL when using Java API in ElasticSearch?

But the solution selected as answer there no longer works. I have the exact same problem as @armstrongya in 2021 and doing QueryBuilders.wrapperQuery(jsonQueryString); is not cutting it for me.

This is what I get:

{ "wrapper": { "query": "eyJib29sIjp7Im11c3Rfbm90IjpbeyJtYXRjaCI6eyJhZnRlci50aXRsZSI6eyJxdWVyeSI6IldpbWJlbGRvbiIsIm9wZXJhdG9yIjoiYW5kIn19fSx7Im1hdGNoIjp7ImZpZWxkLmRlc2NyaXB0aW9uIjp7InF1ZXJ5IjoiV2ltYmVsZG9uIiwib3BlcmF0b3IiOiJhbmQifX19LHsibWF0Y2giOnsiYWZ0ZXIudGl0bGUiOnsicXVlcnkiOiJXVEEiLCJvcGVyYXRvciI6ImFuZCJ9fX0seyJtYXRjaCI6eyJmaWVsZC5kZXNjcmlwdGlvbiI6eyJxdWVyeSI6IldUQSIsIm9wZXJhdG9yIjoiYW5kIn19fV19fQ==" }

I answered my own question: This helped: Elasticsearch Jest client add condition to json query

Mainly, I just realized that I was having the proper query I needed all along, it was just being decoded in base64 encoding. (eyJib29sIjp7Im11c3Rfbm90IjpbeyJtYXRjaCI6eyJhZnRlci50aXRsZSI6eyJxdWVyeSI6IldpbWJlbGRvbiIsIm9wZXJhdG9yIjoiYW5kIn19fSx7Im1hdGNoIjp7ImZpZWxkLmRlc2NyaXB0aW9uIjp7InF1ZXJ5IjoiV2ltYmVsZG9uIiwib3BlcmF0b3IiOiJhbmQifX19LHsibWF0Y2giOnsiYWZ0ZXIudGl0bGUiOnsicXVlcnkiOiJXVEEiLCJvcGVyYXRvciI6ImFuZCJ9fX0seyJtYXRjaCI6eyJmaWVsZC5kZXNjcmlwdGlvbiI6eyJxdWVyeSI6IldUQSIsIm9wZXJhdG9yIjoiYW5kIn19fV19fQ==)

I used an online base64 decoder to reveal the "pretty" decoded version of the query.

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