Hello, I am using the Elastic Python API and having difficulties to process a document through analyzer. My goal is to process the content of ingest's document with analyzer, but I cannot achieve any result with a regular document.
This is the code I use :
es = elasticsearch.Elasticsearch(['localhost:9200'])
ic = IndicesClient(es)
ic.delete(index="_all")
index_body = {
"settings":
{
"analysis":
{
"analyzer":
{
"my_analyzer":
{
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase"]
}
}
}
},
"mappings":
{
"properties": {
"sentence": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
ic.create(index="index1", body=index_body)
body = {"sentence": "HELLO there"}
res = es.index(index="index1", id=1, body=body)
time.sleep(2)
print("SLEEP END")
search_body = {
"query" : {
"term" : {
"sentence": 'hello',
}
},
"highlight": {
"fields": {
"sentence": {
"fragment_size": 20, # The size of the highlighted fragment in characters. Defaults to 100.
"number_of_fragments": 5
}
}
},
"size": 10
}
res2 = es.search(index='index1', body=search_body) #,body=search_body) #
print("SEARCH RESULT::")
print(res2)
Console:
SLEEP END
SEARCH RESULT::
{'took': 4, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 1, 'relation': 'eq'}, 'max_score': 0.2876821, 'hits': [{'_index': 'index1', '_type': '_doc', '_id': '1', '_score': 0.2876821, '_source': {'sentence': 'HELLO there'}, 'highlight': {'sentence': ['HELLO there']}}]}}
The document I have at http://localhost:9200/index1/_doc/1 is
{"_index":"index1","_type":"_doc","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{"sentence":"HELLO there"}}
so the field "sentence" is not processed by the analyzer.
I tried to code according to the Elastic documentation, and couldn't find Python example online that worked for me. Any help is appreciated, and for using analyzer with Ingest, should I just change the "mappings" in index_body ? By the way I am using ES 7.1/Python3