gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:46:11 25 4
gpt4 key购买 nike

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

搜索词:苹果橙色香蕉

{
"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
}
}
}
]
}
}
}

该查询是针对搜索词“苹果橙香蕉”创建的。因此,有三个命名的过滤器。但是,如果用户搜索“苹果橙香蕉葡萄”,则应使用命名过滤器。我想使用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条件,因为它没有针对单个条件

关于c# - 将具有命名过滤器的ElasticSearch聚合查询转换为NEST C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43820878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com