如何通过精确搜索在弹性中编写"select * from users where username ='kuldeep' and (status = 'active' OR status =



我试过这个查询

{
"query": {
"bool" : {
"must" : {
"match" : { "username": "kuldeep" }
},
"should" : [
{ "term" : {"status": "inactive"} },
{ "term" : { "status": "active" } }
],
"minimum_should_match" : 0
}
}
}

我正在使用上述查询获得结果,但它返回用户名kuldeep和kuldeep singh。

"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 8.914892,
"hits": [{
"_index": "searchusers",
"_type": "_doc",
"_id": "6f9b37b1-2934-4125-bbf9-875e61c60730",
"_score": 8.914892,
"_source": {
"status": "active",
"createdOn": 1637145140000,
"id": "6f9b37b1-2934-4125-bbf9-875e61c60730",
"agreementAccepted": false,
"userNumber": "0000000000001251",
"email": "",
"createdBy": "a91a9b69-7774-4e2e-8901-037045c4f5e0",
"username": "kuldeep"
}
},
{
"_index": "searchusers",
"_type": "_doc",
"_id": "1744d7ae-3454-4ad4-9ea9-591102eb0369",
"_score": 6.8384924,
"_source": {
"status": "active",
"createdOn": 1639458287000,
"id": "1744d7ae-3454-4ad4-9ea9-591102eb0369",
"agreementAccepted": false,
"userNumber": "0000000000002429",
"email": "",
"createdBy": "39057c58-3ed6-457d-a832-2f69a2472943",
"username": "kuldeep singh"
}
}
]
}
}

正如elastic docs (https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)中提到的,minimum_should_match默认值是0,如果它包含match或filter。当我将minimum_should_match更改为1时,它不返回任何数据。

"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
}

当使用minimum_should_match为0时,似乎应该查询对结果没有影响。

对于确切的搜索,而不是match,你需要使用term查询,你也需要使用term查询的关键字字段,而不是在text字段,所以如果你使用Elasticsearch的默认映射,对于每个text字段有.keyword字段,在这种情况下,你可以简单地改变你的查询的相关部分如下:

{
"query" : {
"term" : {
"username.keyword" : "kuldeep"
}
}
}

相关内容

最新更新