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