question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Finding first match not working as expected?

See original GitHub issue

Reported by @msarm initially as part of #207 :

I see ‘ft.search’ round trip got reduced now after defaulting the page size to 1000 and this helps other queries to run even faster but my results are not coming this time may be a different issue now.

When I perform the Person query:

Person.find().sort_by('-emp_no').first()

Here is the ft.search performed.

args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 0, 1, 'SORTBY', 'emp_no', 'desc']
args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 1000, 1, 'SORTBY', 'emp_no', 'desc']
args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 2000, 1, 'SORTBY', 'emp_no', 'desc']
args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 3000, 1, 'SORTBY', 'emp_no', 'desc']
args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 4000, 1, 'SORTBY', 'emp_no', 'desc']
args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 5000, 1, 'SORTBY', 'emp_no', 'desc']
args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 6000, 1, 'SORTBY', 'emp_no', 'desc']
args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 7000, 1, 'SORTBY', 'emp_no', 'desc']
args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 8000, 1, 'SORTBY', 'emp_no', 'desc']
args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 9000, 1, 'SORTBY', 'emp_no', 'desc']
args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 10000, 1, 'SORTBY', 'emp_no', 'desc']  - Fired query hangs

When the last fired ft.search is performed it does not return any results it retries for every.

Since are looking just for the first record, can we just fire the first ft.search and return the results?

args: ['ft.search', ':__main__.Person:index', '*', 'LIMIT', 0, 1, 'SORTBY', 'emp_no', 'desc']

When I set explicitly the exhaust_results=False in the first() function, I see quick results with no extra query. Is that right the fix?

    def first(self):
        query = self.copy(offset=0, limit=1, sort_fields=self.sort_fields)
        results = query.execute(exhaust_results=False)
        if not results:
            raise NotFoundError()
        return results[0]

Original code:

import datetime
from typing import Optional

from redis_om import Field, HashModel, Migrator, get_redis_connection

# This Redis instance is tuned for durability.
REDIS_DATA_URL = "redis://localhost:6380"

class Person(HashModel):
    first_name: str = Field(index=True)
    last_name: str = Field(index=True)
    emp_no: int =  Field(index=True)

# set redis connection
Person.Meta.database = get_redis_connection(url=REDIS_DATA_URL,
                                                  decode_responses=True)
# apply migrations
Migrator().run()

for row_number in range(0,10000):
    person = Person(first_name="John" + str(row_number), last_name="Doe", emp_no=row_number)
    
    result = Person.find(Person.emp_no ==row_number).all()
    if (len(result) == 0):
        person.save()

 
    print(person.pk)

# very slow to query a single record (~12 seconds)
Person.find().sort_by('-emp_no').first()

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
simonprickettcommented, Apr 25, 2022

@msarm I think you’re right, and that first does need to set exhaust_results=False… when I do this and try again just get the one query that you’d expect:

1650886093.706949 [0 172.17.0.1:59332] "ft.search" ":__main__.Person:index" "*" "LIMIT" "0" "1" "SORTBY" "emp_no" "desc"

I’ll look at a PR for this, and I suspect that both first and get_item may benefit from this.

Thanks for looking into this.

0reactions
simonprickettcommented, Apr 25, 2022

Fixed in 0.0.26.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Excel MATCH Function Error Troubleshooting Examples
Sometimes, a MATCH formula returns an #N/A error, even if the value you're looking for is in the lookup table. The reason for...
Read more >
Failure to Look Up or Match Values in Excel
First, check for equality between the cells that you believe should match. If Excel considers the contents of cells B1 and E6 to...
Read more >
Index + Match not working as intended, only working for first ...
It's an array formula because there are two row match criteria in the first match function. Switching in the named ranges / named...
Read more >
[Fixed!] INDEX MATCH Not Returning Correct Value in Excel ...
This article describes 5 reasons of index match not returning the correct value & their solutions. Use the solutions to solve your problems....
Read more >
How to correct a #N/A error in INDEX/MATCH functions
Problem : There is no data to match. When the MATCH function does not find the lookup value in the lookup array, it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found