【问题标题】:Query only those documents where image field is not empty仅查询图像字段不为空的文档
【发布时间】:2018-03-25 18:32:49
【问题描述】:

我有以下映射**(dynamic strict on the type)**

               "created": {
                  "type": "date"
               },
               "images": {
                  "properties": {
                     "checksum": {
                        "type": "text",
                        "index": false
                     },
                     "path": {
                        "type": "text",
                        "index": false
                     },
                     "url": {
                        "type": "text",
                        "index": false
                     }
                  }
               },

我想查询有图片的文档

我尝试了几种组合,但到目前为止都没有运气。

这是我最后一次尝试

POST catalog/_search
{
   "query": {
      "script": {
         "script": "doc['images'].values.length > 0"
      }
   }
}

POST catalog/_search
{
   "query": {
      "script": {
         "script": "doc['images.url'].values.length > 0"
      }
   }
}

但是这里它说字段数据对于文本字段不正确。无论如何我可以在不更改映射的情况下做到这一点。

理想情况下,这应该给我所有没有图像的记录。但这是返回所有记录

POST catalog/_search
{
   "query": {
       "bool": {
           "must_not": [
              {
                  "exists": {
                      "field": "images"
                  }
              }
           ]
       }
   }
}

这是包含图像的示例文档。

 {
            "_index": "catalog-2018-03-03",
            "_type": "product",
            "_id": "151755703145e27e4983a0bd1b70be44",
            "_score": 1,
            "_source": {
               "merchant": {
                  "link": "http://shophive.com/",
                  "name": "shophive"
               },
               "images": [],
               "updated": "2018-03-18T13:06:33.583480",
               "name": "Plantronics Savi Talk",
               "created": "2018-03-18T13:06:33.583459",
               "url": "http://www.shophive.com/plantronics-savi-talk",
               "price": {
                  "new": 24999,
                  "old": 24999,
                  "discount_percent": 0
               },
               "category": {
                  "level_1": {
                     "url": "computers/tablets/networking",
                     "name": "Computers/Tablets & Networking "
                  },
                  "level_2": {
                     "url": "tablets/ebook-readers",
                     "name": "Tablets & eBook Readers"
                  }
               }
            }
         }

更新

通过以下查询,我希望 elasticsearch 会返回缺少图像的文档

POST catalog/product/_search
{
   "query": {
      "bool": {
         "must_not": [
            {
               "exists": {
                  "field": "images"
               }
            }
         ]
      }
   }
}

但我收到的结果是我索引中的所有文档,显然每个文档都有一个图像。这是我通过上述查询获得的示例文档

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 20967,
      "max_score": 1,
      "hits": [
         {
            "_index": "catalog-2018-03-03",
            "_type": "product",
            "_id": "151755703145e27e4983a0bd1b70be44",
            "_score": 1,
            "_source": {
               "merchant": {
                  "link": "http://shophive.com/",
                  "name": "shophive"
               },
               "images": [
                  {
                     "url": "http://www.shophive.com/media/catalog/product/cache/1/small_image/165x/9df78eab33525d08d6e5fb8d27136e95/p/l/plantronics_savi_talk.jpg",
                     "path": "full/8e3587bd2b6107f0beafa9b1ba05f476539be0a8.jpg",
                     "checksum": "fa74ade23c8e80e9590d48d4e59b6b64"
                  }
               ],
               "updated": "2018-03-18T13:06:33.583480",
               "name": "Plantronics Savi Talk",
               "created": "2018-03-18T13:06:33.583459",
               "url": "http://www.shophive.com/plantronics-savi-talk",
               "price": {
                  "new": 24999,
                  "old": 24999,
                  "discount_percent": 0
               },
               "category": {
                  "level_1": {
                     "url": "computers/tablets/networking",
                     "name": "Computers/Tablets & Networking "
                  },
                  "level_2": {
                     "url": "tablets/ebook-readers",
                     "name": "Tablets & eBook Readers"
                  }
               }
            }
         }
      }
   }

【问题讨论】:

  • 发布一个没有图片的文档示例
  • @sramalingam24 我用示例文档更新了我的问题谢谢
  • @RaheelKhan 在随附的示例文档中,我看到了空图像数组,尽管您提到了其中存在图像的示例
  • @RaheelKhan 执行 must_not exists 查询后能否附加一份文档
  • @Chan15 请检查我的问题中的更新部分,我已附上查询和示例文档。谢谢

标签: elasticsearch


【解决方案1】:

您应该省略查询中的方括号,因为您只有一个子句

POST /catalog/_search
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "images"
                }
            }
        }
    }
}

这将为我返回没有图像的文档,如果您只需要那些有图像的文档

POST /catalog/_search
    {
        "query": {
                    "exists": {
                        "field": "images"
                    }
                }

    }

【讨论】:

  • 即使没有方括号它也不起作用,不确定是什么问题。正常存在查询给出零结果。但是只有少数文档缺少图像。所以至少它应该返回我剩下的图像存在的文档
  • 我假设您在 /catalog-2018-03-03/_search 而不是 /catalog/_search 上进行 POST,这只是示例
  • 能否请您发布 GET /_cat/aliases 的输出?v
猜你喜欢
  • 2020-03-13
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 2016-07-26
  • 2017-08-25
  • 2021-12-02
相关资源
最近更新 更多