Skip to content

Commit

Permalink
feat: add relevance query index command
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Nov 13, 2024
1 parent 800db76 commit 4ff0e00
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
22 changes: 21 additions & 1 deletion ckanext/search_tweaks/query_relevance/cli.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from __future__ import annotations

import csv
import datetime

import logging
import click
import freezegun

import ckan.model as model
from ckan.lib.redis import connect_to_redis
from ckan.lib.search import rebuild

from . import QueryScore

_search_csv_headers = ["package_id", "search_query", "count_of_hits"]

log = logging.getLogger(__name__)


@click.group(short_help="Manage search relevance")
def query():
Expand Down Expand Up @@ -86,3 +91,18 @@ def safe_export(ctx, days, file):
fg="red",
)
ctx.invoke(import_source, source=click.File()(file))


@query.command()
def index():
"""Re-index datasets that have query relevance scores.
"""

storage = QueryScore.default_storage_class()
ids = {id for id, _, _ in storage.scan()}
with click.progressbar(ids) as bar:
for id in bar:
try:
rebuild(id)
except Exception:
log.exception("Cannot index %s", id)
14 changes: 2 additions & 12 deletions ckanext/search_tweaks/query_relevance/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,14 @@ def read(self, entity):
# ISearchTweaks

def get_search_boost_fn(self, search_params: dict[str, Any]) -> str | None:
if feature_disabled("query_boost", search_params):
return None

prefix = tk.config.get(CONFIG_RELEVANCE_PREFIX, DEFAULT_RELEVANCE_PREFIX)
disabled = tk.asbool(
search_params.get("extras", {}).get(
"ext_search_tweaks_disable_relevance",
False,
),
)

if not search_params.get("q") or disabled:
if feature_disabled("query_boost", search_params) or not search_params.get("q"):
return None

normalized = normalize_query(search_params["q"]).replace(" ", "_")
if not normalized:
return None

prefix = tk.config.get(CONFIG_RELEVANCE_PREFIX, DEFAULT_RELEVANCE_PREFIX)
field = prefix + normalized
boost_string = Template(
tk.config.get(CONFIG_BOOST_STRING, DEFAULT_BOOST_STRING),
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "ckanext-search-tweaks"
version = "0.6.2"
version = "0.6.3.a0"
description = ""
classifiers = [ "Development Status :: 4 - Beta", "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10",]
keywords = [ "CKAN",]
Expand Down

0 comments on commit 4ff0e00

Please sign in to comment.