简体   繁体   English

使用C#Nest查询ElasticSearch

[英]Querying ElasticSearch with C# Nest

Have an ElasticSearch index where the potential hits are build like this: 有一个ElasticSearch索引,其中潜在的点击量是这样构建的:

id: number,
source: string,
type: string,
organization: {
    main: [string],
    support: [string]
},
title: {
    main: [string],
    sub: [string]
}

My problem is that I can't search the elements in []. 我的问题是我无法搜索[]中的元素。

No problem doing this: 这样做没问题:

var searchResults = client.Search<Document>(s => s
                .Index(****)
                .Type(****)
                .MatchAll()
                .Query(q =>
                    q.Term(p => p.source, "some source name")
                ))

But this one doesn't work: 但这不起作用:

var searchResults = client.Search<Document>(s => s
                .Index(****)
                .Type(****)
                .MatchAll()
                .Query(q =>
                    q.Term(p => p.organization.main[0], "some organization name")
                ))

I have also tried this version, but it also doesn't work: 我也尝试过此版本,但它也不起作用:

var searchResults = client.Search<Document>(s => s
                .Index(****)
                .Type(****)
                .MatchAll()
                .Query(q =>
                    q.Term(p => p.organization.main, "some organization name")
                ))

Can anyone spot what is going wrong? 谁能发现问题所在?

You can use LINQ's .First() extension method to reference the "organization.main" field in Elasticsearch 您可以使用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")
    )
 );

Bear in mind that your query is operating over the whole array here, not the first item in organization.main as the usage of .First() might imply. 请记住,您的查询是在此处对整个数组进行操作,而不是organization.main的第一项,因为这可能暗示.First()的用法。 Arrays are indexed as multi value fields which are unordered. 数组被索引为无序的多值字段。 They come back ordered in the _source however. 但是,它们在_source返回了顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM