0

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.

2
  • Is the field hostname analyzed in elasticsearch mapping? Commented Feb 14, 2017 at 16:30
  • I don't know what that means - I don't maintain the ES. I have access to a kibana, and it appears as a text field, if that helps. Commented Feb 14, 2017 at 16:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not familiar enough with ES to understand your answer. Can you provide a sample Match query as am example? And how I would make the field not analyzed?
Thanks for the edit. If I have several hostnames like "hal-pc", "abc-pc", "foo-pc", this will only give me "hal-pc" right? and I can combine this with other bool queries by having {"query":{"bool":{stuff},"match":{stuff in your post}}} right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.