Skip to content

Commit

Permalink
Merge pull request #139 from openedx/ashultz0/detailed-elastic-params
Browse files Browse the repository at this point in the history
Log detailed elasticsearch params
  • Loading branch information
ashultz0 authored Nov 15, 2023
2 parents 6ffa7bf + 7b4df58 commit 93939ba
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 13 deletions.
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.PHONY: clean quality requirements validate test test-python quality-python
.PHONY: clean quality requirements validate test test-with-es quality-python install-local

clean:
find . -name '__pycache__' -exec rm -rf {} +
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +
coverage erase
rm -rf coverage htmlcov
rm -fr build/
Expand Down Expand Up @@ -51,7 +51,8 @@ upgrade: ## update the requirements/*.txt files with the latest packages satisfy
sed '/^[dD]jango==/d' requirements/testing.txt > requirements/testing.tmp
mv requirements/testing.tmp requirements/testing.txt

test-python: clean ## run tests using pytest and generate coverage report
pytest
test: test_with_es ## run tests and generate coverage report

test: test-python ## run tests and generate coverage report
install-local: ## installs your local edx-search into the LMS and CMS python virtualenvs
docker exec -t edx.devstack.lms bash -c '. /edx/app/edxapp/venvs/edxapp/bin/activate && cd /edx/app/edxapp/edx-platform && pip uninstall -y edx-search && pip install -e /edx/src/edx-search && pip freeze | grep edx-search'
docker exec -t edx.devstack.cms bash -c '. /edx/app/edxapp/venvs/edxapp/bin/activate && cd /edx/app/edxapp/edx-platform && pip uninstall -y edx-search && pip install -e /edx/src/edx-search && pip freeze | grep edx-search'
2 changes: 1 addition & 1 deletion edxsearch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
""" Container module for testing / demoing search """

__version__ = '3.6.0'
__version__ = '3.7.0'
8 changes: 5 additions & 3 deletions search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class NoSearchEngineError(Exception):
"""


def perform_search(
def perform_course_search(
search_term,
user=None,
size=10,
Expand All @@ -62,14 +62,16 @@ def perform_search(
)
if not searcher:
raise NoSearchEngineError("No search engine specified in settings.SEARCH_ENGINE")
log_search_params = getattr(settings, "SEARCH_COURSEWARE_CONTENT_LOG_PARAMS", False)

results = searcher.search_string(
search_term,
results = searcher.search(
query_string=search_term,
field_dictionary=field_dictionary,
filter_dictionary=filter_dictionary,
exclude_dictionary=exclude_dictionary,
size=size,
from_=from_,
log_search_params=log_search_params,
)

# post-process the result
Expand Down
4 changes: 4 additions & 0 deletions search/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ def search(self,
aggregation_terms=None,
exclude_ids=None,
use_field_match=False,
log_search_params=False,
**kwargs): # pylint: disable=arguments-differ, unused-argument
"""
Implements call to search the index for the desired content.
Expand Down Expand Up @@ -653,6 +654,9 @@ def search(self,
if aggregation_terms:
body["aggs"] = _process_aggregation_terms(aggregation_terms)

if log_search_params:
log.info(f"full elastic search body {body}")

try:
es_response = self._es.search(index=self._prefixed_index_name, body=body, **kwargs)
except exceptions.ElasticsearchException as ex:
Expand Down
1 change: 1 addition & 0 deletions search/search_engine_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def search(self,
filter_dictionary=None,
exclude_dictionary=None,
aggregation_terms=None,
log_search_params=False,
**kwargs): # pylint: disable=too-many-arguments
"""
Search for matching documents within the search index.
Expand Down
1 change: 1 addition & 0 deletions search/tests/mock_search_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def search(self,
filter_dictionary=None,
exclude_dictionary=None,
aggregation_terms=None,
log_search_params=False,
**kwargs): # pylint: disable=too-many-arguments
"""
Perform search upon documents within index.
Expand Down
4 changes: 2 additions & 2 deletions search/tests/test_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.test.utils import override_settings
from elasticsearch import exceptions
from elasticsearch.helpers import BulkIndexError
from search.api import NoSearchEngineError, perform_search
from search.api import NoSearchEngineError, perform_course_search
from search.elastic import RESERVED_CHARACTERS
from search.tests.mock_search_engine import (MockSearchEngine,
json_date_to_datetime)
Expand Down Expand Up @@ -238,7 +238,7 @@ class TestNone(TestCase):
def test_perform_search(self):
""" search opertaion should yeild an exception with no search engine """
with self.assertRaises(NoSearchEngineError):
perform_search("abc test")
perform_course_search("abc test")


@override_settings(SEARCH_ENGINE="search.elastic.ElasticSearchEngine")
Expand Down
9 changes: 9 additions & 0 deletions search/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ def test_find_string(self):
response = self.searcher.search_string(test_string)
self.assertEqual(response["total"], 3)

def test_log_params(self):
""" Test that if you turn on detailed logging, search doesn't explode. """
test_string = "A test string"
self.searcher.index([{"content": {"name": test_string}}])

# search string
response = self.searcher.search(query_string=test_string, log_search_params=True)
self.assertEqual(response["total"], 1)

def test_field(self):
""" test matching on a field """
test_string = "A test string"
Expand Down
4 changes: 2 additions & 2 deletions search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.views.decorators.http import require_POST

from eventtracking import tracker as track
from .api import perform_search, course_discovery_search, course_discovery_filter_fields
from .api import perform_course_search, course_discovery_search, course_discovery_filter_fields
from .initializer import SearchInitializer

# log appears to be standard name used for logger
Expand Down Expand Up @@ -96,7 +96,7 @@ def do_search(request, course_id=None):
}
)

results = perform_search(
results = perform_course_search(
search_term,
user=request.user,
size=size,
Expand Down

0 comments on commit 93939ba

Please sign in to comment.