悟空,拿我的打狗棒来

请问如何在attachment类型中使用ik分词器?

Elasticsearch | 作者 ouyangDD | 发布于2017年07月11日 | 阅读数:4132

ik的官方文档使用的是text类型的文档,目前本地已测试成功,
但我想搜索pdf文件,要用到attachment类型,
请问如何在搜索时使用ik分词器呢?
能否给个demo
已邀请:

ouyangDD

赞同来自:

目前使用如下配置:
 
映射
{
"settings":{
"index":{
"number_of_shards":1,
"number_of_replicas":0
}
},
"mappings": {
"doc": {
"properties": {
"file": {
"type": "attachment",
"fields": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
}
}
}
}
}
}
}

 
索引文档
fp = open('/Users/oubing/test4.pdf', 'rb')
text = base64.b64encode(fp.read())
fp.close()
es.index(index="ai", doc_type="doc", body={"file":text})

 搜索
res = es.search(index="ai", body={
"query": {
"match": {
"file.content": "中国"
}
},
"highlight": {
"fields": {
"file.content": {}
}
}
})
for hit in res["hits"]["hits"]:
print(hit["_id"])

要回复问题请先登录注册