Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多次运行rebuild_index命令看起来像是再次标记值_Python_Django_Django Haystack - Fatal编程技术网

Python 多次运行rebuild_index命令看起来像是再次标记值

Python 多次运行rebuild_index命令看起来像是再次标记值,python,django,django-haystack,Python,Django,Django Haystack,从一个Python shell会话中多次运行rebuild\u index命令时,我注意到了奇怪的行为 在本例中,我从bashshell运行rebuild\u index,然后进入pythonshell以检查facet值。它们是资本化的。然后,我从pythonshell运行了rebuild\u index命令,facet值是小写的 $ bin/manage rebuild_index WARNING: This will irreparably remove EVERYTHING from yo

从一个Python shell会话中多次运行
rebuild\u index
命令时,我注意到了奇怪的行为

在本例中,我从bashshell运行
rebuild\u index
,然后进入pythonshell以检查facet值。它们是资本化的。然后,我从pythonshell运行了
rebuild\u index
命令,facet值是小写的

$ 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().models(Component).facet('primary_language').facet_counts()
Out[2]:
{'dates': {},
 'fields': {'primary_language': [(u'Java', 24),
   (u'Scala', 5),
   (u'C', 3),
   (u'Python', 2),
   (u'C++', 2),
   (u'PHP', 1),
   (u'Javascript', 1),
   (u'Ada', 1),
   (u'.Net', 1)]},
 'queries': {}}

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().models(Component).facet('primary_language').facet_counts()
Out[5]:
{'dates': {},
 'fields': {'primary_language': [(u'java', 24),
   (u'scala', 5),
   (u'c', 5),
   (u'python', 2),
   (u'php', 1),
   (u'net', 1),
   (u'javascript', 1),
   (u'ada', 1)]},
 'queries': {}}
下一个奇怪的事情是,如果我从Python shell运行
rebuild\u index
命令并尝试按
name
字段排序,我会得到一个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": 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]: []
这使我得出结论,在Python解释器的生命周期中,
rebuild\u index
只能成功运行一次。看起来在Python解释器中再次运行它会使Haystack再次标记这些值。这解释了为什么facet值以小写形式返回。它还解释了我在尝试对结果进行排序时收到的错误消息:“无法对每个文档具有多个值或每个字段具有多个标记的字符串类型进行排序”。它必须将
名称
值按空格字符拆分为多个值

现在的问题是,如果我在Python解释器中多次运行
rebuild\u index
,为什么对Haystack很重要?我不理解这一点,因为
rebuild\u index
作为第一个操作也会运行
clear\u index
。清除索引不应该是确保再次重建索引得到相同结果的一种充分方法吗?显然这不是真的。我不知道为什么

这里真正的困境是,如果我不能编写正确的单元测试(在
setUp()
中运行
rebuild\u index
),我到底该如何编写呢 1) 是否检索原始格式中的镶嵌面值? 2) 我不能按具有空格值的字段排序


有任何解释、建议或想法吗?

之所以会出现这种情况,是因为在第二次运行
rebuild\u 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)
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)