gpt4 book ai didi

c# - 使用C#Nest查询ElasticSearch

转载 作者:行者123 更新时间:2023-12-03 00:59:05 25 4
gpt4 key购买 nike

有一个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/

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