1

I am trying to create a Query Module using Object Initialization method, which will build ES query dynamically based on the search criteria, using ElasticSearch.Net and Nest for .Net (version x6.4).

So what I am doing now is creating the building blocks using the following functions:

        public TermQuery AddTermQuery(string fieldName, string fieldValue)
        {
            return new TermQuery { Field = fieldName, Value = fieldValue };
        }

        public MultiMatchQuery AddMultiMatchQuery(string searchKeyword)
        {
            return new MultiMatchQuery
            {
                Query = searchKeyword,
                Type = TextQueryType.MostFields,
                Analyzer = "standard"
            };
        }

        public BoolQuery AddBoolQuery(\)
        {
            return new BoolQuery
            {
                Must = new QueryContainer[] { },
                MustNot = new QueryContainer[] { },
                Should = new QueryContainer[] { }
            };
        }

        public QueryContainer[] AddMustQuery()
        {
            return new QueryContainer[] { };
        }

        public NumericRangeQuery AddNumericRangeQuery(string fieldName, int greaterThanEqualTo, int lessThanEqualTo, int greaterThan = 0, int lessThan = 0)
        {
            if (greaterThan == 0 || lessThan == 0)
                return new NumericRangeQuery { Field = fieldName, GreaterThanOrEqualTo = greaterThanEqualTo, LessThanOrEqualTo = lessThanEqualTo };
            else
                return new NumericRangeQuery { Field = fieldName, GreaterThan = greaterThan, LessThan = lessThan };
        }

If I want to assemble the below-mentioned query using the above functions:

                var query = new SearchRequest
                {
                    Query = new BoolQuery
                    {
                        Must = new QueryContainer[]
                        {
                            new MultiMatchQuery
                            {
                                Query = "Political Relations: Soviet Union",
                                Analyzer = "standard",
                                Type = TextQueryType.MostFields
                            }
                        },
                        Filter = new QueryContainer[]
                        {
                            new BoolQuery
                            {
                                Must = new QueryContainer[] { new TermQuery { Field = "documentLevel", Value = 1 } },
                                MustNot = new QueryContainer[] { new TermQuery { Field = "moduleNumber", Value = 4 } },
                                Should = new QueryContainer[]
                                {
                                    new BoolQuery
                                    {
                                        Must =new QueryContainer[]
                                        {
                                            new TermQuery { Field = "coverDateStartSpecified", Value = true},
                                            new TermQuery { Field = "coverDateEndSpecified", Value = true },
                                            new BoolQuery
                                            {
                                                Should = new QueryContainer[]
                                                {
                                                    new NumericRangeQuery { Field = "coverDateStartYear",  GreaterThanOrEqualTo = 1946, LessThanOrEqualTo = 1975},
                                                    new NumericRangeQuery { Field = "coverDateEndYear",  GreaterThanOrEqualTo = 1946, LessThanOrEqualTo = 1975}
                                                }
                                            }
                                        },
                                    },
                                    new BoolQuery
                                    {
                                        Must =new QueryContainer[]
                                        {   new NumericRangeQuery { Field = "coverDateYear",  GreaterThanOrEqualTo = 1946, LessThanOrEqualTo = 1975},
                                            new BoolQuery
                                            {
                                                Should = new QueryContainer[]
                                                {
                                                    new TermQuery { Field = "coverDateStartSpecified", Value = false},
                                                    new TermQuery { Field = "coverDateEndSpecified", Value = false }
                                                }
                                            }
                                        },
                                    }
                                }
                            }
                        }
                    }
                };

But I am stuck at the assembling query part with the above-mentioned functions.

        public void DocumentQueryEs(DocumentLevel documentLevel, string doi)
        {
            var searchQuery = new SearchRequest
            {

            };
        }

How can I go about this for the assembling part? Please help, if I am going in the right direction or not or if I am doing anything wrong in this.

Thanks in Advance!!!

Avinash Prasad
  • 158
  • 1
  • 13
  • Hi All, can somebody help regarding this??? – Avinash Prasad Jan 20 '20 at 08:12
  • Which part of the composition is making you the most trouble? – Rob Jan 20 '20 at 08:25
  • So @Rob, the thing is that if you see the example query that I am trying to build dynamically, the Query's Must part might be a multi-match query, a term query or even a match query. So considering the random nature of the query, how can we initialize/create the whole query and put that in the searchRequest? The same goes for the Filter part as well. I hope that I am able to explain the issue. – Avinash Prasad Jan 20 '20 at 09:15
  • So, for that reason, I am trying to create the modular functions, so that I can assign when required, but don't know how to do that. For e.g. `Filter = new QueryContainer[] { new BoolQuery { Must = new QueryContainer[] { new TermQuery { Field = "documentLevel", Value = 1 } }` – Avinash Prasad Jan 20 '20 at 09:16
  • So you want to use your function when composing this filter? `Filter = new QueryContainer[] { new BoolQuery { Must = new QueryContainer[] { AddTermQuery(..) }`? – Rob Jan 20 '20 at 09:33
  • Yes, @Rob, I want to do that dynamically. If I am using the right approach or not, please let me know also. – Avinash Prasad Jan 20 '20 at 09:39
  • Looks good to me, you should also read a [great explanation](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/bool-queries.html) of what options do you have when composing bool queries with NEST. – Rob Jan 20 '20 at 09:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206271/discussion-between-avinash-prasad-and-rob). – Avinash Prasad Jan 20 '20 at 09:49

0 Answers0