Asher
Asher

Reputation: 53

elasticsearch NEST wildcard field

Is there a way to have NEST support wildcard fields. For example.

I have a class like this:

public class SearchDocument
    {
        public string Id { get; set; }
        public string Symbol { get; set; }
        public IList<DisorderData> Disorders { get; set; }
     }

    public class DisorderData
    {
        public string Name { get; set; }
        public string Category { get; set; }
    }

I could easily structure a json query like this: (taken from: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html)

{
    "query_string" : {
        "fields" : ["disorders.*"],
        "query" : "cancer"
    }
}

If I do following in NEST:

            client.Search<SearchDocument>(x => x
                .Query(q => q
                    .QueryString(qs => qs
                        .OnFields(document => document.Disorders)
                        .Query("cancer"))));

it resolves to:

{
   "query_string" : {
       "fields" : ["disorders"],
    "query" : "cancer"
    }
}

Any thoughts on how to get the wildcard in the field?

Maybe there is a class that can resolve the property name to a string and then I can concatenate the asterik.

Upvotes: 1

Views: 2761

Answers (1)

Greg Marzouka
Greg Marzouka

Reputation: 3325

You can use the Suffix extension which will append a . and a given string (in this case *) to the end of the field:

client.Search<SearchDocument>(x => x
    .Query(q => q
        .QueryString(qs => qs
            .OnFields(document => document.Disorders.Suffix("*"))
            .Query("cancer"))));

Also, keep in mind that it's always possible to just pass the field name as a string instead of an object path:

client.Search<SearchDocument>(x => x
    .Query(q => q
        .QueryString(qs => qs
            .OnFields(new string[] { "disorders.*" })
            .Query("cancer"))));

Upvotes: 2

Related Questions