Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Remove unused simple query string features (#3360)
Olga Bulatb35ce3fe5291
diff --git a/api/api/controllers/search_controller.py b/api/api/controllers/search_controller.pyindex 1a5a4eb62..cf5f76a04 100644--- a/api/api/controllers/search_controller.py+++ b/api/api/controllers/search_controller.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport loggingimport logging as log+import refrom math import ceilfrom typing import TYPE_CHECKING@@ -58,6 +59,13 @@ PROVIDER = "provider"QUERY_SPECIAL_CHARACTER_ERROR = "Unescaped special characters are not allowed."DEFAULT_BOOST = 10000DEFAULT_SEARCH_FIELDS = ["title", "description", "tags.name"]+DEFAULT_SQS_FLAGS = "AND|NOT|PHRASE|WHITESPACE"+UNUSED_SQS_FLAGS = [+ ("PRECEDENCE", r"\(.*\)"),+ ("ESCAPE", r"\\"),+ ("FUZZY|SLOP", r"~\d"),+ ("PREFIX", r"\*"),+]def _quote_escape(query_string):@@ -287,8 +295,11 @@ def build_search_query(# individual field-level queries specified.if "q" in search_params.data:query = _quote_escape(search_params.data["q"])+ log_query_features(query, query_name="q")+base_query_kwargs = {"query": query,+ "flags": DEFAULT_SQS_FLAGS,"fields": DEFAULT_SEARCH_FIELDS,"default_operator": "AND",}@@ -301,6 +312,7 @@ def build_search_query(quotes_stripped = query.replace('"', "")exact_match_boost = Q("simple_query_string",+ flags=DEFAULT_SQS_FLAGS,fields=["title"],query=f"{quotes_stripped}",boost=10000,@@ -313,9 +325,11 @@ def build_search_query(("tags", "tags.name"),]:if field_value := search_params.data.get(field):+ log_query_features(field_value, query_name="field")search_queries["must"].append(Q("simple_query_string",+ flags=DEFAULT_SQS_FLAGS,query=_quote_escape(field_value),fields=[field_name],)@@ -339,6 +353,22 @@ def build_search_query()+def log_query_features(query: str, query_name) -> None:+ query_flags = []+ for flag, pattern in UNUSED_SQS_FLAGS:+ if bool(re.search(pattern, query)):+ query_flags.append(flag)+ if query_flags:+ log.info(+ {+ "log_message": "Special features present in query",+ "query_name": query_name,+ "query": query,+ "flags": query_flags,+ }+ )++def build_collection_query(search_params: MediaListRequestSerializer,collection_params: dict[str, str],diff --git a/api/test/unit/controllers/test_search_controller_search_query.py b/api/test/unit/controllers/test_search_controller_search_query.pyindex c327a8223..9b574cd68 100644--- a/api/test/unit/controllers/test_search_controller_search_query.py+++ b/api/test/unit/controllers/test_search_controller_search_query.py@@ -5,6 +5,7 @@ from elasticsearch_dsl import Qfrom api.controllers import search_controllerfrom api.controllers.search_controller import (+ DEFAULT_SQS_FLAGS,FILTERED_PROVIDERS_CACHE_KEY,FILTERED_PROVIDERS_CACHE_VERSION,)@@ -71,6 +72,7 @@ def test_create_search_query_q_search_no_filters(media_type_config):"default_operator": "AND","fields": ["title", "description", "tags.name"],"query": "cat",+ "flags": DEFAULT_SQS_FLAGS,}}],@@ -80,6 +82,7 @@ def test_create_search_query_q_search_no_filters(media_type_config):"boost": 10000,"fields": ["title"],"query": "cat",+ "flags": DEFAULT_SQS_FLAGS,}},{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}},@@ -87,7 +90,7 @@ def test_create_search_query_q_search_no_filters(media_type_config):}-def test_create_search_query_q_search_with_quotes_adds_exact_suffix(media_type_config):+def test_create_search_query_q_search_with_quotes_adds_raw_suffix(media_type_config):serializer = media_type_config.search_request_serializer(data={"q": '"The cutest cat"'})@@ -104,6 +107,7 @@ def test_create_search_query_q_search_with_quotes_adds_exact_suffix(media_type_c"fields": ["title", "description", "tags.name"],"query": '"The cutest cat"',"quote_field_suffix": ".raw",+ "flags": DEFAULT_SQS_FLAGS,}}],@@ -113,6 +117,7 @@ def test_create_search_query_q_search_with_quotes_adds_exact_suffix(media_type_c"boost": 10000,"fields": ["title"],"query": "The cutest cat",+ "flags": DEFAULT_SQS_FLAGS,}},{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}},@@ -152,6 +157,7 @@ def test_create_search_query_q_search_with_filters(image_media_type_config):"default_operator": "AND","fields": ["title", "description", "tags.name"],"query": "cat",+ "flags": DEFAULT_SQS_FLAGS,}}],@@ -161,6 +167,7 @@ def test_create_search_query_q_search_with_filters(image_media_type_config):"boost": 10000,"fields": ["title"],"query": "cat",+ "flags": DEFAULT_SQS_FLAGS,}},{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}},@@ -188,10 +195,23 @@ def test_create_search_query_non_q_query(image_media_type_config):"simple_query_string": {"fields": ["creator"],"query": "Artist From Openverse",+ "flags": DEFAULT_SQS_FLAGS,+ }+ },+ {+ "simple_query_string": {+ "fields": ["title"],+ "query": "kitten🐱",+ "flags": DEFAULT_SQS_FLAGS,+ }+ },+ {+ "simple_query_string": {+ "fields": ["tags.name"],+ "query": "cute",+ "flags": DEFAULT_SQS_FLAGS,}},- {"simple_query_string": {"fields": ["title"], "query": "kitten🐱"}},- {"simple_query_string": {"fields": ["tags.name"], "query": "cute"}},],"should": [{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}},diff --git a/frontend/src/locales/scripts/en.json5 b/frontend/src/locales/scripts/en.json5index deaf2a6b8..494d33b9f 100644--- a/frontend/src/locales/scripts/en.json5+++ b/frontend/src/locales/scripts/en.json5@@ -177,70 +177,14 @@title: "Search for an exact match",ariaLabel: "quote unquote Claude Monet",claudeMonet: '"Claude Monet"',- content: "Put a word or phrase inside quotes. For example, {link}.",- },- combine: {- title: "Combining terms",- description: "If you want to combine terms, you can use the following operators to perform more complex queries",- and: "{symbol} signifies AND operation",- or: "{symbol} signifies OR operation",- not: "{symbol} negates a single token",- prefix: "{symbol} at the end of a term signifies a prefix query",- precedence: "{symbol} signify precedence",- fuzziness: "{symbol} after a word signifies edit distance (fuzziness)",- ariaLabels: {- fuzziness: "tilde N",- open: "open parenthesis",- close: "close parenthesis",- star: "star symbol",- not: "minus symbol",- and: "plus symbol",- or: "vertical bar symbol",- prefix: "asterisk symbol",- verticalBar: "vertical bar symbol",- precedence: "parentheses",- },- },- example: {- and: {- description: "Example: {link}{br} This will search for images related to both dog and cat.",- ariaLabel: "dog plus cat",- example: "dog+cat",- },- or: {- description: "Example: {link}{br} This will search for images related to dog or cat, but not necessarily both.",- ariaLabel: "dog vertical bar cat",- example: "dog|cat",- },- negate: {- description: "You can use the {operator} to exclude a search term from the results.",- operatorName: "operator (signifies NOT)",- operatorAriaLabel: "minus operator (signifies NOT)",- ariaLabel: "dog minus pug",- example: "dog -pug",- content: "Example: {link}{br} This will search for images related to dog but won't include results related to 'pug'",- },- prefix: {- description: "You can use the {operatorName} to mark a prefix term. This will match anything after the *.",- operatorName: "operator (wildcard)",- operatorAriaLabel: "star operator (wildcard)",- ariaLabel: "net star symbol",- example: "net*",- content: "Example: {link}{br} This will search for images matching anything with 'net'. This might include 'network', 'Netflix', 'Netherlands', etc.",- },- precedence: {- description: "You can use parentheses {highlight} to specify precedence of terms or combine more complex queries.",- ariaLabel: "dogs plus open parenthesis corgis vertical bar labrador close parenthesis",- example: "dogs + (corgis | labrador)",- content: "Example: {link}{br} This will search for images that match dogs that are either corgis or labrador.",- },- fuzziness: {- description: "You can use {highlight} to specify some fuzzy logic to the term according to the {link} — the number of one character changes that need to be made to one string to make it the same as another string.",- linkText: "Levenshtein Edit Distance",- ariaLabel: "theatre tilde 1",- example: "theatre~1",- content: "Example: {link}{br} This will search for images that match strings close to the term 'theatre' with a difference of one character. Results might include terms with different spellings like 'theater'.",- },+ content: "To search for an exact word or phrase, put it inside quotes. For example, {link}.",+ },+ negate: {+ title: "Excluding terms",+ operatorName: "minus operator",+ ariaLabel: "dog minus pug",+ example: "dog -pug",+ content: 'To exclude a term from your results, put the {operator} in front of it. Example: {link}{br} This will search for media related to "dog" but won\'t include results related to "pug".',},},feedback: {diff --git a/frontend/src/pages/search-help.vue b/frontend/src/pages/search-help.vueindex e013d1ff2..8b43607ee 100644--- a/frontend/src/pages/search-help.vue+++ b/frontend/src/pages/search-help.vue@@ -15,145 +15,28 @@</template></i18n>- <h2>{{ $t("searchGuide.combine.title") }}</h2>+ <h2>{{ $t("searchGuide.negate.title") }}</h2>- <p>{{ $t("searchGuide.combine.description") }}</p>- <ul class="not-prose list-inside list-disc marker:text-dark-charcoal-20">- <i18n- v-for="[name, operator] in Object.entries(operators)"- :key="name"- :path="`searchGuide.combine.${name}`"- tag="li"- >- <template #symbol>- <code- :aria-label="- $t(`searchGuide.combine.ariaLabels.${name}`).toString()- "- >{{ operator.symbol }}</code- >- </template>- </i18n>- </ul>- <!-- eslint-disable @intlify/vue-i18n/no-raw-text -->- <i18n path="searchGuide.example.and.description" tag="p">- <template #link>- <VLink- :aria-label="$t('searchGuide.example.and.ariaLabel').toString()"- :href="pathFromQuery('dog+cat')"- >- <em>{{ $t("searchGuide.example.and.example") }}</em>- </VLink>- </template>- <template #br><br /></template>- </i18n>-- <i18n path="searchGuide.example.or.description" tag="p">- <template #link>- <VLink- :aria-label="$t('searchGuide.example.or.ariaLabel')"- :href="pathFromQuery('dog|cat')"- >- <em>{{ $t("searchGuide.example.or.example").toString() }}</em>- </VLink>- </template>- <template #br>- <br />- </template>- </i18n>-- <i18n path="searchGuide.example.negate.description" tag="p" class="mt-4">- <template #operator>- <em- :aria-label="- $t('searchGuide.example.negate.operatorAriaLabel').toString()- "- >- {{ $t("searchGuide.example.negate.operatorName").toString() }}</em+ <i18n path="searchGuide.negate.content" tag="p" class="mb-4">+ <template #operator+ ><!-- eslint-disable @intlify/vue-i18n/no-raw-text -->+ <em :aria-label="$t('searchGuide.negate.operatorAriaLabel').toString()"+ >- {{ $t("searchGuide.negate.operatorName").toString() }}</em>+ <!-- eslint-enable @intlify/vue-i18n/no-raw-text --></template>- </i18n>-- <i18n path="searchGuide.example.negate.content" tag="p" class="mb-4"><template #link><VLink- :aria-label="$t('searchGuide.example.negate.ariaLabel')"+ :aria-label="$t('searchGuide.negate.ariaLabel')":href="pathFromQuery('dog -pug')">- <em>{{ $t("searchGuide.example.negate.example") }}</em>- </VLink>- </template>- <template #br>- <br />- </template>- </i18n>-- <i18n path="searchGuide.example.prefix.description" tag="p" class="mt-4">- <template #operator>- <em :aria-label="$t('searchGuide.example.prefix.ariaLabel').toString()"- >* {{ $t("searchGuide.example.prefix.operatorName").toString() }}</em- >- </template>- </i18n>-- <i18n path="searchGuide.example.prefix.content" tag="p" class="mb-4">- <template #link>- <VLink- :aria-label="$t('searchGuide.example.prefix.ariaLabel')"- :href="pathFromQuery('net*')"- >- <em>{{ $t("searchGuide.example.prefix.example") }}</em>- </VLink>- </template>- <template #br>- <br />- </template>- </i18n>-- <i18n path="searchGuide.example.precedence.description" tag="p">- <template #highlight>- <em>( and )</em>- </template>- </i18n>-- <i18n path="searchGuide.example.precedence.content" tag="p" class="mb-4">- <template #link>- <VLink- :aria-label="$t('searchGuide.example.precedence.ariaLabel')"- :href="pathFromQuery('dog (corgis | labrador)')"- >- <em>{{ $t("searchGuide.example.precedence.example") }}</em>- </VLink>- </template>- <template #br>- <br />- </template>- </i18n>-- <i18n path="searchGuide.example.fuzziness.description" tag="p">- <template #highlight>- <em aria-label="tilde N">~N</em>- </template>- <template #link>- <VLink href="https://en.wikipedia.org/wiki/Levenshtein_distance">- {{ $t("searchGuide.example.fuzziness.linkText") }}- </VLink>- </template>- </i18n>-- <i18n path="searchGuide.example.fuzziness.content" tag="p">- <template #link>- <VLink- :aria-label="$t('searchGuide.example.fuzziness.ariaLabel')"- :href="pathFromQuery('theatre~1')"- >- <em>{{ $t("searchGuide.example.fuzziness.example") }}</em>+ <em>{{ $t("searchGuide.negate.example") }}</em></VLink></template><template #br><br /></template></i18n>- <!-- eslint-enable @intlify/vue-i18n/no-raw-text --></VContentPage></template>@@ -167,19 +50,6 @@ import { useI18n } from "~/composables/use-i18n"import VLink from "~/components/VLink.vue"import VContentPage from "~/components/VContentPage.vue"-const operators = {- and: { symbol: "+", query: "dog+cat" },- not: { symbol: "-", query: "dog-cat" },- or: { symbol: "|", query: "dog|cat" },- prefix: { symbol: "*", query: "net*" },- precedence: { symbol: "()", query: "dogs+(corgis|labrador)" },- fuzziness: {- symbol: "~",- query: "theatre~1",- reference: "https://en.wikipedia.org/wiki/Levenshtein_distance",- },-}-export default defineComponent({name: "VSearchHelpPage",components: { VLink, VContentPage },@@ -200,7 +70,7 @@ export default defineComponent({query: { q: queryString },})}- return { pathFromQuery, operators }+ return { pathFromQuery }},head: {},})diff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-2xl-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-2xl-linux.pngindex 579c8b078..257062ec2 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-2xl-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-2xl-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-lg-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-lg-linux.pngindex 7cd94af32..2fdcbd3cd 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-lg-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-lg-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-md-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-md-linux.pngindex c663a3de4..0f799ee29 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-md-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-md-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-sm-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-sm-linux.pngindex bb0c78ced..18e0b01cb 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-sm-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-sm-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-xl-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-xl-linux.pngindex 1e6382675..12cdcaf8d 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-xl-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-xl-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-xs-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-xs-linux.pngindex 447b932dd..339f35a34 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-xs-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-ltr-xs-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-2xl-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-2xl-linux.pngindex 3e1f9995c..ad15a31a4 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-2xl-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-2xl-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-lg-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-lg-linux.pngindex ec57cdc18..e88594445 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-lg-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-lg-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-md-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-md-linux.pngindex ad8e6aa17..f510853e5 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-md-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-md-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-sm-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-sm-linux.pngindex 166c35ee1..7a8456e1a 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-sm-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-sm-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-xl-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-xl-linux.pngindex 52b248067..f4ca1acf5 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-xl-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-xl-linux.png differdiff --git a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-xs-linux.png b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-xs-linux.pngindex f3d85fa17..89b661b0a 100644Binary files a/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-xs-linux.png and b/frontend/test/playwright/visual-regression/pages/pages.spec.ts-snapshots/search-help-rtl-xs-linux.png differ