# Custom query Parser in Search Api

**URL:** <https://discuss.elastic.co/t/custom-query-parser-in-search-api/66850>\
**Category:** Elasticsearch\
**Created:** [November 22, 2016, 11:59am UTC](https://discuss.elastic.co/t/custom-query-parser-in-search-api/66850 "2016-11-22T11:59:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![javid](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@javid](https://discuss.elastic.co/u/javid)\
**Post date:** [November 22, 2016, 11:59am UTC](https://discuss.elastic.co/t/custom-query-parser-in-search-api/66850/1 "2016-11-22T11:59:29Z")

</div>

I have a custom query parser and I can query my Index using Sense as follows:

GET myIndex/\_search  
{  
"query": {  
"myParser":{  
"query" : "blabla"  
}  
}  
}  
My question is, how can I parse the query with myParser in Search Api? How do I mention Parser in QueryBuilders?

---

<div class="post-metadata">

**Author:** ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)\
**Post date:** [November 22, 2016, 12:22pm UTC](https://discuss.elastic.co/t/custom-query-parser-in-search-api/66850/2 "2016-11-22T12:22:59Z")

</div>

> [@javid](#):
>
> My question is, how can I parse the query with myParser in Search Api? How do I mention Parser in QueryBuilders?

What exactly do you mean here? I'm guessing you want to use your "myParser" class from the Java API. What version of ES are you using?

Depending on whether you are on ES 2.x or 5.x what you need to implement is "slightly" different.

Isabel

---

<div class="post-metadata">

**Author:** ![javid](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@javid](https://discuss.elastic.co/u/javid)\
**Post date:** [November 22, 2016, 12:50pm UTC](https://discuss.elastic.co/t/custom-query-parser-in-search-api/66850/3 "2016-11-22T12:50:58Z")

</div>

@mainec Thank you for your answer.  
I'm relatively new to elasticsearch, so please bear with me 🙂

I use ES 2.x

I installed the new parser plugin to my ES, and I thought, if I can use it via Sense, then I can do the same when I use Search Api. Like, building query, and mention there somehow, that you want to parse this query with myParser.

Javid

---

<div class="post-metadata">

**Author:** ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)\
**Post date:** [November 22, 2016, 12:57pm UTC](https://discuss.elastic.co/t/custom-query-parser-in-search-api/66850/4 "2016-11-22T12:57:18Z")

</div>

> [@javid](#):
>
> new parser plugin to my ES

The one you wrote I suppose? Did you follow any documentation in particular when doing so?

> [@javid](#):
>
> I thought, if I can use it via Sense

You _thought you can use it via Sense_ or you actually tried out using it via Sense?

> [@javid](#):
>
> Search Api

Again - to be clear, you mean the Java Search API here? Or do you mean something else?

Isabel

---

<div class="post-metadata">

**Author:** ![javid](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@javid](https://discuss.elastic.co/u/javid)\
**Post date:** [November 22, 2016, 1:02pm UTC](https://discuss.elastic.co/t/custom-query-parser-in-search-api/66850/5 "2016-11-22T13:02:03Z")

</div>

@mainec The plugin is is written by my collegue. and I have the source code as well.  
I used it via Sense, and it works.  
Yes, I mean Java Search Api.  
Javid

---

<div class="post-metadata">

**Author:** ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)\
**Post date:** [November 22, 2016, 2:57pm UTC](https://discuss.elastic.co/t/custom-query-parser-in-search-api/66850/6 "2016-11-22T14:57:19Z")

</div>

I believe what you are missing is implementing a QueryBuilder. No need to actually put that into the QueryBuilders class, you can create instances for that from your application code.

The important part that this QueryBuilder will need is a toXContent method. Checkout the other QueryBuilder implementations in the Elasticsearch code base for examples.

One warning: This infrastructure changes substantially when switching to 5.0.

---

<div class="post-metadata">

**Author:** ![javid](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@javid](https://discuss.elastic.co/u/javid)\
**Post date:** [November 24, 2016, 7:17pm UTC](https://discuss.elastic.co/t/custom-query-parser-in-search-api/66850/7 "2016-11-24T19:17:33Z")

</div>

I just used setSource and just put my query in json format.

```
    SearchResponse response = client.prepareSearch("myIndex")
    .setSource("{\"query\":{\"myParser\":{\"query\":\"blabla\"}}}")
    .execute()
    .actionGet()

```

and it seems to work now.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 22, 2016, 7:18pm UTC](https://discuss.elastic.co/t/custom-query-parser-in-search-api/66850/8 "2016-12-22T19:18:01Z")

</div>

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