0

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?

1 Answer 1

1

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.

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

2 Comments

Unfortunately no, still the same result.
Oh yes, i just got it, i edited. We need to separate into 2 nested queries.

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.