Search code examples
python-3.xelasticsearchflaskelasticsearch-5fuzzy-search

How to implement Case and Punctuation Insenstive,forgive minor spelling mistakes, fuzziness in elastic Search?


I am searching json file using elastic search. I inserted the data. Now I want to incorporate some conditions in DSL query. I have to incorporate

1) basic fuzziness like case and punctuation insensitive and forgive minor spelling mistakes in prefix matching

2) If a search text contains 3+ characters, I should return all search queries that are atmost 2 edit distance away from search text.

I am doing this second condition and I am facing difficulty in writing a query which satisfies both the conditions. Could anyone tell how to write a query? Below is the code written so far

from flask import Flask, request
import json
import requests
import pprint
app = Flask(__name__)
__URL__ = "http://localhost:9200"
__HEAD__ = {'Content-Type': 'application/json'}


def query_data(query: str, fuzz=1):
    url = __URL__ + '/sample_data/_search'

    payload = json.dumps({
        'query': {
            'fuzzy': {
                'search_term': {
                    'value': query,
                    'fuzziness': fuzz,
                    # 'prefix_length': 3 // The number of initial characters which will not be “fuzzified”.
                    # // This helps to reduce the number of terms which must be examined. 
                }
            }
        },
      'sort': { 'search_term': { 'order': 'desc' } }
    })

    resp = requests.get(url=url, data=payload, headers=__HEAD__)

    return resp.json()

@app.route('/elasty')
def elasty():
    q=request.args.get('q')
    if len(q) > 3:
        result = query_data(q, 2)
    else:
        result = query_data(q)

    return json.dumps(result)

if __name__ == '__main__':
    app.run()

Solution

  • Problem is with the sort.
    Change

    'sort': { 'search_term': { 'order': 'desc' } }
    

    to

    'sort': { 'FieldLong': { 'order': 'desc',  "missing": "_last", "unmapped_type": "long" } }
    

    I have tried and this works.