- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在建立类似的索引:
class BookIndex(indexes.SearchIndex,indexes.Indexable):
text= indexes.EdgeNgramField(document=True,use_template=True)
content_auto = indexes.EdgeNgramField(model_attr='title')
isbn_13 = indexes.CharField(model_attr='isbn_13')
validate = indexes.IntegerField(model_attr='validate')
price = indexes.IntegerField(model_attr='price')
authors = indexes.EdgeNgramField()
reviews = indexes.CharField()
publishers = indexes.EdgeNgramField()
institutes = indexes.EdgeNgramField()
sellers = indexes.CharField()
category = indexes.CharField()
sub_category = indexes.CharField()
SearchQuerySet().all().filter(content=query)
一样查询它,即使它没有显示部分匹配的结果,我也尝试过
SearchQuerySet().filter(content__contains=query)
。
最佳答案
Haystack对于ElasticSearch并不是很好,您不能使用适当的索引值,因此必须提供自定义ElasticSearchBackEnd才能启用它:
#in a search_backends.py file
from django.conf import settings
from haystack.backends.elasticsearch_backend import (
ElasticsearchSearchBackend,
ElasticsearchSearchEngine
)
from haystack.fields import EdgeNgramField as BaseEdgeNgramField, NgramField as BaseNgramField
from haystack.indexes import CharField
#just an example of which degree of configuration could be possible
CUSTOM_FIELD_TYPE = {
'completion': {
'type': 'completion',
'payloads': True,
'analyzer': 'suggest_analyzer',
'preserve_separators': True,
'preserve_position_increments': False
},
}
# Custom Backend
class CustomElasticBackend(ElasticsearchSearchBackend):
DEFAULT_ANALYZER = None
def __init__(self, connection_alias, **connection_options):
super(CustomElasticBackend, self).__init__(
connection_alias, **connection_options)
user_settings = getattr(settings, 'ELASTICSEARCH_INDEX_SETTINGS', None)
self.DEFAULT_ANALYZER = getattr(settings, 'ELASTICSEARCH_DEFAULT_ANALYZER', "snowball")
if user_settings:
setattr(self, 'DEFAULT_SETTINGS', user_settings)
def build_schema(self, fields):
content_field_name, mapping = super(CustomElasticBackend,
self).build_schema(fields)
for field_name, field_class in fields.items():
field_mapping = mapping[field_class.index_fieldname]
index_analyzer = getattr(field_class, 'index_analyzer', None)
search_analyzer = getattr(field_class, 'search_analyzer', None)
field_analyzer = getattr(field_class, 'analyzer', self.DEFAULT_ANALYZER)
if field_mapping['type'] == 'string' and field_class.indexed:
field_mapping["term_vector"] = "with_positions_offsets"
if not hasattr(field_class, 'facet_for') and not field_class.field_type in('ngram', 'edge_ngram'):
field_mapping['analyzer'] = field_analyzer
if field_class.field_type in CUSTOM_FIELD_TYPE:
field_mapping = CUSTOM_FIELD_TYPE.get(field_class.field_type).copy()
if index_analyzer and search_analyzer:
field_mapping['index_analyzer'] = index_analyzer
field_mapping['search_analyzer'] = search_analyzer
if 'analyzer' in field_mapping:
del(field_mapping['analyzer'])
mapping.update({field_class.index_fieldname: field_mapping})
return (content_field_name, mapping)
class CustomElasticSearchEngine(ElasticsearchSearchEngine):
backend = CustomElasticBackend
# Custom fields, just use the ones you need or create yours
class CustomFieldMixin(object):
def __init__(self, **kwargs):
self.analyzer = kwargs.pop('analyzer', None)
self.index_analyzer = kwargs.pop('index_analyzer', None)
self.search_analyzer = kwargs.pop('search_analyzer', None)
super(CustomFieldMixin, self).__init__(**kwargs)
class CustomCharField(CustomFieldMixin, CharField):
pass
class CustomCompletionField(CustomFieldMixin, CharField):
field_type = 'completion'
class CustomEdgeNgramField(CustomFieldMixin, BaseEdgeNgramField):
pass
class CustomNgramField(CustomFieldMixin, BaseNgramField):
pass
#settings.py
ELASTICSEARCH_INDEX_SETTINGS = {
'settings': {
"analysis": {
"analyzer": {
"custom_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [ "lowercase", "asciifolding" ]
},
"str_index_analyzer" : {
"type": "custom",
"tokenizer" : "haystack_ngram_tokenizer",
"filter" : ["stopwords", "asciifolding", "lowercase", "snowball", "elision", "worddelimiter"]
},
"str_search_analyzer" : {
"type": "custom",
"tokenizer" : "standard",
"filter" : ["stopwords", "asciifolding", "lowercase", "snowball", "elision", "worddelimiter"]
},
"suggest_analyzer": {
"type":"custom",
"tokenizer":"standard",
"filter":[
"stopwords",
"standard",
"lowercase",
"asciifolding"
]
},
},
"tokenizer": {
"haystack_ngram_tokenizer": {
"type": "nGram",
"min_gram": 2,
"max_gram": 20,
},
},
"filter": {
"elision": {
"type": "elision",
"articles": ["l", "m", "t", "qu", "n", "s", "j", "d"]
},
"stopwords": {
"type": "stop",
"stopwords": ["_french_", "_english_"],
"ignore_case": True
},
"worddelimiter": {
"type": "word_delimiter"
}
}
}
}
}
#Haystack settings
HAYSTACK_CONNECTIONS = {
'default': {
...
'ENGINE': 'path.to.search_backends.CustomElasticSearchEngine',
...
},
}
关于django - 尽管使用Ngram和Edgengram构建索引,但部分搜索在Elasticsearch + Haystack上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35982784/
我正在尝试为索引设置一个新的映射。它将支持由 ES 提供支持的部分关键字搜索和自动完成请求。 带有空白标记器的 edgeNGram 标记过滤器似乎是一种可行的方法。到目前为止,我的设置看起来像这样:
当我在一个字段上搜索时,评分似乎没有考虑字段的长度(即文本较长的文档与较短的文档的评分相同,并且出于某种原因放在前面)。不确定我做错了什么。 字段索引如下: "name": { "ty
我有大约一百万条记录的 Elasticsearch 索引。 我想对 Elasticsearch 索引中的2个字段,名称和ID(总共约10个)进行多前缀搜索。 创建EdgeNGram自动完成过滤器是否有
关于 EdgeNgram 如何处理数字有什么想法吗? 我正在使用 ElasticSearch 后端运行 haystack。我创建了一个 EdgeNgram 类型的索引字段。该字段将包含一个字符串,该字
我最近一直在尝试使用 Django Haystack (v2.1.0) 和 Elasticsearch (v0.90.5) 作为我的搜索引擎在我的网络应用程序上实现全文搜索。我的目标是能够通过一个查询
我正在尝试制作 部分搜索 工作,寻找 “sw” “瑞典” “瑞典” 应该匹配“瑞典” 我环顾四周,就是无法让它工作 Rails Code 我正在使用 this code从轮胎 repo 作为模板代码。
我们有一个符合 OData 的 API,可以将部分全文搜索需求委托(delegate)给 Elasticsearch 集群。 由于 OData 表达式可能变得非常复杂,因此我们决定将它们简单地转换为等
当我使用带有 edgengram (min=3, max=7, front) + term_vector=with_positions_offsets 的分析器时 文档有 text = "CouchD
我有一个大约 40k 行的数据集,每行有 4 个字段。现在我想对文本框中的这 4 个字段使用自动完成机制(在显示建议之前必须将这 4 个字段中的值连接成一个字符串)。哪种查询会更好地扩展和执行?在 E
我有一个大约 40k 行的数据集,每行有 4 个字段。现在我想对文本框中的这 4 个字段使用自动完成机制(在显示建议之前必须将这 4 个字段中的值连接成一个字符串)。哪种查询会更好地扩展和执行?在 E
假设我有 5 部电影片名: 无太阳 珊莎 这也是 Solr 古德 唯一幸存者 我想实现一个具有这种预期行为的自动完成搜索字段: “Sans”> Sans Soleil,Sansa “Sans so”
我有三个带有“用户名”字段的文档: '布里安迪利' 'briangumble' 'briangriffen' 当我搜索“brian”时,我按预期得到了所有三个,但是当我搜索“briandilley”时
编辑 3 :我现在使用的解决方法是从我的查询和索引字段中去除除字母、数字和空格以外的任何内容。这会产生所需的行为,但它在很大程度上是一种解决方法而不是真正的解决方案,而且我仍然想了解 Solr 为什么
我是一名优秀的程序员,十分优秀!