2012-09-14 47 views
1

我注意到,從Python shell運行rebuild_index會導致排序問題。從Bash運行rebuild_index做對了。Haystack使用的ElasticSearch索引被未知原因破壞

$ 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": o[email protected]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": o[email protected]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": o[email protected]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": o[email protected]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": o[email protected]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頭前端檢查的索引元數據。我發現設置結構不同,這取決於我從哪個shell運行rebuild_index

好的版本(從猛砸rebuild_index運行):

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 
} 

壞版本(從Python外殼rebuild_index運行):

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

這是因爲在後端實例變量需要運行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)