简体   繁体   English

ElasticSearch.net与查询的嵌套匹配不起作用

[英]ElasticSearch.net Nest Match with Query doesnt work

I am using elasticsearch.net library to query ES. 我正在使用elasticsearch.net库查询ES。 Facing issue related to finding results.Added screenshot which shows the results to my search used im query from Kibana. 面临与查找结果有关的问题。添加了屏幕快照,该屏幕快照显示了我从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. 但是,当我尝试通过添加匹配项使用以下代码时,所有人都能够成功检索10种产品。

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);

请在下面的代码中找到Kibana的屏幕截图,其中包含与我的搜索关键字相关的数据。

Change how field names are inferred from POCO property names: 更改从POCO属性名称推断字段名称的方式:

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. 现在, Infer.Field<Product>(f => f.Name)将序列化为"Name"以匹配索引映射中的字段名称。

NEST by default camelcases POCO property names , so by default will serialize to "name" . NEST默认为驼峰式POCO属性名称 ,因此默认情况下将序列化为"name"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM