c# - 使用C#Nest查询ElasticSearch

标签 c# elasticsearch nest

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

相关文章:

c# - 没有 File.ReadAllText 有什么办法吗?

c# - 如何使用 c# 从 excel 文件中仅获取事件工作表名称?

python - 仅当doc不存在时,Elasticsearch中的批量索引

c# - 嵌套查询等效于以下 ElasticSearch 查询

c# - 如何将单个字符串分解为字符串数组?

c# - monotuch UICollectionView 给出空引用错误

php - 如何为ElasticSearch聚合添加条件(最小/最大)?

indexing - 如何配置logstash来创建elasticsearch索引?

c# - 如何在 Elasticsearch NEST中对字段的重要性进行评分?

elasticsearch - 使用ElasticSearch存储数据而无需编制索引或进行分析(NEST客户端)