简体   繁体   中英

Non-deterministic results when testing django haystack with elasticsearch

I am functional testing results returned by django-haystack with 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 .

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

I'm wondering if it's a problem of synchronization between the database entries created and the call to 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM