Construct search request from XContentBuilder

Versions (relevant - OpenSearch/Dashboard/Server OS/Browser):
Amazon Opensearch 2.5

Describe the issue:
I am using the java high level rest client to construct and perform a search on my opensearch index. The query is a knn query with a few postfilter filters. I have built the query using XContentBuilder. I have converted the query to a string and run it on the opensearch dashboard and it works perfectly fine. I would like to know how do I use this XContentBuilder to perform a search programmatically? Is there a way to convert it to SearchSourceBuilder? Appreciate any help. Thanks!

This example on the opensearch website is similar to how my query looks like:

  "size": 2,
  "query": {
    "knn": {
      "my_vector2": {
        "vector": [2, 3, 5, 6],
        "k": 2
  "post_filter": {
    "range": {
      "price": {
        "gte": 5,
        "lte": 10


Relevant Logs or Screenshots:

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.