0

I am using the latest version of Elastic Search (7.9) and i'm trying to find a good way to do a multiple term query as an AND.

Essentially what i want do is:

select * where field1 === 'word' AND field2 === 'different word'

I am currently using term to do an exact keyword match on field1. But adding in the second field is causing me some jip.

{
    "query": {
        "term": {
            "field1": {
                "value": "word"
            }
        }
    }
}

This is my current query, i have tried using BOOL. I came across answers in previous versions where i could maybe use a filtered query. But i cant seem to get that to work either.

Can someone help me please. It's really doing my nut.

EDIT

Here is what i've tried with a bool / must with multiple terms. But i get no results even though i know in this case this query should return data

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "field1": {
                            "value": "word"
                        }
                    }
                },
                {
                    "term": {
                        "field2": {
                            "value": "other word"
                        }
                    }
                }
            ]
        }
    }
}
Andy
  • 679
  • 2
  • 10
  • 25

1 Answers1

0

Ok, so, fun fact. You can do the bool / match as i have tried. What you should remember is that keyword matches (which is what i'm using) are case sensitive. ES doesnt do any analyzing or filtering of the data when you set the type of a field to keyword.

It's also possible to use a bool with a filter to get the same results (when you factor in a type)

{
    "query": {
        "bool": {
            "must": 
                {
                    "term": {
                        "field1": {
                            "value": "word"
                        }
                    }
                },
            "filter":
                {
                    "term": {
                        "field2": {
                            "value": "other word"
                        }
                    }
                }
        }
    }
}
Andy
  • 679
  • 2
  • 10
  • 25
  • A keyword is a hash. It's case sensitive by default, unless you use it in some custom analyzer. It's your use case, the query is strickly = to select * where field1 === 'word' AND field2 === 'different word' which is also case sentitive. – Jaycreation Aug 24 '20 at 12:15