Convert ElasticSearch Aggregation Query with Named Filters to NEST C#


(Illict Anonymous) #1

I wrote this ElasticSearch query to find aggregation count of terms in indexed data.

SearchTerm: apple orange banana

{
  "size": 0,
  "_source": {
    "excludes": [
      "chapterData"
    ]
  },
  "aggs": {
    "asPerBookID": {
      "terms": {
        "field": "bookID",
        "size": 100000
      },
      "aggs": {
        "asPerChapterIndex": {
          "terms": {
            "field": "chapterIndex",
            "min_doc_count": 1,
            "size": 10000
          },
          "aggs": {
            "asPerChapterData": {
              "nested": {
                "path": "chapterData"
              },
              "aggs": {
                "asPerChapterDatadotData": {
                  "filters": {
                    "filters": {
                      "apple": {
                        "query_string": {
                          "query": "apple",
                          "default_field": "chapterData.data"
                        }
                      },
                      "orange": {
                        "query_string": {
                          "query": "orange",
                          "default_field": "chapterData.data"
                        }
                      },
                      "banana": {
                        "query_string": {
                          "query": "banana",
                          "default_field": "chapterData.data"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "bool": {
                "should": [
                  {
                    "query_string": {
                      "query": "apple",
                      "default_field": "chapterData.data"
                    }
                  },
                  {
                    "query_string": {
                      "query": "orange",
                      "default_field": "chapterData.data"
                    }
                  },
                  {
                    "query_string": {
                      "query": "banana",
                      "default_field": "chapterData.data"
                    }
                  }
                ],
                "minimum_number_should_match": 1
              }
            },
            "path": "chapterData",
            "inner_hits": {
              "size": 10000
            }
          }
        }
      ]
    }
  }
}

This query is created for search term 'apple orange banana'. So, there are three named filters. But if user searches for 'apple orange banana grape' there should be for named filters. I want to insert it using NEST.

Below is the code I have implemented to create named filters as per SearchTerm.

string[] words = pSearchTerm.Split(' ');
NamedFiltersContainer myFilters = new NamedFiltersContainer();
foreach (var str in words)
{
      myFilters.Add(str, new QueryStringQuery() { Query = str, DefaultField = "chapterData.data", DefaultOperator = lOperator, Analyzer = "whitespace" });
}

Now, Problem is I'm using Fluent DSL to execute query in ElasticSearch and I don't know how to add myFilters in it. Can anyone help?

Any kind of help would be great for me!

Thanks in advance.


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.