有一个ElasticSearch索引,在该索引中潜在的命中是这样构建的:
id: number,
source: string,
type: string,
organization: {
main: [string],
support: [string]
},
title: {
main: [string],
sub: [string]
}
我的问题是我无法搜索[]中的元素。
这样做没问题:
var searchResults = client.Search<Document>(s => s
.Index(****)
.Type(****)
.MatchAll()
.Query(q =>
q.Term(p => p.source, "some source name")
))
但这不起作用:
var searchResults = client.Search<Document>(s => s
.Index(****)
.Type(****)
.MatchAll()
.Query(q =>
q.Term(p => p.organization.main[0], "some organization name")
))
我也尝试过此版本,但它也不起作用:
var searchResults = client.Search<Document>(s => s
.Index(****)
.Type(****)
.MatchAll()
.Query(q =>
q.Term(p => p.organization.main, "some organization name")
))
谁能发现问题所在?
最佳答案
您可以使用LINQ的.First()
扩展方法来引用Elasticsearch中的"organization.main"
字段
var searchResults = client.Search<Document>(s => s
.Index(****)
.Type(****)
.MatchAll()
.Query(q =>
q.Term(p => p.organization.main.First(), "some organization name")
)
);
请记住,您的查询是在这里对整个数组进行操作,而不是
organization.main
中的第一项,因为.First()
的使用可能暗含着这种情况。数组被索引为无序的多值字段。但是,它们以_source
的顺序返回。
关于c# - 使用C#Nest查询ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40306386/