【问题标题】:Multiple Analyzers to a specific field多个分析器到特定字段
【发布时间】:2018-11-09 03:31:27
【问题描述】:

我正在开发 Elastic Search 6.4.2。我需要将多个分析器应用于单个字段。我希望将滚雪球和停用词分析器应用于标题和内容字段。我正在分享我的映射这是定义分析器的正确方法吗?

PUT /some-index
{
    "settings": {
        "index": {
            "number_of_shards": 5,
            "number_of_replicas": 1,
            "refresh_interval": "60s",
            "analysis" : {
              "analyzer" : {
                "my_analyzer" : {
                    "tokenizer" : "standard",
                    "filter" : ["standard", "lowercase", "my_snow"]
               },
               "stop_analyzer": {
                 "type":       "stop",
                 "stopwords":  "_english_"
               }
              } ,


              "filter" : {
                "my_snow" : {
                    "type" : "snowball",
                    "language" : "Lovins"
                }
            }
        }
        }
    },
    "mappings": {
        "doc": {
            "_source": {
                "enabled": true
            },
            "properties": {
                "content": {
                    "type": "text",
                    "index": "true",
                    "store": true,
                     "analyzer":["my_analyzer","stop_analyzer"],
                     "search_analyzer": ["my_analyzer","stop_analyzer"]
                },

                "title": {
                    "type": "text",
                    "index": "true",
                    "store": true,
                            "analyzer":["my_analyzer","stop_analyzer"],
                            "search_analyzer": ["my_analyzer","stop_analyzer"]

                },
                "url": {
                    "type": "text",
                    "index": "true",
                    "store": true

       }

            }
        }
    }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您正在寻找的东西是不可能的。 你不能在一个字段上有多个分析器。

    查看您的要求,您可以简单地添加两个过滤器,停止过滤器和雪球过滤器,然后添加它们,如Solution 1 部分所示。我还提到了另外两种方法供您参考,但我相信它们对您的用例没有多大意义。

    解决方案 1:查看您的需求,使用两个过滤器(一个用于雪球,另一个用于停用词)

    映射

    PUT <your_index_name>
    {
      "settings": {
            "analysis" : {
                "analyzer" : {
                    "my_analyzer" : {
                        "tokenizer" : "standard",
                        "filter" : ["standard", "lowercase", "my_snow", "my_stop"]
                    }
                },
                "filter" : {
                    "my_snow" : {
                        "type" : "snowball",
                        "language": "English"
    
                    },
                    "my_stop": {
                        "type":       "stop",
                        "stopwords":  "_english_"
                    }
                }
            }
        },
        "mappings": {
          "doc": {
                "_source": {
                    "enabled": true
                },
                "properties": {
                    "title": {
                        "type": "text",
                        "index": "true",
                        "store": true,
                        "analyzer": "my_analyzer"
                    }
                }
            }
        }
    }
    

    样本分析查询

    POST <your_index_name>/_analyze
    {
      "analyzer": "my_analyzer",
      "text": "This is the thing, perfection is not worth it"
    }
    

    查询响应

    {
      "tokens": [
        {
          "token": "thing",
          "start_offset": 12,
          "end_offset": 17,
          "type": "<ALPHANUM>",
          "position": 3
        },
        {
          "token": "perfect",
          "start_offset": 19,
          "end_offset": 29,
          "type": "<ALPHANUM>",
          "position": 4
        },
        {
          "token": "worth",
          "start_offset": 37,
          "end_offset": 42,
          "type": "<ALPHANUM>",
          "position": 7
        }
      ]
    }
    

    解决方案 2:使用多字段

    但是,如果您真的坚持要使用多个分析器,则可以使用创建 multi-field 并让它们都使用单独的分析器。

    以下是您在这种情况下的映射方式。我仅将以下示例用于title 字段,您可以将更改应用于其他字段。注意下面的映射仅用于演示,我建议solution 1 满足您的要求。

    PUT <your_index_name>
    {  
       "settings":{  
          //same as the one you've posted in the question. 
       },
       "mappings":{  
          "doc":{  
             "_source":{  
                "enabled":true
             },
             "properties":{  
                "title":{  
                   "type":"text",
                   "index":"true",
                   "store":true,
                   "analyzer":"my_analyzer",
                   "fields":{  
                      "withstopwords":{  
                         "type":"text",
                         "analyzer":"stop_analyzer"
                      }
                   }
                }
             }
          }
       }
    }
    

    请注意,您需要确保在查询时使用正确的字段名称。

    my_analyzer 基本上使用字段titlestop_analyzer 使用title.stopwords

    解决方案 3:多个索引,相同的别名

    为此,您最终会拥有

    some_index1:analyzer_type_1
    some_index2:analyzer_type_2
    Add alias "some_index" for both some_index1 & some_index2
    Query using this alias "some_index"
    

    然后您可以使用别名查询如下。请注意,当您使用some_index 进行查询时,最终会在内部搜索some_index1some_index2 两个索引。

    POST some_index/_search
    {
      "query": {
        "match": {
          "title": "perfection"
        }
      }
    }
    

    希望对你有帮助!

    【讨论】:

    • 完美的一个@kamal。解决方案 1 方法是您建议的更可取的方法。
    • @an__snatcher 很高兴它有帮助! ;-)
    • 无论如何我可以将自定义停用词传递给这样的过滤器。 "my_stop": { "type": "stop", "stopwords": "path_to_stopwords/my_stopwords.txt" }
    • @an_snatcher 我不确定,需要调查一下。同时,请您为此提出单独的问题。如果其他人知道答案,他可能有适当的解决方案/建议。
    • 当然@kamal 我会发帖的。
    猜你喜欢
    • 2015-09-05
    • 1970-01-01
    • 2014-09-03
    • 2013-08-28
    • 2018-01-05
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多