【问题标题】:Multi-level nesting in elastic search弹性搜索中的多级嵌套
【发布时间】:2016-03-01 11:55:50
【问题描述】:

我有以下结构(一个非常大的弹性搜索文档的一小部分)

sample: {
   {
       "md5sum":"4002cbda13066720513d1c9d55dba809",
       "id":1,
       "sha256sum":"1c6e77ec49413bf7043af2058f147fb147c4ee741fb478872f072d063f2338c5",
       "sha1sum":"ba1e6e9a849fb4e13e92b33d023d40a0f105f908",
       "created_at":"2016-02-02T14:25:19+00:00",
       "updated_at":"2016-02-11T20:43:22+00:00",
       "file_size":188416,
       "type":{
          "name":"EXE"
       },
       "tags":[

       ],
       "sampleSources":[
          {
             "filename":"4002cbda13066720513d1c9d55dba809",
             "source":{
                "name":"default"
             }
          },
         {
             "filename":"4002cbda13066720332513d1c9d55dba809",
             "source":{
                "name":"default"
             }
          }
       ]

    }
}

我想使用的过滤器是使用弹性搜索通过 sample.sampleSources.source 中包含的“名称”来查找。

我尝试了以下查询

curl -XGET "http://localhost:9200/app/sample/_search?pretty" -d {query} 其中,{query} 是

{
   "query":{
      "nested":{
         "path":"sample.sampleSources",
         "query":{
            "nested":{
               "path":"sample.sampleSources.source",
               "query":{
                  "match":{
                     "sample.sampleSources.source.name":"default"
                  }
               }
            }
         }
      }
   }
}

但是,它没有返回任何结果。我的文档中有某些情况,其中嵌套比这更深。有人可以指导我如何制定此查询以使其适用于所有情况吗?

编辑 1 映射:

{
   "app":{
      "mappings":{
         "sample":{

               "sampleSources":{
                  "type":"nested",
                  "properties":{
                     "filename":{
                        "type":"string"
                     },
                     "source":{
                        "type":"nested",
                        "properties":{
                           "name":{
                              "type":"string"
                           }
                        }
                     }
                  }
               }

}

编辑 2 下面 Waldemar Neto 发布的解决方案适用于 match query,但不适用于 a wild-card or neither for a regexp

你能指导一下吗?我需要通配符和正则表达式查询才能为此工作。

【问题讨论】:

  • 请显示您的索引映射。

标签: elasticsearch


【解决方案1】:

我在这里尝试使用您的示例并且工作正常。 看看我的数据。 映射:

PUT /app
{
      "mappings": {
         "sample": {
            "properties": {
               "sampleSources": {
                  "type": "nested",
                  "properties": {
                     "source": {
                        "type": "nested"
                     }
                  }
               }
            }
         }

   }
}

索引数据

POST /app/sample
{
   "md5sum": "4002cbda13066720513d1c9d55dba809",
   "id": 1,
   "sha256sum": "1c6e77ec49413bf7043af2058f147fb147c4ee741fb478872f072d063f2338c5",
   "sha1sum": "ba1e6e9a849fb4e13e92b33d023d40a0f105f908",
   "created_at": "2016-02-02T14:25:19+00:00",
   "updated_at": "2016-02-11T20:43:22+00:00",
   "file_size": 188416,
   "type": {
      "name": "EXE"
   },
   "tags": [],
   "sampleSources": [
      {
         "filename": "4002cbda13066720513d1c9d55dba809",
         "source": {
            "name": "default"
         }
      },
      {
         "filename": "4002cbda13066720332513d1c9d55dba809",
         "source": {
            "name": "default"
         }
      }
   ]
}

搜索查询

GET /app/sample/_search
{
   "query": {
      "nested": {
         "path": "sampleSources.source",
         "query": {
             "match": {
                "sampleSources.source.name": "default"
             }
         }
      }
   }
} 

使用通配符的示例

GET /app/sample/_search
{
   "query": {
      "nested": {
         "path": "sampleSources.source",
         "query": {
             "wildcard": {
                "sampleSources.source.name": {
                   "value": "*aul*"
                }
             }
         }
      }
   }
}

我看到的唯一区别是路径,您不需要在嵌套路径中设置 sample (类型),只需要在内部对象中设置。 测试并给我反馈。

【讨论】:

  • 好的,这适用于“匹配”查询,但是对于通配符查询它不起作用,有什么猜测吗?更新了我上面的问题
  • 我用通配符示例编辑了我的帖子,我在这里进行了测试并且工作正常。如果您需要更多信息,请告诉我
  • 你能解释一下为什么只用通配符替换另一个查询中的match 不起作用吗?我无法理解为什么 match 会这样工作,但无法处理嵌套对象的任何术语查询。简而言之,嵌套对象的遍历是如何工作的?
猜你喜欢
  • 2012-07-15
  • 2020-07-26
  • 2021-09-01
  • 2015-09-24
  • 2011-08-30
  • 2020-10-01
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
相关资源
最近更新 更多