【问题标题】:Elasticsearch index comma-separated values and use them as filtersElasticsearch 索引逗号分隔值并将它们用作过滤器
【发布时间】:2020-09-13 02:21:09
【问题描述】:

我目前使用以下配置将element 字段索引为"element" : "dog,cat,mouse":

ES 配置:

"settings": {
    "analysis": {
        "analyzer": {
            "search_synonyms": {
                "tokenizer": "whitespace",
                "filter": [
                    "graph_synonyms",
                    "lowercase",
                    "asciifolding"
                ],
            },
            "comma" : {
                "type" : "custom",
                "tokenizer" : "comma"
            }
        },
        "filter": {
            "graph_synonyms": {
                ...
            }
        },
        "normalizer": {
            "normalizer_1": {
                ...
            }
        },
        "tokenizer" : {
            "comma" : {
                "type" : "pattern",
                "pattern" : ","
            }
        },
    }
},

字段映射:

"mappings": {
    "properties": {
        "element": {
            "type": "keyword",
            "normalizer": "normalizer_1"
        }
        ................
    }
}

dog,cat,mouse 的值随后用作过滤器,但我想将它们分开并将每个值用作单独的过滤器。我尝试使用multi-fields 功能并进行了以下更改,但我仍然不确定我还应该做什么。

"element": {
    "type": "keyword",
    "normalizer": "normalizer_1",
    "fields": {
        "separated": {
            "type": "text",
            "analyzer": "comma"
        }
    }
},

【问题讨论】:

  • 提供了答案,根据我的理解,如果您有进一步的问题,请告诉我

标签: elasticsearch elasticsearch-plugin elasticsearch-dsl elasticsearch-mapping


【解决方案1】:

如果我理解正确,您有一个字段将值存储为dog,cat,mouse,并且您需要单独使用它们,例如dog、cat 和mouse,因为您可以简单地使用文本字段来存储它们使用default standard analyzer,在逗号, 上拆分标记。

analyze API 显示令牌

{
    "text": "dog,cat,mouse",
    "analyzer": "standard"
}

生成的令牌

{
    "tokens": [
        {
            "token": "dog",
            "start_offset": 0,
            "end_offset": 3,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "cat",
            "start_offset": 4,
            "end_offset": 7,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "mouse",
            "start_offset": 8,
            "end_offset": 13,
            "type": "<ALPHANUM>",
            "position": 2
        }
    ]
}

根据评论,添加有关如何定义 element 字段的示例,以便使用 standard 分析器,注意目前它定义为带有规范化器的 keyword,因此不使用 standard 分析器。

索引映射

PUT /你的索引/

{
  "mappings": {
    "properties": {
      "name": { 
        "element": "text"
        }
      }
    }
  }

【讨论】:

  • 我的应用程序提供了一个element 下拉列表,我可以在其中选择dog,cat,mouse 并将此值用作搜索过滤器。我希望能够选择dog、cat 或mouse 作为过滤器值。请注意,逗号不是此 element 字段的唯一分隔符。我也可以有dog&amp;cat&amp;mouse
  • @shAkur 感谢您的评论,我刚刚尝试了dog&amp;cat&amp;mouse 和标准分析器分别标记它们:)
  • element 字段是否默认使用标准分析器?我应该对字段映射进行哪些更改?感谢您的耐心等待,我对 elasticsearch 很陌生。
  • @shAkur,您需要在映射中将element 字段定义为text,将在我的答案中添加示例。
  • 我应该完全抛弃带有规范化器的keyword 类型吗?正如我所说,这目前用于过滤目的,我担心它会搞砸
猜你喜欢
  • 2020-10-20
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
相关资源
最近更新 更多