Louisa
Louisa

Reputation: 550

Querying ElasticSearch with C# Nest

Have an ElasticSearch index where the potential hits are build like this:

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?

Upvotes: 0

Views: 880

Answers (1)

Russ Cam
Russ Cam

Reputation: 125488

You can use LINQ's .First() extension method to reference the "organization.main" field in Elasticsearch

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. Arrays are indexed as multi value fields which are unordered. They come back ordered in the _source however.

Upvotes: 2

Related Questions