- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用了两个包(即django-elasticsearch-dsl==7.1.4
和django-elasticsearch-dsl-drf==0.20.8
)将搜索引擎添加到我的Django项目中。
我用 flex 索引的模型是:
class Article(models.Model):
created_time = models.DateTimeField(_('created time'), auto_now_add=True)
updated_time = models.DateTimeField(_('updated time'), auto_now=True)
profile = models.ForeignKey('accounts.UserProfile', verbose_name=_('profile'), on_delete=models.PROTECT)
approved_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('approved user'), blank=True, null=True, editable=False, on_delete=models.CASCADE, related_name='article_approved_users')
approved_time = models.DateTimeField(_('approved time'), blank=True, null=True, db_index=True, editable=False)
title = models.CharField(_('title'), max_length=50)
image = models.ImageField(_('image'), blank=True, upload_to=article_directory_path)
slug = models.SlugField(_('slug'), max_length=50, unique=True)
content = models.TextField(_('content'))
summary = models.TextField(_('summary'))
views_count = models.PositiveIntegerField(verbose_name=_('views count'), default=int, editable=False)
is_free = models.BooleanField(_('is free'), default=True)
is_enable = models.BooleanField(_('is enable'), default=True)
tags = TaggableManager(verbose_name=_('tags'), related_name='articles')
categories = models.ManyToManyField('Category', verbose_name=_('categories'), related_name='articles')
namads = models.ManyToManyField('namads.Namad', verbose_name=_('namads'), related_name='articles', blank=True)
我使用以下文档为
Article
模型建立索引:
html_strip = analyzer(
'html_strip',
tokenizer="whitespace",
filter=["lowercase", "stop", "snowball"],
char_filter=["html_strip"]
)
@registry.register_document
class ArticleDocument(Document):
title = fields.TextField(
analyzer=html_strip,
fields={
'raw': fields.TextField(analyzer='keyword'),
'suggest': fields.CompletionField(),
}
)
tags = fields.ObjectField(
properties={
"name": fields.TextField(
analyzer=html_strip,
fields={
'raw': fields.TextField(analyzer='keyword'),
'suggest': fields.CompletionField(),
}
)
}
)
categories = fields.ObjectField(
properties={
'id': fields.IntegerField(),
'title': fields.TextField(
analyzer=html_strip,
fields={
'raw': fields.TextField(analyzer='keyword'),
'suggest': fields.CompletionField(),
}
)
}
)
namads = fields.ObjectField(
properties={
"id": fields.IntegerField(),
"name": fields.TextField(
analyzer=html_strip,
fields={
'raw': fields.TextField(analyzer='keyword'),
'suggest': fields.CompletionField(),
}
),
"group_name": fields.TextField(
analyzer=html_strip,
fields={
'raw': fields.TextField(analyzer='keyword'),
'suggest': fields.CompletionField(),
}
)
}
)
class Index:
name = settings.ARTICLE_INDEX_NAME
settings = {
"number_of_shards": 1,
"number_of_replicas": 0
}
def get_queryset(self):
return super(ArticleDocument, self).get_queryset().filter(
approved_user__isnull=False,
is_enable=True
).prefetch_related(
'tags',
'categories',
'namads'
)
class Django:
model = Article
fields = ['id', 'summary']
最后使用以下 View 集搜索其结果(基于
this document)。
class ArticleSearchViewSet(DocumentViewSet):
"""
list:
Search on all articles, ordered by most recently added.
query parameters
- Search fields: 'title', 'summary', 'tags.name', 'categories.title', 'namads.name',
'namads.group_name' . Ex: ?search=some random name.
retrieve:
Return a specific article details.
"""
serializer_class = ArticleDocumentSerializer
document = ArticleDocument
pagination_class = PageNumberPagination
lookup_field = 'id'
filter_backends = [
FilteringFilterBackend,
IdsFilterBackend,
OrderingFilterBackend,
DefaultOrderingFilterBackend,
CompoundSearchFilterBackend,
SuggesterFilterBackend,
]
search_fields = (
'title',
'summary',
'tags.name',
'categories.title',
'namads.name',
'namads.group_name'
)
filter_fields = {
'id': {
'field': 'id',
# Note, that we limit the lookups of id field in this example,
# to `range`, `in`, `gt`, `gte`, `lt` and `lte` filters.
'lookups': [
LOOKUP_FILTER_RANGE,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_GT,
LOOKUP_QUERY_GTE,
LOOKUP_QUERY_LT,
LOOKUP_QUERY_LTE,
],
},
'namads': {
'field': 'namads',
# Note, that we limit the lookups of `pages` field in this
# example, to `range`, `gt`, `gte`, `lt` and `lte` filters.
'lookups': [
LOOKUP_FILTER_RANGE,
LOOKUP_QUERY_GT,
LOOKUP_QUERY_GTE,
LOOKUP_QUERY_LT,
LOOKUP_QUERY_LTE,
],
},
'title': "title.raw",
'summary': 'summary',
'categories': {
'field': 'categories',
# Note, that we limit the lookups of `pages` field in this
# example, to `range`, `gt`, `gte`, `lt` and `lte` filters.
'lookups': [
LOOKUP_FILTER_RANGE,
LOOKUP_QUERY_GT,
LOOKUP_QUERY_GTE,
LOOKUP_QUERY_LT,
LOOKUP_QUERY_LTE,
],
},
'tags': {
'field': 'tags',
# Note, that we limit the lookups of `tags` field in
# this example, to `terms, `prefix`, `wildcard`, `in` and
# `exclude` filters.
'lookups': [
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_EXCLUDE,
],
},
}
# Suggester fields
suggester_fields = {
'title_suggest': {
'field': 'title.suggest',
'suggesters': [
SUGGESTER_TERM,
SUGGESTER_COMPLETION,
SUGGESTER_PHRASE,
],
'default_suggester': SUGGESTER_COMPLETION,
'options': {
'size': 10, # Number of suggestions to retrieve.
'skip_duplicates': True, # Whether duplicate suggestions should be filtered out.
},
},
'tags_suggest': {
'field': 'tags.name.suggest',
'suggesters': [
SUGGESTER_COMPLETION,
],
# 'options': {
# 'size': 20, # Override default number of suggestions
# },
},
'categories_suggest': {
'field': 'categories.title.suggest',
'suggesters': [
SUGGESTER_TERM,
SUGGESTER_COMPLETION,
SUGGESTER_PHRASE,
],
},
'namads_name_suggest': {
'field': 'namads.name.suggest',
'suggesters': [
SUGGESTER_COMPLETION,
],
},
'namad_group_name_suggest': {
'field': 'namads.group_name.suggest',
'suggesters': [
SUGGESTER_COMPLETION,
],
},
}
ordering_fields = {
'id': 'id',
'title': 'title',
'summary': 'summary',
}
# Specify default ordering
ordering = ('-id', )
我的文档序列化器是:
class ArticleDocumentSerializer(DocumentSerializer):
class Meta:
document = ArticleDocument
fields = ['id', 'title', 'summary', 'namads', 'categories', 'tags']
一切正常,除了当我使用带有以下查询参数的
term
和
phrase
搜索时:
?title_suggest__phrase=fi
?title_suggest__term=fi
在
localhost/api/v1/blog/articles-search/suggest/
网址中,但在两种情况下,结果都相同,如下所示:
{
"title_suggest__term": [
{
"text": "fi",
"offset": 0,
"length": 2,
"options": []
}
]
}.
而且我也很确定自己的索引中包含
First article
,并且当我使用完成提示器时,一切正常(即
?title_suggest__completion=fi
),并且返回结果。
最佳答案
问题出在我的建议程序配置中。首先,对于term
和phrase
,建议我们不需要完成字段(即'suggest': fields.CompletionField()
),我们只需要在Index
中声明我们的字段即可,例如:
title = fields.TextField(
fields={
'raw': fields.TextField(analyzer=html_strip)
}
) # which goes in documents.py
并将以下内容添加到任何字段以启用
term
和
phrase
建议:
suggester_fields = {
'title_suggest': {
'field': 'title',
'suggesters': [
SUGGESTER_TERM,
SUGGESTER_PHRASE,
],
},
} # Which goes in views.py and search view suggester_fields
并查看相关的建议结果,我们应该发送查询参数,例如
?title_suggest__term=something
或
?title_suggest__phrase=something
。
suggester_fields = {
'title_suggest': {
'field': 'title',
'suggesters': [
SUGGESTER_TERM,
SUGGESTER_PHRASE,
],
},
'title': {
'field': 'title',
'suggesters': [
SUGGESTER_COMPLETION,
],
},
}
现在通过此配置,我们可以在标题字段中获得三种类型的建议(即
term
,
phrase
和
completion
)。因此,如果我们想要基于这三个建议者的整体结果,则应使用三个查询参数,例如:
localhost/api/v1/blog/articles-search/suggest/?title_suggest__term=something&title_suggest__phrase=something&title__completion=something
并且不要忘记在
Index
中更改字段配置(以防我们需要完成提示器):
title = fields.TextField(
fields={
'raw': fields.TextField(analyzer=html_strip),
'suggest': fields.CompletionField(),
}
) # Which goes in documents.py
关于python - Django elasticsearch DSL术语和短语搜索不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63965538/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!