布尔和对Elasticsearch 6.5.3的查询

时间:2019-01-20 13:51:20

标签: python-3.x elasticsearch

我正在使用ElasticSearch 6.5.3和。请在下面找到我的代码:

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)

我想对名字和姓氏进行布尔模糊查询。我该怎么办?

我尝试了以下方法:

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)

我收到以下错误,这意味着我的查询中存在一些问题。看来我无法同时合并两个具有模糊性的匹配查询

RequestError: RequestError(400, 'parsing_exception', 'Unknown key 
for a START_OBJECT in [bool].')

1 个答案:

答案 0 :(得分:0)

您的查询有点错误,您需要像这样修复它:

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

原因是,布尔查询数组已经了解它将成为查询的对象,因此您无需再次指定它