我在elasticsearch中使用动态映射将json文件加载到elasticsearch中,如下所示:
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
def extract():
f = open('tmdb.json')
if f:
return json.loads(f.read())
movieDict = extract()
def index(movieDict={}):
for id, body in movieDict.items():
es.index(index='tmdb', id=id, doc_type='movie', body=body)
index(movieDict)如何更新单个字段的映射?我有字段title,我想要添加不同的分析器。
title_settings = {"properties" : { "title": {"type" : "text", "analyzer": "english"}}}
es.indices.put_mapping(index='tmdb', body=title_settings)此操作失败。
我知道我不能更新已经存在的索引,但是什么是重新索引从json文件生成的映射的正确方法呢?我的文件有很多字段,手动创建映射/设置会非常麻烦。
我可以为查询指定分析器,如下所示:
query = {"query": {
"multi_match": {
"query": userSearch, "analyzer":"english", "fields": ['title^10', 'overview']}}} 如何为索引或字段指定它?
我还可以在关闭和打开索引后将分析器设置为
analysis = {'settings': {'analysis': {'analyzer': 'english'}}}
es.indices.close(index='tmdb')
es.indices.put_settings(index='tmdb', body=analysis)
es.indices.open(index='tmdb')复制英语分析器的精确设置并不能“激活”我的数据。
我的意思是,搜索不是以英语分析器ie处理的形式返回的。仍然有一些停顿的词。
发布于 2020-03-03 05:58:48
通过大量的谷歌搜索解决了这个问题...
代码:
def create_index(movieDict={}, mapping={}):
es.indices.create(index='test_index', body=mapping)
start = time.time()
for id, body in movieDict.items():
es.index(index='test_index', id=id, doc_type='movie', body=body)
print("--- %s seconds ---" % (time.time() - start))现在,我已经从json文件的动态映射中获得了mapping。我只是将其保存回json文件,以便于处理(编辑)。这是因为我有40多个字段要映射,手工完成会很累人。
mapping = es.indices.get_mapping(index='tmdb')
这是如何指定title密钥以使用english分析器的示例
'title': {'type': 'text', 'analyzer': 'english','fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}
https://stackoverflow.com/questions/60479644
复制相似问题