elasticsearch,Regex,Lucene,elasticsearch" /> elasticsearch,Regex,Lucene,elasticsearch" />

Regex ElasticSearch正则表达式过滤器正则表达式破折号

Regex ElasticSearch正则表达式过滤器正则表达式破折号,regex,lucene,elasticsearch,Regex,Lucene,elasticsearch,我的ElasticSearch v1.2.1中有一些文档,如: { "tempSkipAfterSave": "false", "variation": null, "images": null, "name": "Dolce & Gabbana Short Sleeve Coat", "sku": "MD01575254-40-WHITE", "user_id": "123foo", "creation_date": null, "changed": 1

我的ElasticSearch v1.2.1中有一些文档,如:

{
  "tempSkipAfterSave": "false",
  "variation": null,
  "images": null,
  "name": "Dolce & Gabbana Short Sleeve Coat",
  "sku": "MD01575254-40-WHITE",
  "user_id": "123foo",
  "creation_date": null,
  "changed": 1
}
其中,
sku
可以是一种变体,例如:
MD01575254-40-BlUE
MD01575254-38-WHITE

我可以让我的弹性搜索查询处理以下内容:

{
  "size": 1000,
  "from": 0,
  "filter": {
    "and": [
      {
        "regexp": {
          "sku": "md01575254.*"
        }
      },
      {
        "term": {
          "user_id": "123foo"
        }
      },
      {
        "missing": {
          "field": "project_id"
        }
      }
    ]
  },
  "query": {
    "match_all": {}
  }
}    
我得到了sku的所有变体:
MD01575254*

然而,破折号“-”真的把我搞砸了

当我将regexp更改为:

"regexp": {
  "sku": "md01575254-40.*"
}
我无法得到任何结果。我也试过了

  • “sku”:“md01575254-40.*”
  • “sku”:“md01575254-40.*”
  • “sku”:“md01575254-40-.*”

只是似乎不能让它工作?我在这里没有错是什么?

问题:

这是因为默认分析器通常标记为
-
,因此您的字段最有可能保存为:

  • MD01575254
  • 40
  • 蓝色

解决方案:

您可以更新映射,使其具有索引时不会分析的
sku.raw
字段。这将要求您删除并重新编制索引

{
  "<type>" : {
    "properties" : {
      ...,
      "sku" : {
        "type": "string",
        "fields" : {
          "raw" : {"type" : "string", "index" : "not_analyzed"}
        }
      }
    }
  }
}

HTTP端点:

用于删除当前映射和数据的API为:

DELETE http://localhost:9200/<index>/<type>
删除http://localhost:9200//
使用原始SKU添加新映射的API为:

PUT http://localhost:9200/<index>/<type>/_mapping
PUThttp://localhost:9200///_mapping

链接:


这也可以通过以下查询实现。(使用字段旁边的
.keyword


使用任何JSON解析库。将
-
放入char类中,如
[-]
这实际上是从JSON解析库生成的,[-]不起作用感谢您提供的有用答案,我实际上只是在搜索elasticsearch github bugs列表,并且开始走上分析vs nonGlad的道路,我可以帮助:)我在创建方面(现在称为聚合,在1.*中)时遇到了同样的问题。该死!你救了我们!:)
PUT http://localhost:9200/<index>/<type>/_mapping
"regexp": {
"sku.keyword": "md01575254-40.*"
}