Skip to content

Commit

Permalink
Filter Elasticsearch search results by score
Browse files Browse the repository at this point in the history
Fix #250
  • Loading branch information
ta2-1 committed Jul 8, 2014
1 parent bc87427 commit 173f201
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
3 changes: 1 addition & 2 deletions pootle/apps/pootle_store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,7 @@ def update_tmserver(self):
update_tmserver(self.store.translation_project.language.code, obj)

def get_tm_suggestions(self):
return get_tmsuggestions(self.store.translation_project.language.code,
self.source)
return get_tmsuggestions(self)

##################### TranslationUnit ############################

Expand Down
40 changes: 26 additions & 14 deletions pootle/core/tmserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
from django.conf import settings


# Elasticsearch filter settings
MIN_SCORE = 0.1


def get_params():
params = getattr(settings, 'POOTLE_TM_SERVER', None)

Expand All @@ -51,26 +55,34 @@ def update(language, obj):
body=obj,
id=obj['id'])

def search(language, source):

def is_valuable_hit(unit, hit):
if hit['_score'] < MIN_SCORE or str(unit.id) == hit['_id']:
return False

return True


def search(unit):
if es is not None:
res = []
language = unit.store.translation_project.language.code
es_res = es.search(index=es_params['INDEX_NAME'],
doc_type=language,
body={"query": {"match": {'source': source}}})
body={"query": {"match": {'source': unit.source}}})

max_score = es_res['hits']['max_score']
for hit in es_res['hits']['hits']:
res.append({
'unit_id': ['_id'],
'quality': 100 * hit['_score'] / max_score,
'source': hit['_source']['source'],
'target': hit['_source']['target'],
'project': hit['_source']['project'],
'path': hit['_source']['path'],
'username': hit['_source']['username'],
'fullname': hit['_source']['fullname'],
'email_md5': hit['_source']['email_md5'],
})
if is_valuable_hit(unit, hit):
res.append({
'unit_id': hit['_id'],
'source': hit['_source']['source'],
'target': hit['_source']['target'],
'project': hit['_source']['project'],
'path': hit['_source']['path'],
'username': hit['_source']['username'],
'fullname': hit['_source']['fullname'],
'email_md5': hit['_source']['email_md5'],
})

return res

Expand Down

0 comments on commit 173f201

Please sign in to comment.