0
votes

I'm working on ElasticSearch 6.5.3 and . Please find below my code:

from elasticsearch import Elasticsearch, helpers

def search(es_object, index_name, request):
    toto = es_object.search(index=index_name, body=request)
    return toto

fuzziness = 1
request1 = {
    "query": {
        "match" : {
            "Family_name" : {
                "query" : family_names,
                "fuzziness": fuzziness,
            }
        }
    },
    "size" : size
}

request2 = {
    "query": {
        "match" : {
            "First_name" : {
                "query" : first_names,
                "fuzziness": fuzziness,
            }
        }
    },
    "size" : size
}

result1 = search(es,ELASTICSEARCH_INDEX_NAME,request1)
result2 = search(es,ELASTICSEARCH_INDEX_NAME,request2)

I'd like to make a bool fuzzy query on firstname and family name. How can I do it please ?

I tried the following :

request = {
    "bool": {
        "must": [
            {
                "query": {
                    "match" : {
                        "First_name" : {
                            "query" : family_name,
                            "fuzziness": fuzziness,
                        }
                    }
                },
                "query": {
                    "match" : {
                        "Prénom" : {
                            "query" : firstname,
                            "fuzziness": fuzziness,
                        }
                    }
                }
            }
        ]
    }
}


result = search(es,ELASTICSEARCH_INDEX_NAME,request)

I got the following error meaning that there is some problem in my query. It seems that I cannot combine two match queries having fuzziness simultaneously

RequestError: RequestError(400, 'parsing_exception', 'Unknown key 
for a START_OBJECT in [bool].')
1
you just need to make a boolean query out of twoMysterion
i dit it above but it is not working ?BillyLeZeurbé

1 Answers

0
votes

Your query is a bit wrong, you need to fix it like this:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "First_name": {
              "query": "family_name",
              "fuzziness": fuzziness
            }
          }
        },
        {
          "match": {
            "Prénom": {
              "query": "firstname",
              "fuzziness": fuzziness
            }
          }
        }
      ]
    }
  }
}

The reason is, that boolean query array already understand that it will be an object of query, so you don't need to specify it again