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.

changed behaviour for __contains queries and multiple words in search-term

See original GitHub issue

I’m creating this as a bug, while of course this could be also an intentional (breaking) change to the library. If that’s the case I would work around it.

Beginning with version 7.6.0 there was a change in search behaviour, originating in in this commit.

The example is

  • search_fields = {'title__icontains'} and
  • using ModelSelect2Widget as base for the widget

Now I’m searching for two separate words.

The old behavior (7.5.0 and earlier) was that both words needed to be in the title, so the term was split into the words, while combining the resulting two __icontains searches with AND. So in the result for the selectbox we only get the records which contain both words in the title.

New behaviour since 7.6.0 is different, here we also split, but the searches are combined with OR. So in the result we have all records that have either one OR the other word in the title

Expected behavior I would have expected the search-results to be the same. While I understand the breaking change in the release, I don’t see why this specific behaviour was changed.

When we take the default django-admin-search as an example (also the base for autocomplete_fields), we find:

        use_distinct = False
        search_fields = self.get_search_fields(request)
        if search_fields and search_term:
            orm_lookups = [construct_search(str(search_field))
                           for search_field in search_fields]
            for bit in search_term.split():
                or_queries = [models.Q(**{orm_lookup: bit})
                              for orm_lookup in orm_lookups]
                queryset = queryset.filter(reduce(operator.or_, or_queries))
            use_distinct |= any(lookup_needs_distinct(self.opts, search_spec) for search_spec in orm_lookups)

Which means, django admin also does:

  • use OR for connecting the searches by field,
  • but is uses AND for the search for multiple words in the search-term.

possible solution

If you see this also as a bug, I’m happy to work on a fix for it. Otherwise I would try to work around this behaviour.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

codingjoecommented, Mar 6, 2021

I don’t want to just revert the commit, since it added tests, which are great even if they test for the wrong thing. I will create a patch really quick.

codingjoecommented, Mar 8, 2021

yes 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

List Search behaviour? - ServiceNow Community
Mahe, Here are the ways you can do a "contains" search: Use wildcards to search column CONTAINS query; Set the property glide.ui.
Read more >
MongoDB Atlas Search - Multiple terms in search-string with ...
Update Currently, I have solved it by splitting the search-term on space (' ') and adding each individual keyword to a separate must-sub-clause ......
Read more >
42 Elasticsearch Query Examples - Tutorial - Coralogix
The default behavior of the OR operator being applied to multi-word searches can be changed using the “operator” parameter passed along with ...
Read more >
3 Oracle Text CONTAINS Query Operators
Use the AND operator to search for documents that contain at least one occurrence of each of the query terms. The AND operator...
Read more >
SQL Queries - EF Core - Microsoft Learn
Basic SQL queries; Passing parameters; Composing with LINQ; Change Tracking; Querying scalar (non-entity) types; Executing non-querying SQL ...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found