kranthi kumar
kranthi kumar

Reputation: 191

ElasticSearch.net Nest Match with Query doesnt work

I am using elasticsearch.net library to query ES. Facing issue related to finding results.Added screenshot which shows the results to my search used im query from Kibana.

When tried with below code No results are retrieved.

var searchResponse = client.Search<Product>(s => s
.Index("products1")
.From(0)
.Size(10)
.Query(q => q
.Match(m => m
.Field(f => f.Name)
.Query("Duracarb 32 oz (907.2 g)")
)
)
);

But when I tried with below code by adding match all able to retrieve 10 products successfully.

var searchResponse = client.Search<Product>(s => s
.Index("products1")
.From(0)
.Size(10)
.Query(q => q
.MatchAll()
)
);

Below is the full code I have used.

var uris = new[]
{
    new Uri(_searchSettings.EnableElasticURL),
    //new Uri("http://localhost:9201"),
    //new Uri("http://localhost:9202"),
};

var connectionPool = new SniffingConnectionPool(uris);
var settings = new ConnectionSettings(connectionPool)
    .DefaultIndex("products1");

var client = new ElasticClient(settings);
var searchRequest = new SearchRequest<Product>(Nest.Indices.All, Types.All)
{
    From = 0,
    Size = 10,
    Query = new MatchQuery
    {
        Field = Infer.Field<Product>(f => f.Name),
        Query = "Duracarb 32 oz(907.2 g)"
    }
};
var searchResponse = client.Search<Product>(s => s
.Index("products1")
            .From(0)
            .Size(10)
            .Query(q => q
     .Match(m => m.Field(f => f.Name).Query("Duracarb 32 oz (907.2 g)")))
    );
var stream = new System.IO.MemoryStream();
client.SourceSerializer.Serialize(searchResponse, stream);
var jsonQuery = System.Text.Encoding.UTF8.GetString(stream.ToArray());
var query = searchResponse.Documents;
var requestttt=searchResponse.DebugInformation;
//var query = GetProductQuery(searchQuery, null);
_logger.Debug("Search Response: "+query);

Please find the screenshot below of Kibana which has data related to my search keyword in code above.

Upvotes: 0

Views: 486

Answers (1)

Russ Cam
Russ Cam

Reputation: 125538

Change how field names are inferred from POCO property names:

var settings = new ConnectionSettings(connectionPool)
    .DefaultFieldNameInferrer(n => n)
    .DefaultIndex("products1");

Now, Infer.Field<Product>(f => f.Name) will serialize to "Name" to match the field name in the index mapping.

NEST by default camelcases POCO property names, so by default will serialize to "name".

Upvotes: 1

Related Questions