Skip to content

Commit

Permalink
use get_current_index_name to find name of index
Browse files Browse the repository at this point in the history
  • Loading branch information
BeritJanssen committed Jan 16, 2025
1 parent cd4999f commit 7460144
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
10 changes: 8 additions & 2 deletions backend/addcorpus/permissions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from rest_framework import permissions
from rest_framework.exceptions import NotFound
from rest_framework.request import Request
from addcorpus.models import Corpus
from addcorpus.models import Corpus, CorpusConfiguration

def corpus_name_from_request(request):

def corpus_name_from_request(request: Request):
'''
Extract the corpus name from a request
'''
Expand All @@ -24,6 +25,11 @@ def corpus_name_from_request(request):
return corpus


def corpus_config_from_request(request: Request) -> CorpusConfiguration:
corpus_name = corpus_name_from_request(request)
return CorpusConfiguration.objects.get(corpus__name=corpus_name)


class CanSearchCorpus(permissions.BasePermission):
message = 'You do not have permission to access this corpus'

Expand Down
2 changes: 1 addition & 1 deletion backend/es/es_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def alias(corpus: Corpus, clean=False):
def get_current_index_name(corpus: CorpusConfiguration, client) -> str:
"""get the name of the current corpus' associated index"""
alias = corpus.es_alias or corpus.es_index
indices = client.indices.get(index="{}".format(alias))
indices = client.indices.get(index=alias)
return max(sorted(indices.keys()))


Expand Down
8 changes: 5 additions & 3 deletions backend/es/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from addcorpus.permissions import CanSearchCorpus
from api.save_query import should_save_query
from addcorpus.models import Corpus
from addcorpus.permissions import corpus_config_from_request
from api.models import Query
from api.api_query import api_query_to_es_query
from es.es_alias import get_current_index_name
from es.search import get_index, total_hits, hits
from ianalyzer.elasticsearch import elasticsearch
from tag.permissions import CanSearchTags
Expand Down Expand Up @@ -115,10 +117,10 @@ class NamedEntitySearchView(APIView):
permission_classes = [CanSearchCorpus]

def get(self, request, *args, **kwargs):
corpus_name = kwargs.get('corpus')
corpus_config = corpus_config_from_request(request)
document_id = kwargs.get('id')
client = elasticsearch(corpus_name)
index = get_index(corpus_name)
client = elasticsearch(corpus_config.corpus.name)
index = get_current_index_name(corpus_config, client)
fields = self.find_named_entity_fields(client, index)
query = self.construct_named_entity_query(document_id)
response = client.search(index=index, query=query)
Expand Down

0 comments on commit 7460144

Please sign in to comment.