elasticsearch索引测量单元(带和不带空格)

yptwkmov  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(272)

所以我想要的是索引度量单位,不管数字和单位之间是否有空格。
我使用了一个模式捕获过滤器:

GET /_analyze
{
  "char_filter": [
    {
      "pattern": "(\\d+)\\s*(cm|m|in)",
      "type": "pattern_replace",
      "replacement": "$1_$2"
    }
  ],
  "text": ["10cm", "10 cm"]
}

产生了我所期望的:

{
  "tokens" : [
    {
      "token" : "10_cm",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "10_cm",
      "start_offset" : 5,
      "end_offset" : 10,
      "type" : "word",
      "position" : 101
    }
  ]
}

虽然这是可行的,但我不确定模式捕获的性能。elasticsearch文档中到处都有regex警告,我有点担心这会对性能造成太大影响。
也许有更好的方法来更一致地分析这类单位?

4zcjmb1e

4zcjmb1e1#

在elasticsearch中小心处理性能和正则表达式是正确的。
您可以在摄取时使用简单的正则表达式。但这取决于你的执行情况。
主要的风险是在索引过程的循环中引入另一个或几个其他循环。所以在大的文本字段上是非常危险的。但是它不应该在你的用例中改变。
首先,你应该标记你的正则表达式以避免循环,即使用户发送了一个错误的数据(10cm 20cm 14cm等等)。。。。如果在您的用例中可能的话,添加^regex$

GET /_analyze
{
  "char_filter": [
    {
      "pattern": "^(\\d+)\\s*(cm|m|in)$",
      "type": "pattern_replace",
      "replacement": "$1_$2"
    }
  ],
  "text": ["10cm", "10 cm", "a10cm", "10 cm3"]
}

最后两个例子将被忽略。

{
  "tokens" : [
    {
      "token" : "10_cm",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "10_cm",
      "start_offset" : 5,
      "end_offset" : 10,
      "type" : "word",
      "position" : 101
    },
    {
      "token" : "a10cm",
      "start_offset" : 11,
      "end_offset" : 16,
      "type" : "word",
      "position" : 202
    },
    {
      "token" : "10 cm3",
      "start_offset" : 17,
      "end_offset" : 23,
      "type" : "word",
      "position" : 303
    }
  ]
}

但是真正衡量集群中这种变化影响的唯一方法是测试它。您可以创建自己的基准测试,或者使用rally和定义一个要用大量数据进行测试的竞赛。

相关问题