Sto provando un sacco di tempo a trattare i caratteri apostrofo vicino all'inizio o al centro delle parole. Sono in grado di trattare l'inglese possessivo, ma cerco anche di occuparmi del francese e di gestire parole come "d'action", dove un personaggio apostrofo arriva all'inizio della parola e non alla fine come "suo".Come configurare Haystack/Elasticsearch per gestire le contrazioni e gli apostrofi vicino all'inizio di una parola
Una ricerca tramite pagliaio auto_query per "d azione" restituirà risultati, ma "d'azione" non restituisce nulla. Se interrogo direttamente l'API _search di elasticsearch (_search? Q = D% 27ACTION) ottengo risultati per "d'action". Mi sto pertanto chiedendo se si tratta di un problema con il motore del pagliaio.
La mia configurazione:
'settings': {
"analysis": {
"char_filter": {
"quotes": {
"type": "mapping",
"mappings": [
"\\u0091=>\\u0027",
"\\u0092=>\\u0027",
"\\u2018=>\\u0027",
"\\u2019=>\\u0027",
"\\u201B=>\\u0027"
]
}
},
"analyzer": {
"ch_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ['ch_en_possessive_word_delimiter', 'ch_fr_stemmer'],
"char_filter": ['html_strip', 'quotes'],
},
},
"filter": {
"ch_fr_stemmer" : {
"type": "snowball",
"language": "French"
},
"ch_en_possessive_word_delimiter": {
"type": "word_delimiter",
"stem_english_possessive": True
}
}
}
}
Ho anche sottoclasse ElasticsearchSearchBackend e BaseEngine in modo da poter aggiungere la configurazione di cui sopra:
class ConfigurableESBackend(ElasticsearchSearchBackend):
# Word reserved by Elasticsearch for special use.
RESERVED_WORDS = (
'AND',
'NOT',
'OR',
'TO',
)
# Characters reserved by Elasticsearch for special use.
# The '\\' must come first, so as not to overwrite the other slash replacements.
RESERVED_CHARACTERS = (
'\\', '+', '-', '&&', '||', '!', '(', ')', '{', '}',
'[', ']', '^', '"', '~', '*', '?', ':',
)
def setup(self):
"""
Defers loading until needed.
"""
# Get the existing mapping & cache it. We'll compare it
# during the ``update`` & if it doesn't match, we'll put the new
# mapping.
try:
self.existing_mapping = self.conn.get_mapping(index=self.index_name)
except Exception:
if not self.silently_fail:
raise
unified_index = haystack.connections[self.connection_alias].get_unified_index()
self.content_field_name, field_mapping = self.build_schema(unified_index.all_searchfields())
current_mapping = {
'modelresult': {
'properties': field_mapping,
'_boost': {
'name': 'boost',
'null_value': 1.0
}
}
}
if current_mapping != self.existing_mapping:
try:
# Make sure the index is there first.
self.conn.create_index(self.index_name, settings.ELASTICSEARCH_INDEX_SETTINGS)
self.conn.put_mapping(self.index_name, 'modelresult', mapping=current_mapping)
self.existing_mapping = current_mapping
except Exception:
if not self.silently_fail:
raise
self.setup_complete = True
class CHElasticsearchSearchEngine(BaseEngine):
backend = ConfigurableESBackend
query = ElasticsearchSearchQuery
Grazie! Sono rimasto bloccato su questo per molto tempo. –