【问题标题】:Elasticsearch 'failed to find analyzer' error & analyzer not shown by Settings APIElasticsearch“未能找到分析器”错误和分析器未由设置 API 显示
【发布时间】:2019-08-03 00:08:19
【问题描述】:

我正在尝试让我的 Elasticsearch 索引使用 Porter 词干算法,但是当我使用 _analyze 端点进行测试时,我的自定义分析器没有定义。

我查看了关于 SO 的 ES 文档和类似问题,但我不确定问题是什么。我尝试在创建索引时对设置使用单独的 PUT 请求,但没有效果。

这就是我创建映射的方式:

@staticmethod
    def make_mapping():
        mapping = {
            'settings': {
                'analysis':
                {
                    'analyzer': {
                        'porter_english': {
                            'type': 'custom',
                            'tokenizer': 'standard',
                            'stopwords': '_english_',
                            'filter': ['lowercase', 'porter_stem']
                            }
                        }
                    }
                },
            'mappings': {
                'properties': {
                    'published': {
                        'type': 'boolean'
                    },
                    'title': {
                        'type': 'text',
                        'analyzer': 'english'
                    },
                    'date': {
                        'type': 'date'
                    },
                    'description': {
                        'type': 'text',
                        'analyzer': {
                            'porter_english': {
                                'type': 'custom',
                                'tokenizer': 'standard',
                                'stopwords': '_english_',
                                'filter': ['lowercase', 'porter_stem']
                            }
                        }
                    },
                    'keywords': {
                        'type': 'text',
                        'analyzer': {
                            'porter_english': {
                                'type': 'custom',
                                'tokenizer': 'standard',
                                'stopwords': '_english_',
                                'filter': ['lowercase', 'porter_stem']
                            }
                        }
                    },
                    'price': {
                        'type': 'float'
                    }
                }
            }
        }
        return mapping

这是从映射创建索引的函数。

def init_elasticsearch():
    es = elasticsearch.Elasticsearch(['http://localhost:9200'])
    # idx_client = elasticsearch.client.IndicesClient(es)
    for i in searchables.included:
        index_name = camelTo_snake(i.__name__)
        index_m = i.make_mapping()
        index_uri = "{}/{}".format(current_app.config['ELASTIC_URI'], index_name)
        create_index = requests.put(index_uri, json=index_m)
        init_settings = requests.put(index_uri, json=index_m['settings'])

如果我查询设置,这就是我得到的全部内容:

>>> g = requests.get(e + '/gallery_item/_settings')
>>> g.text
'{
    "gallery_item":{
        "settings":{
            "index":{
                "creation_date":"1564789941204",
                "number_of_shards":"1",
                "number_of_replicas":"1",
                "uuid":"SgkEBN4nTxWUCeSGWMwbGw",
                "version":{"created":"7020099"},
                "provided_name":"gallery_item"
             }
         }
    }
}'

我只需要这两个字段来使用porter_stem 令牌过滤器。

【问题讨论】:

    标签: python elasticsearch elasticsearch-analyzers


    【解决方案1】:

    我认为mappings这部分是错误的

    改一下

    'description': {
                            'type': 'text',
                            'analyzer': {
                                'porter_english': {
                                    'type': 'custom',
                                    'tokenizer': 'standard',
                                    'stopwords': '_english_',
                                    'filter': ['lowercase', 'porter_stem']
                                }
                            }
                        },
    

     'description': {
                     'type': 'text',
                      'analyzer': 'porter_english'    
    
      },
    

    因为您已经在 settings 中定义了分析器。你只需要在mappings中使用它

    【讨论】:

    • 谢谢,这成功了。我发誓我最初实际上尝试过这样设置,但它没有按预期工作,但也许是因为我没有对映射和设置分别执行 PUT 请求?
    【解决方案2】:

    您是否考虑过使用 Python ES 客户端而不是请求?

    它让您可以轻松操作与集群相关的所有内容:从创建索引、设置属性、执行查询等。

    对于您的情况,您也可以Set settings to your indicesSet mappings。只需使用正确的参数,一切都会好起来的。

    希望这有帮助:D

    【讨论】:

    • 是的,谢谢,我已经使用了一点——我发现使用请求更容易且更易读,但我会看看它是否可以使用 Python ES 客户端。
    猜你喜欢
    • 1970-01-01
    • 2016-05-07
    • 2016-08-29
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 2011-07-19
    • 2020-07-06
    • 1970-01-01
    相关资源
    最近更新 更多