使用elasticsearch测试django haystack时的非确定性结果

时间:2022-10-26 03:19:06

I am functional testing results returned by django-haystack with elasticsearch.

我是django-haystack使用elasticsearch返回的功能测试结果。

I'm having different results when running a test. Sometimes the test pass, sometimes it doesn't.

我在运行测试时遇到了不同的结果。有时测试通过,有时它没有。

I can't figure out why this happens.

我无法弄清楚为什么会这样。

My test class first creates entries in the test database, and then call the manage.py rebuild_index, using StaticLiveServerTestCase setUp method. In the end I call manage.py clear_index.

我的测试类首先在测试数据库中创建条目,然后使用StaticLiveServerTestCase setUp方法调用manage.py rebuild_index。最后我调用manage.py clear_index。

I won't go to reproduce here all django code for search indexes, _text.txt's because the django-haystack/elasticsearch code is working.

我不会在这里重现搜索索引的所有django代码,_ text.txt,因为django-haystack / elasticsearch代码正在运行。

I'm wondering if it's a problem of synchronization between the database entries created and the call to rebuild_index.

我想知道在创建的数据库条目和对rebuild_index的调用之间是否存在同步问题。

Basically, in my tests I do this

基本上,在我的测试中,我这样做

class SearchTest(FunctionalTest):

    def setUp(self):
        super(SearchTest, self).setUp()  # this make the entries in database
        self.rebuild_index()

    def tearDown(self):
        super(SearchTest, self).tearDown()
        call_command('clear_index', interactive=False)

Printing the objects indexed I obtain, sometimes, the incorrect results:

打印索引的对象,有时会得到不正确的结果:

<QuerySet [<Step: emg>]>
<QuerySet [<Step: emg>]>
<QuerySet [<Step: emg>]>

and, sometimes, the correct ones:

而且,有时,正确的:

<QuerySet [<Step: emg>]>
<QuerySet [<Step: emg>]>
<QuerySet [<Step: emg>]>
<QuerySet [<Step: eeg>, <Step: emg>]>
<QuerySet [<Step: eeg>, <Step: emg>]>

So, what could be happening?

那么,会发生什么?

1 个解决方案

#1


0  

You say the answer at the title. "Non deterministic", first you need to understand what is this. Nondeterministic algorithms can get different answer for the same input. But why this?

你在标题上说出答案。 “非确定性”,首先你需要了解这是什么。非确定性算法可以为相同的输入获得不同的答案。但为什么呢?

If you remember why we use non Nondeterministic algorithms you find the solution. Nondeterministic algorithms need to be faster than deterministic, generally, and for this they can "generalized" some things (like approximation in math) and this generalization can result in differents results. But, is really fast :)

如果您还记得为什么我们使用非非确定性算法,您会找到解决方案。通常,非确定性算法需要比确定性更快,并且为此它们可以“推广”某些事物(如数学中的近似),并且这种推广可以导致不同的结果。但是,真的很快:)

Read more about Nondeterministic algorithms

阅读有关非确定性算法的更多信息

edit: maybe elasticsearch or django-haystack have some config for deterministic search

编辑:也许elasticsearch或django-haystack有一些确定性搜索的配置

#1


0  

You say the answer at the title. "Non deterministic", first you need to understand what is this. Nondeterministic algorithms can get different answer for the same input. But why this?

你在标题上说出答案。 “非确定性”,首先你需要了解这是什么。非确定性算法可以为相同的输入获得不同的答案。但为什么呢?

If you remember why we use non Nondeterministic algorithms you find the solution. Nondeterministic algorithms need to be faster than deterministic, generally, and for this they can "generalized" some things (like approximation in math) and this generalization can result in differents results. But, is really fast :)

如果您还记得为什么我们使用非非确定性算法,您会找到解决方案。通常,非确定性算法需要比确定性更快,并且为此它们可以“推广”某些事物(如数学中的近似),并且这种推广可以导致不同的结果。但是,真的很快:)

Read more about Nondeterministic algorithms

阅读有关非确定性算法的更多信息

edit: maybe elasticsearch or django-haystack have some config for deterministic search

编辑:也许elasticsearch或django-haystack有一些确定性搜索的配置