【问题标题】:elastic search 5 - how to query Object datatype and nested array of jsonelastic search 5 - 如何查询对象数据类型和json的嵌套数组
【发布时间】:2017-12-28 02:50:37
【问题描述】:

我想查询已经加载到 Elasticsearch 5 中的嵌套数据,但每个查询都没有返回任何内容。数据为object datatypenested array of json

这是嵌套数据类型,即 json 的 team_members 数组:

[{
"id": 6,
"name": "mike",
"priority": 1
}, {
"id": 7,
"name": "james",
"priority": 2
}]

此对象数据类型即availability_slot json:

{
"monday": {
    "on": true,
    "end_time": "15",
    "start_time": "9",
    "end_time_unit": "pm",
    "start_time_unit": "am",
    "events_starts_every": 10
}
}

这是我的弹性搜索映射:

{
"meetings_development_20170716013030509": {
    "mappings": {
        "meeting": {
            "properties": {
                "account": {"type": "integer"},
                "availability_slot": {
                    "properties": {
                        "monday": {
                            "properties": {
                                "end_time": {"type": "text"},
                                "end_time_unit": {"type": "text"},
                                "events_starts_every": {
                                      "type":"integer"
                                },
                                "on": {"type": "boolean"},
                                "start_time": {"type": "text"},
                                "start_time_unit": {
                                   "type": "text"
                                }
                            }
                        }
                    }
                },
                "team_members": {
                    "type": "nested",
                    "properties": {
                        "id": {"type": "integer"},
                        "name": {"type": "text"},
                        "priority": {"type": "integer"}
                    }
                }
            }
        }
    }
 }
}

我有两个因不同原因而失败的查询:

查询 1 尽管弹性搜索中存在记录,但此查询返回的计数为零,我发现查询由于过滤器而失败:

curl -u elastic:changeme http://172.19.0.4:9200/meetings_development/_search?pretty -d '{"query":{"nested":{"path":"team_members","score_mode":"avg","query":{"bool":{"must":[{"match":{"team_members.name":"mike"}},{"match":{"team_members.priority":1}}],"filter":[{"match":{"account":1}}]}}}}}'

这将返回零结果:

{
"took" : 8,
"timed_out" : false,
"_shards" : {
  "total" : 5,
  "successful" : 5,
  "failed" : 0
},
"hits" : {
  "total" : 0,
  "max_score" : null,
  "hits" : [ ]
 }
}

不带过滤器的查询 1 上面没有过滤器的相同查询有效:

curl -u elastic:changeme http://172.19.0.4:9200/meetings_development/_search?pretty -d '{"query":{"nested":{"path":"team_members","score_mode":"avg","query":{"bool":{"must":[{"match":{"team_members.name":"mike"}},{"match":{"team_members.priority":1}}]}}}}}'

上面的查询返回 3 个命中:

{
"took" : 312,
"timed_out" : false,
"_shards" : {
  "total" : 5,
  "successful" : 5,
  "failed" : 0
},
"hits" : {
  "total" : 3,
  "max_score" : 2.1451323,
  "hits" : [{**results available here**} ]
 }
}

对象数据类型的查询 2

curl -u elastic:changeme http://172.19.0.4:9200/meetings_development/_search?pretty -d '{"query":{"bool":{"must":{"match":{"availability_slot.start_time":1}},"filter":[{"match":{"account":1}}]}}}'

查询返回零命中,但数据在 elasticsearch 中:

{
"took" : 172,
"timed_out" : false,
"_shards" : {
  "total" : 5,
  "successful" : 5,
  "failed" : 0
},
"hits" : {
  "total" : 0,
  "max_score" : null,
  "hits" : [ ]
 }
}

如何让两个查询都按帐户进行过滤。谢谢

【问题讨论】:

    标签: elasticsearch elasticsearch-5 elasticsearch-query elasticsearch-nested


    【解决方案1】:

    elasticsearch guide link 非常有助于提出正确的弹性搜索查询,如下所示:

    查询 1 以获取 json 的嵌套数组

     {
      "query" => {
    
     "bool": {
      "must": [
       {
        "match": {
          "name": "sales call"
        }
       },
       {"nested" => {
      "path" => "team_members",
      "score_mode" => "avg",
      "query" => {
        "bool" => {
          "must" => {
            "match" => {"team_members.name" => "mike"} 
          }
        }
       }
      }
     }
     ],
      "filter": {
        "term": {
          "account": 1
        }
       }
      },
    
    
     }
    }
    

    只需像这样将查询传递给弹性搜索:

    curl http://172.19.0.4:9200/meetings_development/_search?pretty -d '{"query":{"bool":{"must":[{"match":{"name":"sales call"}},{"nested":{"path":"team_members","score_mode":"avg","query":{"bool":{"must":{"match":{"team_members.name":"mike"}}}}}}],"filter":{"term":{"account":1}}}}}'
    

    对象数据类型即 json 的查询 2 的正确语法

     {
       "query": {
         "bool": {
           "must": { 
              "match": {'availability_slot.monday.start_time' => '9'} 
           },
         "filter": [{
              "match":  {'account': 1}
         }]
       }
     }
    }
    

    您可以像这样将其传递给 elasticsearch:

    curl http://172.19.0.4:9200/meetings_development/_search?pretty -d '{"query":{"bool":{"must":{"match":{"availability_slot.monday.start_time":"9"}},"filter":[{"match":{"account":1}}]}}}'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      相关资源
      最近更新 更多