Stormcloak
Stormcloak

Reputation: 105

ElasticSearch-NEST Query String Phrase

I have a problem with ElasticSearch query phrases. My index document is;

    var person = new Person
    {
        Id = "4",
        Firstname = "ali ahmet",
        Lastname = "yazıcı"
    };
    var index = client.Index(person, x => x.Index("personindex"));

My search phrase is;

    var result = client.Search<Person>(s => s
        .From(0)
        .Size(10)
        .Query(q => q
            .SimpleQueryString(qs => qs
                .OnFields(new[]{"firstname","lastname"})
                .Query("\"ali ah*\"")
            )
        )
  );

Result document is empty. But when i change my phrase to

.Query("\"ali ahmet\"")

result is coming. Why return empty result from

.Query("\"ali ah*\"")

this phrase.

EDIT

Person Class

public class Person
{
    public string Id { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}

Index mapping

var response = client.CreateIndex("personindex", c => c
            .AddMapping<Person>(m => m.MapFromAttributes())

Upvotes: 1

Views: 5842

Answers (2)

Rob
Rob

Reputation: 9979

From documentation for simple query string:

" wraps a number of tokens to signify a phrase for searching

When you are searching for .Query("\"ali ah*\"") actually it looks for phrase ali ah*, but * is not treated as wildcard character.

Chnage your NEST query to:

var result = client.Search<Person>(s => s
    .Explain()
    .From(0)
    .Size(10)
    .Query(q => q
        .QueryString(qs => qs
            .OnFields(new[] {"firstname", "lastname"})
            .Query("ali ah*")
        )
    ));

Hope it helps.

Upvotes: 1

Moussa Elb
Moussa Elb

Reputation: 1

var result = client.Search<Person>(s => s
.Explain()
.From(0)
.Size(10)
.Query(q => q
    .Match(qs => qs
        .OnFields(new[] {"firstname", "lastname"})
        .Query("ali ah*")
        .MinimumShouldMatch(100)
    )
));

Upvotes: 0

Related Questions