【发布时间】:2018-10-10 14:48:03
【问题描述】:
我有一个 Python 代码中描述的场景。 在此,我试图将 new york 和 ny 明确定义为同义词。但不幸的是,它不起作用。您能否指导我,因为我是弹性搜索的新手。 我也在使用自定义分析器。 我也有包含文本的文件 synonyms.txt: 纽约,纽约,纽约
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch()
keywords = ['thousand eyes', 'facebook', 'superdoc', 'quora', 'your story', 'Surgery', 'lending club', 'ad roll',
'the honest company', 'Draft kings', 'newyork']
count = 1
doc_setting = {
"settings": {
"analysis": {
"analyzer": {
"my_analyzer_keyword": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"asciifolding",
"lowercase",
"synonym"
]
},
"my_analyzer_shingle": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"asciifolding",
"lowercase",
"synonym"
]
}
},
"filter": {
"synonym": {
"type": "synonym",
"synonyms_path": "synonyms.txt",
"ignore_case": "true"
}
}
}
}, "mappings": {
"your_type": {
"properties": {
"keyword": {
"type": "string",
"index_analyzer": "my_analyzer_keyword",
"search_analyzer": "my_analyzer_shingle"
}
}
}
}
}
validate=es.index(index='test', doc_type='your_type', body=doc_setting)
print(validate)
for keyword in keywords:
doc = {
'id': count,
'keyword': keyword
}
res = es.index(index="test", doc_type='your_type', id=count, body=doc)
print(res['result'])
count = count + 1
#res11 = es.get(index="test", doc_type='your_type', id=1)
#print(res11['_source'])
es.indices.refresh(index="test")
question = "I saw news on ny news channel of lending club on facebook, your story and quora"
print("Question asked: %s" % question)
res = es.search(index="test",`enter code here` doc_type='your_type', body={
"query": {"match": {"keyword": question}}})
print("Got %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
print(hit["_source"])
【问题讨论】:
-
你把同义词文件放在哪里了?
-
在同一个文件夹中,我正在运行这个 python 文件。我也尝试过手动方法,方法是在过滤器中给出同义词而不是 synonyms_path:“同义词”:[“ny,newyork,nyork”]。它也不适用。
-
您的“关键字”字段的示例内容是什么?因为您的 indexing_analyzer 和您的 search_analyzer 仅在该字段仅包含一个单词时才兼容...我实际上认为问题在于您的 indexing_analyzer - 使用关键字标记器非常奇怪。
-
我也尝试过使用标准分词器。实际上我正在从列表关键字解析问题并匹配其中的单词= ['千眼','facebook','superdoc','quora','你的故事','手术','借贷俱乐部','广告卷'、'诚实的公司'、'选秀之王'、'纽约']。而且我希望纽约也能进入结果集中,因为它是以同义词给出的
-
如果我没记错的话,同义词文件必须在elasticsearch下的config文件夹中。您可以使用分析 api 进行调试,以查看您所期望的是否与 ES 所做的匹配,请参阅此处了解详细信息elastic.co/guide/en/elasticsearch/reference/6.4/…
标签: python python-3.x elasticsearch