Skip to content

Commit

Permalink
use class variables instead of global variables for field mappings (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jochengcd committed May 5, 2024
1 parent c83a281 commit a7fcc25
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions haystack/backends/elasticsearch7_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,6 @@
)


DEFAULT_FIELD_MAPPING = {
"type": "text",
"analyzer": "snowball",
}
FIELD_MAPPINGS = {
"edge_ngram": {
"type": "text",
"analyzer": "edgengram_analyzer",
},
"ngram": {
"type": "text",
"analyzer": "ngram_analyzer",
},
"date": {"type": "date"},
"datetime": {"type": "date"},
"location": {"type": "geo_point"},
"boolean": {"type": "boolean"},
"float": {"type": "float"},
"long": {"type": "long"},
"integer": {"type": "long"},
}


class Elasticsearch7SearchBackend(ElasticsearchSearchBackend):
# Settings to add an n-gram & edge n-gram analyzer.
DEFAULT_SETTINGS = {
Expand Down Expand Up @@ -90,6 +67,29 @@ class Elasticsearch7SearchBackend(ElasticsearchSearchBackend):
},
}

DEFAULT_FIELD_MAPPING = {
"type": "text",
"analyzer": "snowball",
}

FIELD_MAPPINGS = {
"edge_ngram": {
"type": "text",
"analyzer": "edgengram_analyzer",
},
"ngram": {
"type": "text",
"analyzer": "ngram_analyzer",
},
"date": {"type": "date"},
"datetime": {"type": "date"},
"location": {"type": "geo_point"},
"boolean": {"type": "boolean"},
"float": {"type": "float"},
"long": {"type": "long"},
"integer": {"type": "long"},
}

def __init__(self, connection_alias, **connection_options):
super().__init__(connection_alias, **connection_options)
self.content_field_name = None
Expand Down Expand Up @@ -550,8 +550,8 @@ def build_schema(self, fields):
mapping = self._get_common_mapping()

for _, field_class in fields.items():
field_mapping = FIELD_MAPPINGS.get(
field_class.field_type, DEFAULT_FIELD_MAPPING
field_mapping = self.FIELD_MAPPINGS.get(
field_class.field_type, self.DEFAULT_FIELD_MAPPING
).copy()
if field_class.boost != 1.0:
field_mapping["boost"] = field_class.boost
Expand Down

0 comments on commit a7fcc25

Please sign in to comment.