Home > Mobile >  Elasticsearch NEST - filter by multiple fields
Elasticsearch NEST - filter by multiple fields

Time:07-02

I have a query and want to do a search by multiple fields in case if no result by uuid need to do the same by parentUuid:

C# query:

return new NestedQuery
{
    Path = Infer.Field<ElasticDocument>(t => t.KeywordFields),
    Query = new BoolQuery
    {
        Filter = new List<QueryContainer>
        {
            new TermsQuery
            {
                Field = Infer.Field<ElasticDocument>(t => t.KeywordFields),
                Terms = new List<string>
                {
                    "term2"
                },
                Boost = 10
            }
        },
        Should = new List<QueryContainer>
        {
            new TermQuery
            {
                Field = Infer.Field<ElasticDocument>(t => t.KeywordFields.First().UUID),
                Value = filter.UUID
            },
            new TermQuery
            {
                Field = Infer.Field<ElasticDocument>(t => t.KeywordFields.First().ParentUUID),
                Value = filter.UUID
            },
        }
    }
};

example of documents:

"keywordFields": [
    {
        "value": "term1",
        "uuid": "bf18ee9f-7592-488d-7985-2b9fe8b878ca",
        "parentUUID": null
    },
    {
        "value": "term2",
        "uuid": "079205ed-30df-08f6-02a1-9caf093c3be0",
        "parentUUID": "103d6061-cb99-4fba-8118-2ea501e4425d"
    }
]

How to update the query to use or condition in case if no result by 'UUID' then let's do it by ParentUUID?

CodePudding user response:

Try to combine bool query:

 nested1 = new NestedQuery {
                Path = Infer.Field<ElasticDocument>(t => t.KeywordFields),
                Query = new BoolQuery {
                    Filter = new List<QueryContainer>
                    {
                        new TermsQuery {
                            Field = Infer.Field<ElasticDocument>(t => t.KeywordFields),
                            Terms = new List<string>
                            {
                                "term2"
                            },
                            Boost = 10
                        }
                    }
                }
            };
        
       nested2 = new NestedQuery {
            Path = Infer.Field<ElasticDocument>(t => t.KeywordFields),
            Query = new BoolQuery {
                Filter = new List<QueryContainer>
                {
                    new TermsQuery {
                        Field = Infer.Field<ElasticDocument>(t => t.KeywordFields),
                        Terms = new List<string>
                        {
                            "term2"
                        },
                        Boost = 10
                    }
                },
                Should = new List<QueryContainer>
                {
                    new BoolQuery {
                        MustNot = new List<QueryContainer> {
                            new TermQuery
                            {
                                Field = Infer.Field<ElasticDocument>(t => t.KeywordFields.First().UUID),
                                Value = filter.UUID
                            }
                        },
                        Must = new List<QueryContainer> {
                            new TermQuery
                            {
                                Field = Infer.Field<ElasticDocument>(t => t.KeywordFields.First().parentUUID),
                                Value = filter.UUID
                            }
                        }
                    }
                }
            }
        };

And should nested1 with nested2

note: not IDE for this edit, the nested2 query could be simplify for sure.

  • Related