【发布时间】:2016-12-23 21:43:51
【问题描述】:
在之前的 Nest 版本中,我知道如何使用 Nest 进行基本的 es 匹配查询:
我创建了一个示例索引和映射
PUT /base_well
{
"mappings": {
"person": {
"properties": {
"first_name":{
"type": "string"
},
"last_name":{
"type": "string"
},
"age":{
"type": "integer"
}
}
}
}
}
POST /base_well/person
{
"first_name":"Adrien",
"last_name" : "Mopo",
"Age" : 21
}
POST /base_well/person
{
"first_name":"Polo",
"last_name" : "Apou",
"Age" : 36
}
ES 请求确实有效
POST /base_well/person/_search
{
"query":
{
"match":{
"first_name":"Adrien"
}
}
}
这个 Elasticsearch 请求给我这个答案:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "base_well",
"_type": "person",
"_id": "AVkq9PI5ybdSs0epy_Rb",
"_score": 0.2876821,
"_source": {
"first_name": "Adrien",
"last_name": "Mopo",
"Age": 21
}
}
]
}
}
不再起作用的 NEST 等效项:
public class Person
{
public string first_name {get;set;}
public string last_name { get; set; }
public int Age { get; set; }
}
//nest equivalent does not work anymore
var uri = new Uri("http://localhost:9200");
var setting = new ConnectionSettings(uri);
setting.DisableDirectStreaming(true);
setting.DefaultIndex("base_well");
var Client = new ElasticClient(setting);
var response = Client.Search<Person>(s => s.Query(p => p.Term(q => q.first_name, "Adrien")));
var tooks = response.Took;
var hits = response.Hits;
var total = response.Total;
它给了我 0 个文档结果,0 个命中
你知道在上一个版本中是怎么做的吗?
【问题讨论】:
-
如果要使用 NEST 进行匹配查询,请将
p.Term(..)替换为p.Match(..)。 -
一定要怎么写?
标签: elasticsearch nest