Not receiving correct results when searching for a phrase

(Dave K) #1

Can anyone see what is incorrect about the following command?{ "query":{"bool": {"must": {"wildcard": {"DESC":"post traumatic stress disorder"}}}}}&size=100&pretty&explain

The above command returns results when submitted from a Phython program as follows:
elastic_query ='fborep',body={"size":1000,"query":{"bool":{"must":[{"wildcard":{"DESC":"post traumatic stress disorder"}}]}}})

When the browser command is run it returns size= records from the database but not any that contain "post traumatic stress disorder" in the DESC field.

Can anyone see anything incorrect with my syntax?

Dave K

(Zachary Tong) #2

Your first query request:{ "query":{"bool": {"must": {"wildcard": {"DESC":"post traumatic stress disorder"}}}}}&size=100&pretty&explain

Is not valid syntax. You cannot specify JSON-style searches through the URI. That's limited to simple queries like:

If you want to execute JSON DSL queries, you have to send it as the body of the HTTP request. From the command line using curl for example:

curl -XGET "" -d '
          "DESC":"post traumatic stress disorder"

The reason you're seeing all the results is because Elasticsearch didn't know what to do with the invalid search request, so it just executed a "Match Everything" search, which returns all your records :slight_smile:

I'd recommend starting at the beginning of the Definitive Guide and working through some of the intro chapters about search, it'll help a bunch with understanding how ES works :slight_smile:

(Dave K) #3

Hi Zachary,

Thanks for the reply.

We were using a different query as follows:{"query":{"wildcard":{"tags":"post traumatic stress disorder"}}}

and that runs correctly, giving correct results.

Isn’t this also a JSON body search? I do seem to remember reading somewhere that one could do this.
We were wanting to submit url queries directly to the browser via C# code and that has been successful with the above query.

Dave K

(Zachary Tong) #4

Yeah, that's not valid either. See URI Search for details.

At one point in the distant past, you could URLencode the JSON and put it in a "source" param, but I'm not sure if this is supported anymore. And it's bad practice, since JSON DSL queries can get quite long but URIs have a fixed length limit

e.g. this used to work at some point:


(Zachary Tong) #5

To clarify, you need to put the JSON in the HTTP request body, which means you can't execute it directly from the browser's URL bar. You'll need something like Sense, Postman extension, curl, etc which can send HTTP requests with bodies

(Dave K) #6

Thanks again Zachary,

The query I sent to you has been working from both the url directly and also from our logic that places it into the
body before submitting it. I use the url directly to debug my queries before I place them into code but it sounds like
this isn’t a very good idea from what you say regardless that it has “worked so far”. It won’t some day.

I’ll have to go back and debug from my code for now.

Dave K

(system) #7