如何在搜索查询中编写条件



我正在特定地区的文档中搜索。文档具有各种状态。目的是返回所有文档,除非文档的状态代码为ABCD——只有当ID大于100时,才应返回此类文档。我尝试过编写多个查询,包括下面的查询,它只返回ID大于100的ABCD文档,而不返回其他文档。这里怎么了?如何获取非wen dang文档?

"_source": true,
"from": 0,
"size": 50,
"sort": [
{
"firstStamp": "DESC"
}
],
"query": {
"bool": {
"must": [
{
"term": {
"districtId": "3755"
}
},
{
"bool": {
"must": [
{
"terms": {
"documentStatus.code.keyword": [
"ABCD"
]
}
},
{
"bool": {
"must": {
"script": {
"script": "doc['id'].value > 100"
}
}
}
}
]
}
}
]
}
}
}```

由于您没有添加任何索引映射,请查看您的搜索查询数据似乎属于对象字段数据类型。尽我所能请理解,您的目标是归还所有文件,除非文档的状态代码为ABCD,具有状态代码的文档为ABCD只有当其ID大于100时才应返回。

添加具有索引数据、搜索查询和搜索结果的工作示例

指数数据:

{
"id":200,
"documentStatus":{
"code":"DEF"
}
}
{
"id":200,
"documentStatus":{
"code":"ABCD"
}
}
{
"id":100,
"documentStatus":{
"code":"ABCD"
}
}

搜索查询:

{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"terms": {
"documentStatus.code.keyword": [
"ABCD"
]
}
},
{
"bool": {
"must": {
"script": {
"script": "doc['id'].value > 100"
}
}
}
}
]
}
},
{
"bool": {
"must_not": {
"terms": {
"documentStatus.code.keyword": [
"ABCD"
]
}
}
}
}
]
}
}
}

搜索结果:

"hits": [
{
"_index": "stof_64351595",
"_type": "_doc",
"_id": "2",
"_score": 2.0,
"_source": {
"id": 200,
"documentStatus": {
"code": "ABCD"
}
}
},
{
"_index": "stof_64351595",
"_type": "_doc",
"_id": "3",
"_score": 0.0,
"_source": {
"id": 200,
"documentStatus": {
"code": "DEF"
}
}
}
]

如果您希望文档的状态代码不为ABCD,则需要在查询中使用must_not。所以你的查询是这样的:

"from": 0,
"size": 50,
"sort": [
{
"firstStamp": "DESC"
}
],
{
"query": {
"bool": {
"must": [
{
"term": {
"districtId": "3755"
}
},
{
"range": {
"id": {
"gt": 100
}
}
}
],
"must_not": [
{
"terms": {
"documentStatus.code.keyword": [
"ABCD"
]
}
}
]
}
}
}

最新更新