Custom query Parser in Search Api

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?

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

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

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

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

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

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

Isabel

@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

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.

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.

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