31

How do I specify AND operation in URI based query? I'm looking for something like:

http://localhost:9200/_search?q="profiletype:student AND username:s*"

4 Answers 4

27

For URI search in ElasticSearch, you can use either the AND operator profiletype:student AND username:s, as you say but without quotes:

  _search?q=profiletype:student AND username:s*

You can also set the default operator to AND

  _search?q=profiletype:student username:s*&default_operator=AND

Or you can use the + operator for terms that must be present, i.e. one would use +profiletype:student +username:s as query string. This doesn't work without URL encoding, though. In URL encoding + is %2Band space is %20, therefore the alternative would be

  _search?q=%2Bprofiletype:student%20%2Busername:s*
Sign up to request clarification or add additional context in comments.

1 Comment

if you use AND, don't forget to URL encode the spaces so, _search?q=profiletype:student%20AND%20username:s*
26

According to documentation, it should work as you described it. See http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query.html

That said, you can also use the following:

http://localhost:9200/_search?q="+profiletype:student +username:s*"

6 Comments

ES throws an exception. java.lang.IllegalArgumentException: invalid version format: +USERNAME:S HTTP/1.1
Try this: curl localhost:9200/_search?q=%2Bprofiletype%3Astudent+%2Busername%3As*
This will give you the results from both parameters combined and also from both separate parameters. What if I only want the first?
Thanks @dadoonet, You have saved my ass a**, Now can you help me with how can we use the count with group by ?
You can't run aggregations from the URI. You need to pass a JSon body (query dsl) and add aggs you want in it.
|
5

You can try bellow line.

http://localhost:9200/_search?q=profiletype:student%20AND%20username:s*

http://localhost:9200/_search?q=profiletype:student AND username:s*

Comments

3

i had to combine the default operator and + sign to make it work

curl -XGET 'localhost:9200/apm/inventory/_search?default_operator=AND&q=tenant_id:t2+_all:node_1'

1 Comment

I want to do something like - response = client.search q: tenant_id:t2&&username:s* For this what is the correct approach in this case ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.