# Construct search request from XContentBuilder

**URL:** <https://forum.opensearch.org/t/construct-search-request-from-xcontentbuilder/14575>\
**Category:** OpenSearch Client Libraries\
**Tags:** opensearch-java\
**Created:** [June 6, 2023, 5:44pm UTC](https://forum.opensearch.org/t/construct-search-request-from-xcontentbuilder/14575 "2023-06-06T17:44:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![slvrhwk](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@slvrhwk](https://forum.opensearch.org/u/slvrhwk)\
**Post date:** [June 6, 2023, 5:44pm UTC](https://forum.opensearch.org/t/construct-search-request-from-xcontentbuilder/14575/1 "2023-06-06T17:44:04Z")

</div>

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

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

```

**Configuration** :

**Relevant Logs or Screenshots** :

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/3/33547ea01a5b12dcca2958411d3edd97ae2ea8c1.png) [@system](https://forum.opensearch.org/u/system)\
**Post date:** [August 5, 2023, 5:44pm UTC](https://forum.opensearch.org/t/construct-search-request-from-xcontentbuilder/14575/2 "2023-08-05T17:44:34Z")

</div>

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