【问题标题】:Querying ElasticSearch with C# Nest使用 C# Nest 查询 ElasticSearch
【发布时间】:2016-10-30 06:35:07
【问题描述】:

有一个 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")
                ))

谁能发现哪里出了问题?

【问题讨论】:

  • 可以分享索引映射吗?

标签: c# elasticsearch nest


【解决方案1】:

您可以使用 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 中订购了回来。

【讨论】:

    猜你喜欢
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多