Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search does not use limit #437

Closed
1 task done
masics opened this issue Jun 30, 2019 · 5 comments
Closed
1 task done

Search does not use limit #437

masics opened this issue Jun 30, 2019 · 5 comments

Comments

@masics
Copy link

masics commented Jun 30, 2019

Description of the Issue

When I use limit=1 to the search query it still generates a lot results.

Versions Used

Box Python SDK: 2.5.0
Python: 3.7.3

Steps to Reproduce

search_results = client.search().query(
    "'" + name + "'",
    limit=1,
    offset=0,
    ancestor_folders=[client.folder(folder_id='XXXXXXXXX')],
    result_type='folder',
    content_types='n'
)
collectionSize = 0;
for item in search_results:
    collectionSize += 1
    item_with_name = item.get(fields=['name', 'path_collection'])
print(collectionSize)

Error Message, Including Traceback

I can get thousands of results in collection.

@mattwiller
Copy link
Contributor

@masics Please see #366 — the limit parameter refers to the limit of search results fetched per API call, not the total number of results that will be produced by the iterator. If you only want the first item out of the iterator, you can just call search_results.next() to get it.

@masics
Copy link
Author

masics commented Jul 2, 2019 via email

@mattwiller
Copy link
Contributor

@masics Unfortunately, there is not an exact way to perform that sort of search. It looks like you're already trying to use quotes to get an exact search for the name of the item you're looking for, but I think you'll need to use double quotes instead of single quotes for that. You should be able to get the most precise search results and then iterate through the results to find the exact folder you're looking for like this:

search_results = client.search().query(
    '"' + name + '"',
    ancestor_folders=[client.folder(folder_id='XXXXXXXXX')],
    result_type='folder',
    content_types='name',
    fields=['name', 'path_collection']
)
for item in search_results:
    path = '/'.join([folder.name for folder in item.path_collection])
    if path == 'All Files/folder1/folder2':
        # `item` is the one you're looking for!

@masics
Copy link
Author

masics commented Jul 2, 2019 via email

@mattwiller
Copy link
Contributor

@masics Ah, if your folder name is a single letter then that does seem like it may be expected behavior. You should be able to speed up your code overall by using fields on the search query itself rather than making a separate call to get each item (see my sample code above), but otherwise this isn't an SDK issue but a limitation of the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants