11

I can do a quick URI search like

GET twitter/tweet/_search?q=user:kimchy

Can I search multiple fields this way? For example, user:kimchy AND age:23?


What I tried 1 (error):

curl -XDELETE localhost:9200/myindex/
curl localhost:9200/myindex/mytype/1 -d '{"a":1,"b":9}'
curl localhost:9200/myindex/mytype/2 -d '{"a":9,"b":9}'
curl localhost:9200/myindex/mytype/3 -d '{"a":9,"b":1}'

Say I want just the document {"a":9, "b":9}, I tried

GET localhost:9200/myindex/_search?q=a:9&b:9

but I get error

{
    error: {
        root_cause: [{
            type: "illegal_argument_exception",
            reason: "request [/myindex/_search] contains unrecognized parameter: [b:9]"
        }],
        type: "illegal_argument_exception",
        reason: "request [/myindex/_search] contains unrecognized parameter: [b:9]"
    },
    status: 400
}

What I tried 2 (works!):

GET localhost:9200/myindex/_search?q=a:9 AND b:9

The spaces are important. Alternatively, use %20.

kgf3JfUtW
  • 13,702
  • 10
  • 57
  • 80
  • [A similar question](https://stackoverflow.com/questions/40729271/how-to-search-on-multiple-fields-in-uri-search) which requires a more complicated query than mine. I just test for equality. – kgf3JfUtW Dec 01 '17 at 22:09
  • Yes, you can. Have you tried? – Val Dec 02 '17 at 05:00

1 Answers1

17

Yes, you can. Try something like this:

GET twitter/tweet/_search?q=user:kimchy%20AND%20age:23

Note that if you URI decode this, it's equivalent to:

GET twitter/tweet/_search?q=user:kimchy AND age:23

Note that when you are using this REST endpoint like this, I think you are really taking advantage of something like the query_string_query. Refer to those docs to get an idea of the extent of the query string language and features available to you.

eemp
  • 1,156
  • 9
  • 17
  • It's funny that, when I said in my question `For example, user:kimchy AND age:23` in my question, I was not literally talking about this syntax, which apparently exists and actually works! Thank you so much. – kgf3JfUtW Dec 04 '17 at 20:46
  • 1
    for me `...%20AND%20...` did not work but `+AND+` worked. [See this answer for more details](https://stackoverflow.com/a/39470764/2008111) – caramba May 08 '19 at 07:38
  • for anyone coming to this post, "AND" has to be capital – Arpit Agarwal Aug 07 '22 at 17:15