【问题标题】:Shingles in Elasticsearch which respect punctuationElasticsearch 中尊重标点符号的带状疱疹
【发布时间】:2015-12-31 09:03:52
【问题描述】:

我正在 Elasticsearch 中为英国地址构建地址匹配引擎,我发现 shingles 非常有用,但是在标点符号方面我发现了一些问题。 “4 Walmley Close”的查询返回以下匹配项:

  1. 3 和 4 单元,Walmley Chambers,3 Walmley Close
  2. Flat 4, Walmley Court, 10 Walmley Close
  3. Co-Operative Retail Services Ltd, 4 Walmley Close

真正的匹配是 3 号,但是 1 和 2 都匹配(错误地),因为它们在变成带状疱疹时都变成了“4 walmley”。我想告诉 shingle 分析器不要生成跨越逗号的 shingles。因此,例如 1) 目前我得到:

  • 单元 3
  • 3 和
  • 和4
  • 4 沃姆利
  • 沃姆利室
  • 分庭 3
  • 3 沃姆利
  • walmley 关闭

...实际上我想要的只是....

  • 单元 3
  • 3 和
  • 和4
  • 沃姆利室
  • 3 沃姆利
  • walmley 关闭

我当前的设置如下。我已经尝试将标记器从标准转换为空白,这有助于它保留逗号并可能避免上述情况(即我最终以 '4, walmley' 作为地址 1 和 2 中的带状疱疹)但是我结束了在我的索引中有很多不可用的 shingles 并且有 7000 万个文档,我需要减小索引大小。

正如您在索引设置中看到的那样,我还有一个 street_sym 过滤器,我希望能够在我的带状疱疹中使用它,例如对于这个例子,除了生成“walmley close”之外,我还想要“walmley cl”,但是当我尝试包含这个时,我得到了“close cl”的带状疱疹,这并不是很有帮助!

非常感谢更有经验的 Elasticsearch 用户的任何建议。我已经阅读了 Gormley 和 Tong 的优秀书籍,但无法理解这个特定的问题。

提前感谢您提供的任何帮助。

"analysis": {
    "filter": {
        "shingle": {
            "type": "shingle",
            "output_unigrams": false
        },
        "street_sym": {
            "type": "synonym",
            "synonyms": [
                "st => street",
                "rd => road",
                "ave => avenue",
                "ct => court",
                "ln => lane",
                "terr => terrace",
                "cir => circle",
                "hwy => highway",
                "pkwy => parkway",
                "cl => close",
                "blvd => boulevard",
                "dr => drive",
                "ste => suite",
                "wy => way",
                "tr => trail"
            ]
        }
    },
    "analyzer": {
        "shingle": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": [
                "lowercase",
                "shingle"
            ]
        }
    }
}

【问题讨论】:

  • 即使你只得到了你想要的,“4 Walmley Close”仍然会匹配所有三个,因为它被标记为“4 walmley”和“walmley close”,后者仍然出现在所有三个。

标签: elasticsearch lucene


【解决方案1】:

请参阅我对您的问题的评论,了解为什么该解决方案仍无法阻止“4 Walmley Close”匹配您提供的所有三个匹配项。但是,至少可以获得您想要的令牌。我不确定它是否是最优雅/性能最好的解决方案,但在您的带状疱疹上使用 Pattern ReplacePattern CaptureLength 过滤器似乎可以解决问题:

"analysis": {
    "filter": {
        "shingle": {
            "type": "shingle",
            "output_unigrams": false
        },
        "street_sym": {
            "type": "synonym",
            "synonyms": [
                "st => street",
                "rd => road",
                "ave => avenue",
                "ct => court",
                "ln => lane",
                "terr => terrace",
                "cir => circle",
                "hwy => highway",
                "pkwy => parkway",
                "cl => close",
                "blvd => boulevard",
                "dr => drive",
                "ste => suite",
                "wy => way",
                "tr => trail"
            ]
        },
        "no_middle_comma": {
            "type": "pattern_replace",
            "pattern": ".+,.+",
            "replacement": "" 
        },
        "no_trailing_comma": {
            "type": "pattern_capture",
            "preserve_original": false,
            "patterns": [
                "(.*),"
            ]
        },
        "not_empty": {
            "type": "length",
            "min": 1
        }
    },
    "analyzer": {
        "test": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
                "lowercase",
                "street_sym",
                "shingle",
                "no_middle_comma",
                "no_trailing_comma",
                "not_empty"
            ]
        }
    }
}
  • no_middle_comma:将中间带逗号的任何标记替换为空标记
  • no_trailing_comma:用逗号之前的部分替换任何以逗号结尾的标记
  • not_empty:删除由上述产生的任何空标记

例如,“Units 3 And 4, Walmley Chambers, 3 Walmley Cl”变为:

{
   "tokens": [
      {
         "token": "units 3",
         "start_offset": 0,
         "end_offset": 7,
         "type": "shingle",
         "position": 0
      },
      {
         "token": "3 and",
         "start_offset": 6,
         "end_offset": 11,
         "type": "shingle",
         "position": 1
      },
      {
         "token": "and 4",
         "start_offset": 8,
         "end_offset": 14,
         "type": "shingle",
         "position": 2
      },
      {
         "token": "walmley chambers",
         "start_offset": 15,
         "end_offset": 32,
         "type": "shingle",
         "position": 4
      },
      {
         "token": "3 walmley",
         "start_offset": 33,
         "end_offset": 42,
         "type": "shingle",
         "position": 6
      },
      {
         "token": "walmley close",
         "start_offset": 35,
         "end_offset": 45,
         "type": "shingle",
         "position": 7
      }
   ]
}

请注意,您的同义词过滤器有效:“Walmley Cl”变为“walmley close”。

【讨论】:

    猜你喜欢
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 2019-01-04
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多