kgf3JfUtW
kgf3JfUtW

Reputation: 14918

Elasticsearch URI Search multiple fields

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.

Upvotes: 11

Views: 7925

Answers (1)

eemp
eemp

Reputation: 1166

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.

Upvotes: 17

Related Questions