Skip to content

Commit d410686

Browse files
committed
remove solr5555 from search engine code
Remove one layer of abstraction by letting Solr9PySolrSearchEngine inherit from SearchEngineBase directly
1 parent a76f9aa commit d410686

32 files changed

+1048
-4888
lines changed

freesound/settings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@
609609
SEARCH_FORUM_SORT_DEFAULT = SEARCH_FORUM_SORT_OPTION_THREAD_DATE_FIRST
610610

611611
SEARCH_ENGINE_BACKEND_CLASS = 'utils.search.backends.solr9pysolr.Solr9PySolrSearchEngine'
612-
SOLR5_BASE_URL = "http://search:8983/solr"
613612
SOLR9_BASE_URL = "http://search:8983/solr"
614613

615614
SEARCH_ENGINE_SIMILARITY_ANALYZERS = {

freesound/test_settings.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
from .logger import LOGGING
4242
LOGGING['handlers']['stdout']['class'] = 'logging.NullHandler'
4343

44-
SOLR5_SOUNDS_URL = "http://fakehost:8080/fs2/" # Avoid making accidental queries to "real" search server if running
45-
SOLR5_FORUM_URL = "http://fakehost:8080/forum/" # Avoid making accidental requests to "real" search server if running
46-
SEARCH_ENGINE_BACKEND_CLASS = 'utils.search.backends.solr555pysolr.Solr555PySolrSearchEngine' # Test with our own custom search engine functions
4744
SIMILARITY_ADDRESS = 'fakehost' # Avoid making accidental requests to "real" similarity server if running
4845
TAGRECOMMENDATION_ADDRESS = 'fakehost' # Avoid making accidental requests to "real" tag rec server if running
4946

requirements.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ psycopg2-binary==2.9.6
4444
PyJWT==2.6.0
4545
pyparsing==2.4.7
4646
pysndfile==1.4.4
47-
pysolr==3.10.0b1
47+
pysolr==3.10.0
4848
python-louvain==0.16 # community detection in clustering
4949
pytest~=8.0.2
5050
pytest-django~=4.8.0

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ pyproject-hooks==1.0.0
268268
# via build
269269
pysndfile==1.4.4
270270
# via -r requirements.in
271-
pysolr==3.10.0b1
271+
pysolr==3.10.0
272272
# via -r requirements.in
273273
pytest==8.0.2
274274
# via

search/management/commands/test_search_engine_backend.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,8 @@
3838
def core_exists(solr_base_url, core_name):
3939
r = requests.get(f'{solr_base_url}/admin/cores?action=STATUS&core={core_name}')
4040
r.raise_for_status()
41-
try:
42-
status = r.json()
43-
return status['status'][core_name] != {}
44-
except ValueError:
45-
# Solr 5 returns xml. "Empty list" means that the core does not exist
46-
return f"""<lst name="{core_name}"/></lst>""" not in r.text
41+
status = r.json()
42+
return status['status'][core_name] != {}
4743

4844

4945
def create_core(solr_base_url, core_name, configSet, delete_core=False):

search/templatetags/search.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
from django.conf import settings
2424

2525
from sounds.models import License
26-
from utils.search import search_query_processor_options
27-
from utils.search.backends.solr555pysolr import FIELD_NAMES_MAP
26+
from utils.search import search_query_processor_options, FIELD_NAMES_MAP
2827
from utils.tags import annotate_tags
2928

3029
register = template.Library()

utils/search/__init__.py

Lines changed: 87 additions & 233 deletions
Large diffs are not rendered by default.

utils/search/backends/__init__.py

Lines changed: 912 additions & 0 deletions
Large diffs are not rendered by default.

utils/search/backends/solr555pysolr.py

Lines changed: 0 additions & 824 deletions
This file was deleted.

utils/search/backends/solr9pysolr.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
31
#
42
# Freesound is (c) MUSIC TECHNOLOGY GROUP, UNIVERSITAT POMPEU FABRA
53
#
@@ -24,41 +22,46 @@
2422
import pysolr
2523
from django.conf import settings
2624

27-
from utils.search.backends import solr555pysolr
25+
from utils.search.backends import SearchEngineBase
26+
from utils.search.backends.solr_common import FreesoundSoundJsonEncoder, SolrResponseInterpreter
2827

2928
SOLR_FORUM_URL = f"{settings.SOLR9_BASE_URL}/forum"
3029
SOLR_SOUNDS_URL = f"{settings.SOLR9_BASE_URL}/freesound"
3130

3231

33-
class Solr9PySolrSearchEngine(solr555pysolr.Solr555PySolrSearchEngine):
32+
class Solr9PySolrSearchEngine(SearchEngineBase):
33+
3434
def __init__(self, sounds_index_url=None, forum_index_url=None):
3535
if sounds_index_url is None:
3636
sounds_index_url = SOLR_SOUNDS_URL
3737
if forum_index_url is None:
3838
forum_index_url = SOLR_FORUM_URL
3939
self.sounds_index_url = sounds_index_url
4040
self.forum_index_url = forum_index_url
41+
self._sounds_index = None
42+
self._forum_index = None
4143

42-
43-
def get_sounds_index(self):
44-
if self.sounds_index is None:
45-
self.sounds_index = pysolr.Solr(
44+
@property
45+
def sounds_index(self):
46+
if self._sounds_index is None:
47+
self._sounds_index = pysolr.Solr(
4648
self.sounds_index_url,
47-
encoder=solr555pysolr.FreesoundSoundJsonEncoder(),
48-
results_cls=solr555pysolr.SolrResponseInterpreter,
49+
encoder=FreesoundSoundJsonEncoder(),
50+
results_cls=SolrResponseInterpreter,
4951
always_commit=True
5052
)
51-
return self.sounds_index
53+
return self._sounds_index
5254

53-
def get_forum_index(self):
54-
if self.forum_index is None:
55-
self.forum_index = pysolr.Solr(
55+
@property
56+
def forum_index(self):
57+
if self._forum_index is None:
58+
self._forum_index = pysolr.Solr(
5659
self.forum_index_url,
57-
encoder=solr555pysolr.FreesoundSoundJsonEncoder(),
58-
results_cls=solr555pysolr.SolrResponseInterpreter,
60+
encoder=FreesoundSoundJsonEncoder(),
61+
results_cls=SolrResponseInterpreter,
5962
always_commit=True
6063
)
61-
return self.forum_index
64+
return self._forum_index
6265

6366

6467
def search_process_filter(self, query_filter, only_sounds_within_ids=False, only_sounds_with_pack=False):
@@ -92,23 +95,23 @@ def search_process_filter(self, query_filter, only_sounds_within_ids=False, only
9295
query_filter = query_filter.replace('created:', 'created_range:')
9396

9497
# If we only want sounds with packs and there is no pack filter, add one
95-
if only_sounds_with_pack and not 'pack:' in query_filter:
98+
if only_sounds_with_pack and 'pack:' not in query_filter:
9699
query_filter += ' pack:*'
97100

98101
if 'geotag:"Intersects(' in query_filter:
99102
# Replace geotag:"Intersects(<MINIMUM_LONGITUDE> <MINIMUM_LATITUDE> <MAXIMUM_LONGITUDE> <MAXIMUM_LATITUDE>)"
100103
# with geotag:["<MINIMUM_LATITUDE>, <MINIMUM_LONGITUDE>" TO "<MAXIMUM_LONGITUDE> <MAXIMUM_LATITUDE>"]
101-
query_filter = re.sub('geotag:"Intersects\((.+?) (.+?) (.+?) (.+?)\)"', r'geotag:["\2,\1" TO "\4,\3"]', query_filter)
104+
query_filter = re.sub(r'geotag:"Intersects\((.+?) (.+?) (.+?) (.+?)\)"', r'geotag:["\2,\1" TO "\4,\3"]', query_filter)
102105

103106
query_filter = self.search_filter_make_intersection(query_filter)
104107

105108
# When calculating results form clustering, the "only_sounds_within_ids" argument is passed and we filter
106109
# our query to the sounds in that list of IDs.
107110
if only_sounds_within_ids:
108-
sounds_within_ids_filter = ' OR '.join(['id:{}'.format(sound_id) for sound_id in only_sounds_within_ids])
111+
sounds_within_ids_filter = ' OR '.join([f'id:{sound_id}' for sound_id in only_sounds_within_ids])
109112
if query_filter:
110-
query_filter += ' AND ({})'.format(sounds_within_ids_filter)
113+
query_filter += f' AND ({sounds_within_ids_filter})'
111114
else:
112-
query_filter = '({})'.format(sounds_within_ids_filter)
115+
query_filter = f'({sounds_within_ids_filter})'
113116

114-
return query_filter
117+
return query_filter

utils/search/backends/solr_common.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
# Bram de Jong
1919
#
2020
import json
21-
import urllib.request, urllib.parse, urllib.error
21+
from datetime import date, datetime
22+
import urllib.request
23+
import urllib.parse
24+
import urllib.error
2225

2326
from utils.search import SearchEngineException
2427

@@ -324,3 +327,13 @@ def __init__(self, response, next_page_query=None):
324327
self.highlighting = response["highlighting"]
325328
except KeyError:
326329
self.highlighting = {}
330+
331+
332+
class FreesoundSoundJsonEncoder(json.JSONEncoder):
333+
def default(self, value):
334+
if isinstance(value, datetime):
335+
return value.strftime('%Y-%m-%dT%H:%M:%S.000Z')
336+
elif isinstance(value, date):
337+
return value.strftime('%Y-%m-%dT00:00:00.000Z')
338+
339+
return json.JSONEncoder.default(self, value)

utils/search/backends/tests/test_solr555pysolr.py

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from django.test import TestCase
22

3-
from utils.search.backends import solr555pysolr
3+
from utils.search.backends import solr9pysolr
44

55
class SolrCommonTest(TestCase):
66
def test_search_filter_make_intersection(self):
77

88
filter_query = "username:alastairp"
9-
updated = solr555pysolr.Solr555PySolrSearchEngine().search_filter_make_intersection(filter_query)
9+
updated = solr9pysolr.Solr9PySolrSearchEngine().search_filter_make_intersection(filter_query)
1010
self.assertEqual(updated, "+username:alastairp")
1111

1212
filter_query = "username:alastairp license:(a OR b)"
13-
updated = solr555pysolr.Solr555PySolrSearchEngine().search_filter_make_intersection(filter_query)
13+
updated = solr9pysolr.Solr9PySolrSearchEngine().search_filter_make_intersection(filter_query)
1414
self.assertEqual(updated, "+username:alastairp +license:(a OR b)")

utils/search/search_query_processor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
from utils.clustering_utilities import get_ids_in_cluster, get_clusters_for_query
3333
from utils.encryption import create_hash
34-
from utils.search.backends.solr555pysolr import Solr555PySolrSearchEngine
3534
from utils.search.search_sounds import allow_beta_search_features
3635
from .search_query_processor_options import SearchOptionStr, SearchOptionChoice, \
3736
SearchOptionInt, SearchOptionBool, SearchOptionRange, SearchOptionMultipleChoice, \

utils/search/search_sounds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def get_sound_similarity_from_search_engine_query(query_params, analyzer_name=se
175175

176176
# Update query params to get similarity vectors of the first
177177
config_options = settings.SEARCH_ENGINE_SIMILARITY_ANALYZERS[analyzer_name]
178-
vector_field_name = utils.search.backends.solr555pysolr.get_solr_dense_vector_search_field_name(config_options['vector_size'], config_options('l2_norm', False))
178+
vector_field_name = utils.search.get_solr_dense_vector_search_field_name(config_options['vector_size'], config_options('l2_norm', False))
179179
query_params.update({
180180
'facets': None,
181181
'current_page': current_page if current_page is not None else query_params['current_page'],

utils/search/solr5.5.5/cores/forum/conf/currency.xml

Lines changed: 0 additions & 67 deletions
This file was deleted.

utils/search/solr5.5.5/cores/forum/conf/elevate.xml

Lines changed: 0 additions & 1 deletion
This file was deleted.

utils/search/solr5.5.5/cores/forum/conf/lang/stopwords_en.txt

Lines changed: 0 additions & 54 deletions
This file was deleted.

utils/search/solr5.5.5/cores/forum/conf/params.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)