Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch - Python client - How to match on multiple fields?

I'm trying to match on more than one field when querying an ES server via the Python API. But can't figure out the syntax within Python:

I've tried;

res = es.search(index="pyats", doc_type="router_show", body={"query": {"match": {"name": "mark"} AND {"age": "21"}}}, size=1000)

and

res = es.search(index="pyats", doc_type="router_show", body={"query": {"match_all": {"name": "mark"} AND {"age": "21"}}}, size=1000)

and

res = es.search(index="pyats", doc_type="router_show", body={"query": {"match": {"name": "mark"}}, {"match": {"age": "21"}}}, size=1000)

Any advice would be greatly appreciated. None, seem to be working.

like image 443
M. Wombat Avatar asked Jul 01 '26 20:07

M. Wombat


1 Answers

Please make sure that age field is of the type integer or string. The keyword which will solve your problem is must.

{
    "query": {
        "constant_score" : {
            "filter" : {
                 "bool" : {
                    "must" : [
                        { "term" : { "age" : 21 } }, 
                        { "term" : { "name" : "mark" } } 
                    ]
                }
            }
        }
    }
}
like image 55
harshil9968 Avatar answered Jul 04 '26 10:07

harshil9968