I am trying to tune the relevance of my field search result based on another field numerical value (the field is in another model). Looking at the ES documentation it seems that functional boosts is what I need for my usecase.
Note that I am using the django-elasticsearch-dsl; package.
My use case:
- I have a search field where users can search for companies based on the company name.
- I want to return the company names that match the query where sorting (relevance) depends on the company's net asset, where the higher the number, the more relevant (field in another model)
Model definition:
class Company(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255, blank=False, null=False)
def __str__(self):
return self.name
class CompanyNetAsset(models.Model):
id = models.AutoField(primary_key=True)
assets = models.IntegerField
company_id = models.ForeignKey('Company', on_delete=models.PROTECT, blank=True, null=True)
def __str__(self):
return self.name
my es document:
...
custom_stop_words = token_filter(
'custom_stopwords',
type='stop',
ignore_case=True,
stopwords=['the', 'and']
)
html_strip = analyzer(
'html_strip',
tokenizer="standard",
filter=["lowercase", "asciifolding", custom_stop_words],
char_filter=["html_strip"],
)
@INDEX.doc_type
class CompanyDocument(Document):
id = fields.IntegerField(attr='id')
name = fields.TextField(
analyzer=html_strip,
fields={
'raw': fields.TextField(analyzer='keyword'),
}
)
class Django:
model = Company
and here is the DocumentViewSet:
class CompanyDocumentViewSet(DocumentViewSet):
"""The Company Document view."""
serializer_class = CompanyDocumentSerializer
lookup_field = 'id'
document = CompanyDocument
filter_backends = [
FilteringFilterBackend,
SearchFilterBackend,
]
search_fields = (
'name'
)
filter_fields = {
'id': None,
'name': 'name.raw',
}
Any idea how I can achieve this using the drf ES package?
UPDATE
here's an example query:
/api/v1/employers/companies/?search=name:foundation%20center
"results": [
{
"id": 469329,
"name": "THE FOUNDATION CENTER",
"city": "NEW YORK",
"state": "NY"
},
{
"id": 323012,
"name": "OVERTURE CENTER FOUNDATION",
"city": "MADISON",
"state": "WI"
},
{
"id": 367286,
"name": "PEACE CENTER FOUNDATION",
"city": "GREENVILLE",
"state": "SC"
},
...
And here is a son output of the document:
{'settings': {'number_of_shards': 1,
'number_of_replicas': 1,
'analysis': {'analyzer': {'html_strip': {'tokenizer': 'standard',
'filter': ['lowercase', 'asciifolding', 'custom_stopwords'],
'char_filter': ['html_strip'],
'type': 'custom'}},
'filter': {'custom_stopwords': {'ignore_case': True,
'stopwords': ['the', 'and'],
'type': 'stop'}}}},
'mappings': {'properties': {'id': {'type': 'integer'},
'city': {'type': 'text'},
'state': {'type': 'text'},
'name': {'analyzer': 'html_strip',
'fields': {'raw': {'analyzer': 'keyword', 'type': 'text'}},
'type': 'text'}}}}
from Django DRF elastic search dsl, Apply functional boosting based on another field numerical value
No comments:
Post a Comment