特定领域的多个分析器

an_*_*her 1 elasticsearch

我正在研究 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

       }

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Kam*_*mal 6

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

根据您的要求,您可以简单地添加两个过滤器,停止过滤器和滚雪球过滤器,然后按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"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

示例分析查询

POST <your_index_name>/_analyze
{
  "analyzer": "my_analyzer",
  "text": "This is the thing, perfection is not worth it"
}
Run Code Online (Sandbox Code Playgroud)

查询响应

{
  "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
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

解决方案2:使用多字段

但是,如果您确实坚持希望拥有多个分析器,那么您可以使用的是创建多字段并让它们都使用单独的分析器。

以下是这种情况下的映射方式。我仅将以下示例用于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"
                  }
               }
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

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

基本上使用 field titleformy_analyzer和 use title.stopwordsfor stop_analyzer

解决方案 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"
Run Code Online (Sandbox Code Playgroud)

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

POST some_index/_search
{
  "query": {
    "match": {
      "title": "perfection"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!