0

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.

1 Answer 1

1

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".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.