将带有命名过滤器的 ElasticSearch 聚合查询转换为 NEST C#

Convert ElasticSearch Aggregation Query with Named Filters to NEST C#

我写了这个 ElasticSearch 查询来查找索引数据中术语的聚合计数。

搜索词: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
            }
          }
        }
      ]
    }
  }
}

此查询是为搜索词 'apple orange banana' 创建的。因此,存在三个命名过滤器。但是如果用户搜索 'apple orange banana grape' 应该有命名过滤器。我想使用 NEST 插入它。

下面是我根据 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" });
}

现在,问题是我正在使用 Fluent DSL 在 ElasticSearch 中执行查询,但我不知道如何在其中添加 myFilters。有人可以帮忙吗?

任何形式的帮助对我来说都是很棒的!

提前致谢。

假设 words 变量包含所需的名称和术语,这里是 NEST 查询的结果

client.Search<Sample>(search => search
    .Size(0)
    .Source(
        source => source.Excludes(
            excludes => excludes.Field(i => i.ChapterData)
        )
    ).Aggregations(aggs => aggs
        .Terms("asPerBookID", terms => terms
            .Field(i => i.BookID)
            .Aggregations(bookIDAggs => bookIDAggs
                .Nested("asPerChapterData", nested => nested
                    .Path(i => i.ChapterData)
                    .Aggregations(chapterDataAggs => chapterDataAggs
                        .Filters("asPerChapterDatadotData", filters => filters
                            .NamedFilters(named => words
                                .Aggregate(named, (_named, word) => _named
                                    .Filter(word, filter => filter
                                        .QueryString(queryString => queryString
                                            .Query(word)
                                            .DefaultField("chapterData.data")
                                        )
                                    )
                                )
                            )
                        )
                    )
                )
            )
        )
    ).Query(query => query
        .Nested(nested => nested
            .Path(i => i.ChapterData)
            .Query(nestedQuery => nestedQuery
                .Bool(boolean => boolean
                    .Should(words
                        .Select(word => (QueryContainer)new QueryStringQuery
                        {
                            Query = word,
                            DefaultField = "chapterData.data"
                        }).ToArray()
                    )
                )
            )
        )
    )
);

我还删除了根 must 条件,因为它对单个条件没有意义