0

我试图让我的 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令牌过滤器。

4

2 回答 2

0

我相信这部分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

于 2019-08-03T12:55:25.873 回答
0

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

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

对于您的情况,您也可以设置索引设置映射。只需使用正确的参数,一切都应该没问题。

希望这会有所帮助:D

于 2019-08-03T00:52:03.850 回答