Not receiving correct results when searching for a phrase


(Dave K) #1

Can anyone see what is incorrect about the following command?

http://192.168.1.162:9200/fborep/_search?{ "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 = es.search(index='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?

Thanks,
Dave K


(Zachary Tong) #2

Your first query request:

http://192.168.1.162:9200/fborep/_search?{ "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:

http://192.168.1.162:9200/fborep/_search?q=foo&size=100&pretty&explain

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 "http://192.168.1.162:9200/fborep/_search?size=100&pretty&explain" -d '
{
  "query":{
    "bool":{
      "must":{
        "wildcard":{
          "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:

http://192.168.1.162:9200/fbo_update/_search?pretty&size=200&{"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.

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

ttp://localhost:9200/app/users/_search?source=%7b%22query%22%3a+%7b%22term%22%3a+%7b%22email%22%3a%22foo%40gmail.com%22%7d%7d%7d

(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.

Thanks
Dave K


(system) #7