相关疑难解决方法(0)

在Elasticsearch中通过pHash距离搜索类似的图像

类似的图像搜索问题

  • 数百万张图像在Elasticsearch中进行了标记和存储.
  • 格式为"11001101 ... 11"(长度64),但可以更改(最好不要).

给定主题图像的散列"100111..10",我们希望在汉明距离为8的 Elasticsearch索引中找到所有相似的图像散列.

当然,查询可以返回距离大于8的图像,Elasticsearch或外部的脚本可以过滤结果集.但总搜索时间必须在1秒左右.

我们目前的映射

每个文档都有images包含图像哈希的嵌套字段:

{
  "images": {
    "type": "nested", 
    "properties": {
      "pHashFingerprint": {"index": "not_analysed", "type": "string"}
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我们的穷解决方案

事实: Elasticsearch模糊查询仅支持最大2的Levenshtein距离.

我们使用自定义标记生成器将64位字符串拆分为4组16位,并使用4个模糊查询进行4组搜索.

分析:

{
   "analysis": {
      "analyzer": {
         "split4_fingerprint_analyzer": {
            "type": "custom",
            "tokenizer": "split4_fingerprint_tokenizer"
         }
      },
      "tokenizer": {
         "split4_fingerprint_tokenizer": {
            "type": "pattern",
            "group": 0,
            "pattern": "([01]{16})"
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

然后新的字段映射:

"index_analyzer": "split4_fingerprint_analyzer",
Run Code Online (Sandbox Code Playgroud)

然后查询:

{
   "query": {
      "filtered": {
         "query": {
            "nested": {
               "path": "images",
               "query": {
                  "bool": …
Run Code Online (Sandbox Code Playgroud)

image hamming-distance elasticsearch phash

36
推荐指数
4
解决办法
8316
查看次数

标签 统计

elasticsearch ×1

hamming-distance ×1

image ×1

phash ×1