简体   繁体   中英

How to escape hyphen character in Python elasticsearch

I'm using the basic elasticsearch library in python 3.

I have a query:

query = {
        "query": {
            "bool": {
                "must": [{ "term": {"hostname": '"hal-pc"' } }]
            }
        }
    }

That I call with: page = es.search(index = index_name, body=query, search_type='scan', scroll='2m')

However I'm not getting any results. I can query on other fields so I know my query works, but when I add the search for a field with a hyphen in the value, I cannot find anything. How can I escape this character? I know that with normal ES queries you can send a message to configure your ES to respond to certain characters in certain ways, but I don't know how to do that in python.

If the field hostname is analyzed in mapping, elasticsearch does not store the field value as is. Instead, it stores "hal-pc" as two separate terms: "hal" and "pc".So, the doc might not be obtained when search for "hal-pc" using term filter.

You can search for "hal-pc" using Match query to get the necessary result. Or, by making the field hostname field not-analyzed and using term query as is.

{
    "query": {
        "match" : {
            "hostname": "hal-pc"
        }
    }
}

But, this might also return docs where hostname is just "hal" or just "pc" as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM