我正在使用elasticsearch dsl在elasticsearch索引中索引数据。我正在使用以下类创建ElasticSearch文档:
class BooksDoc(Document):
title = Text(
analyzer=my_analyzer
)
author = Text(
analyzer=my_analyzer
)
publisher = Text(
analyzer=my_analyzer
)
image_url = Text(analyzer=my_analyzer)
price = Text(analyzer=my_analyzer)
category =Text(analyzer=my_analyzer)
published = Boolean()
upload_date = Text()
class Index:
name = 'books'
这是我的分析仪:
my_analyzer = analyzer('my_analyzer',
tokenizer=tokenizer('trigram', 'edge_ngram', min_gram=1, max_gram=20),
filter=['lowercase']
)
我正在使用以下函数为文档编制索引:
def indexing(self):
doc = BooksDoc(
meta={'id': self.id},
title=self.book_title,
author=self.book_author,
publisher=self.book_publisher,
image_url=self.front_image,
price=self.book_price,
catagory=__catagory,
published=self.isPublished,
upload_date=self.dateadded
)
try:
doc.save()
return doc.to_dict(include_meta=True)
except:
print(traceback.format_exc())
return None
我希望实现搜索,这样当用户输入搜索字符串时,我可以查询elasticsearch并获取找到匹配项的所有记录。例如,如果用户输入“嘘”,那么所有包含字符串“嘘”的记录都应该被返回。现在,当字符串在弹性搜索中完全匹配时,搜索效果很好,但是如果有部分匹配,我也想获取记录。我该怎么做?