elasticsearch 5.x:如何使嵌套匹配查询搜索

时间:2016-12-23 10:21:39

标签: elasticsearch nest

在以前的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次点击

enter image description here

你知道在上一版本中该怎么做吗?

1 个答案:

答案 0 :(得分:2)

data = [[-10,  0],
 [-9,   1],
 [ 9,  -5],
 [-8,   6],
 [-7,  -5],
 [-7,  -6],
 [ 0, -10]]

final = []
for outer_values in data:
    if -10 <= outer_values[0] <= 0 and  -9 <= outer_values[1] <= 1:
        final.append(outer_values)