Repository navigation
[fix] return results for a query made only of negated terms - #806
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A negated field filter on its own returns every other document:
-title:golanglists everything whose title doesn't contain golang. A negated plain term on its own returns nothing, and neither does a query made only of negated terms:-title:golang-golang-"golang language"-golang -pythonlanguage -golang(These come from three indexed pages: go.dev with "golang language", rust-lang.org with "rust language" and python.org with "python language".)
Bleve handles a boolean query that only has must-not clauses fine; it adds a match-all as the required part.
querybuilder.Buildadds a required clause in two places, though:golang(or with-golang, since the raw token is used) can match.Disjunction(Conjunction(positive terms...), phrase(full query)). When every term is negated, the conjunction is empty, so the whole disjunction depends on the phrase-golang -pythonmatching.The fix only adds each of those when there's a positive term to go with it (
len(qs) > 0/len(qs) == 1). Queries with at least one positive term build exactly the same query as before.Tests:
TestSearchOnlyNegatedTermsinserver/indexer/indexer_test.goindexes the three pages above and checks the table. Onmainit fails for-golang,-"golang language"and-golang -python.Test_build_negated_wordnow also checks that-golanggets no should clause.go test -race ./...passes (with a placeholderserver/static/appfor the embed), andgolangci-lint run ./server/indexer/...reports 0 issues.