gorjan
gorjan

Reputation: 5555

ElasticSearch in Python: Conjunction between separate terms queries

Maybe my question is a bit unclear, but I will give it a shot since I am quite inexperienced with ElasticSeach. What I have are indexed documents in the following format:

{"field1": "something", "field2": "nothing", "text_filed": "some_text"}

What I want to do, is run queries such that the field1 and field2 will be used for terms matching as a filter, and the text_field will be used as the standard query. My question is about the filter. Because my inputs are going to be in the form:

field1 = ["1", "2", "3"]
field2 = [["one", "one1"], ["two", "2two", "two2"], ...]
query = "whatever"

I want to filter all documents that have 1 in field1 and either one or one1 in field2, then, all documents that have 2 in field1 and either two, 2two or two2 as field2 and so on...

For me it is pretty clear how to do basic filtering across both field1 and field2, but I do not know how to combine them. Preferably, I would like to know how can I achieve this using Python DSL.

Looking forward to your answers!

Upvotes: 0

Views: 87

Answers (1)

Tom Slabbaert
Tom Slabbaert

Reputation: 22296

You just need to combined some boolean logic.

The request body should look like this:

{
    query: {
        bool: {
            must: [
                {
                    bool: {
                        must: [
                            {
                                term: {
                                    field1: "1"
                                }
                            },
                            {
                                fuzzy: {
                                    field2: {
                                        value: "one",
                                        fuzziness: 1
                                    }
                                }
                            }
                        ]
                    }
                },
                {
                    bool: {
                        must: [
                            {
                                term: {
                                    field1: "2"
                                }
                            },
                            {
                                fuzzy: {
                                    field2: {
                                        value: "two",
                                        fuzziness: 1
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

Notice that each boolean "cluster" has to be conditioned on one number due to elastic array type being flattened.

  • this answer is conditioned on fields being of the default types when indexing, if you have a nested field in your mapping this will not necessarily work.

Upvotes: 1

Related Questions