我正在使用elasticsearch.net库查询ES。面临与查找结果相关的问题。添加了屏幕快照,该屏幕快照显示了我从Kibana进行的查询所使用的搜索结果。
尝试使用以下代码时,不会检索到结果。
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)")
)
)
);
但是当我尝试通过添加匹配项来尝试以下代码时,所有人都能够成功检索10种产品。
var searchResponse = client.Search<Product>(s => s
.Index("products1")
.From(0)
.Size(10)
.Query(q => q
.MatchAll()
)
);
下面是我使用的完整代码。
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);
答案 0 :(得分:1)
更改从POCO属性名称推断字段名称的方式:
var settings = new ConnectionSettings(connectionPool)
.DefaultFieldNameInferrer(n => n)
.DefaultIndex("products1");
现在,Infer.Field<Product>(f => f.Name)
将序列化为"Name"
以匹配索引映射中的字段名称。
NEST by default camelcases POCO property names,因此默认情况下将序列化为"name"
。