Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 715
kgf3JfUtW Avatar asked Feb 05 '23 03:02

kgf3JfUtW


1 Answers

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.

like image 86
eemp Avatar answered Mar 13 '23 00:03

eemp