1

我注意到rebuild_index从 Python shell 运行会导致排序问题。rebuild_index从 Bash运行是正确的。

$ bin/manage rebuild_index
WARNING: This will irreparably remove EVERYTHING from your search index in connection 'default'.
Your choices after this are to restore from backups or rebuild via the `rebuild_index` command.
Are you sure you wish to continue? [y/N] y

Removing all documents from your index because you said so.
Failed to clear Elasticsearch index: Non-OK status code returned (404) containing u'IndexMissingException[[my_index] missing]'.
All documents removed.
Indexing 40 components.

--

In [1]: from haystack.query import SearchQuerySet

In [2]: SearchQuerySet().order_by('name')
Out[2]: [<SearchResult: my_app.component (pk=u'2')>, <SearchResult: my_app.component (pk=u'1')>, <SearchResult: my_app.component (pk=u'5')>, <SearchResult: my_app.component (pk=u'4')>, <SearchResult: my_app.component (pk=u'3')>, <SearchResult: my_app.component (pk=u'6')>, <SearchResult: my_app.component (pk=u'7')>, <SearchResult: my_app.component (pk=u'8')>, <SearchResult: my_app.component (pk=u'9')>, <SearchResult: my_app.component (pk=u'11')>, <SearchResult: my_app.component (pk=u'14')>, <SearchResult: my_app.component (pk=u'13')>, <SearchResult: my_app.component (pk=u'15')>, <SearchResult: my_app.component (pk=u'19')>, <SearchResult: my_app.component (pk=u'20')>, <SearchResult: my_app.component (pk=u'21')>, <SearchResult: my_app.component (pk=u'22')>, <SearchResult: my_app.component (pk=u'23')>, <SearchResult: my_app.component (pk=u'24')>, '...(remaining elements truncated)...']

In [3]: from django.core import management

In [4]: management.call_command('rebuild_index', interactive=False, verbosity=0)
Failed to clear Elasticsearch index: Non-OK status code returned (404) containing u'IndexMissingException[[my_index] missing]'.

In [5]: SearchQuerySet().order_by('name')
Failed to query Elasticsearch using '*:*': Non-OK status code returned (500) containing u'SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[SkgxFvdjRgyoiHv2qSpfbQ][my_index][4]: QueryPhaseExecutionException[[my_index][4]: query[filtered(ConstantScore(NotDeleted(cache(QueryWrapperFilter(django_ct:my_app.component)))))->cache(_type:modelresult)],from[0],size[20],sort[<custom:"name": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@3cb9b947>]: Query Failed [Failed to execute main query]]; nested: IOException[Can\'t sort on string types with more than one value per doc, or more than one token per field]; }{[SkgxFvdjRgyoiHv2qSpfbQ][my_index][0]: QueryPhaseExecutionException[[my_index][0]: query[filtered(ConstantScore(NotDeleted(cache(QueryWrapperFilter(django_ct:my_app.component)))))->cache(_type:modelresult)],from[0],size[20],sort[<custom:"name": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@249cf580>]: Query Failed [Failed to execute main query]]; nested: IOException[Can\'t sort on string types with more than one value per doc, or more than one token per field]; }{[SkgxFvdjRgyoiHv2qSpfbQ][my_index][3]: QueryPhaseExecutionException[[my_index][3]: query[filtered(ConstantScore(NotDeleted(cache(QueryWrapperFilter(django_ct:my_app.component)))))->cache(_type:modelresult)],from[0],size[20],sort[<custom:"name": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@4ff80724>]: Query Failed [Failed to execute main query]]; nested: IOException[Can\'t sort on string types with more than one value per doc, or more than one token per field]; }{[SkgxFvdjRgyoiHv2qSpfbQ][my_index][2]: QueryPhaseExecutionException[[my_index][2]: query[filtered(ConstantScore(NotDeleted(cache(QueryWrapperFilter(django_ct:my_app.component)))))->cache(_type:modelresult)],from[0],size[20],sort[<custom:"name": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@99b8b51>]: Query Failed [Failed to execute main query]]; nested: IOException[Can\'t sort on string types with more than one value per doc, or more than one token per field]; }{[SkgxFvdjRgyoiHv2qSpfbQ][my_index][1]: QueryPhaseExecutionException[[my_index][1]: query[filtered(ConstantScore(NotDeleted(cache(QueryWrapperFilter(django_ct:my_app.component)))))->cache(_type:modelresult)],from[0],size[20],sort[<custom:"name": org.elasticsearch.index.field.data.strings.StringFieldDataType$1@42d777c4>]: Query Failed [Failed to execute main query]]; nested: IOException[Can\'t sort on string types with more than one value per doc, or more than one token per field]; }]'.
Out[5]: []

我使用 elasticsearch-head 前端检查了索引元数据。我发现设置结构因我rebuild_index从哪个 shell 运行而异。

好的版本(rebuild_index从 Bash 运行):

settings: {
index.analysis.filter.haystack_ngram.type: nGram
index.analysis.tokenizer.haystack_edgengram_tokenizer.type: edgeNGram
index.analysis.analyzer.edgengram_analyzer.filter.0: haystack_edgengram
index.analysis.filter.haystack_edgengram.min_gram: 2
index.analysis.tokenizer.haystack_edgengram_tokenizer.side: front
index.analysis.filter.haystack_edgengram.type: edgeNGram
index.analysis.filter.haystack_ngram.max_gram: 15
index.analysis.analyzer.edgengram_analyzer.type: custom
index.analysis.tokenizer.haystack_ngram_tokenizer.max_gram: 15
index.analysis.tokenizer.haystack_ngram_tokenizer.type: nGram
index.analysis.tokenizer.haystack_edgengram_tokenizer.max_gram: 15
index.analysis.tokenizer.haystack_edgengram_tokenizer.min_gram: 2
index.analysis.analyzer.ngram_analyzer.filter.0: haystack_ngram
index.analysis.analyzer.ngram_analyzer.tokenizer: lowercase
index.analysis.tokenizer.haystack_ngram_tokenizer.min_gram: 3
index.analysis.filter.haystack_ngram.min_gram: 3
index.analysis.analyzer.ngram_analyzer.type: custom
index.analysis.analyzer.edgengram_analyzer.tokenizer: lowercase
index.analysis.filter.haystack_edgengram.max_gram: 15
index.number_of_shards: 5
index.number_of_replicas: 1
index.version.created: 190999
}

坏版本(rebuild_index从 Python shell 运行):

settings: {
index.number_of_shards: 5
index.number_of_replicas: 1
index.version.created: 190999
}

这些index.analysis.*钥匙是什么?他们来自哪里?

干草堆票:https ://github.com/toastdriven/django-haystack/issues/645

4

1 回答 1

4

发生这种情况是因为后端的实例变量需要在rebuild_index第二次运行之前重置。

from django.core import management
from haystack import connections
backend = connections['default'].get_backend()
backend.setup_complete = False
backend.existing_mapping = None
management.call_command('rebuild_index', interactive=False, verbosity=0)
于 2012-09-20T10:17:04.583 回答