弹性搜索中的非条件

Roo*_*dra 1 elasticsearch

我正在尝试NOT在 elasticsearch 查询中实现条件。我可以filter在里面实现bool还是我需要编写单独的过滤器,如下所示。有什么最佳解决方案吗?

{
   "query": {
      "bool": {
     "must": [
        {
           "query_string": {
          "query": "fashion"
           }
        },
        {
           "term": {
          "post_status": "publish"
           }
        }
     ]
      }
   },
   "filter": {
      "not": {
     "filter": {
        "term": {
           "post_type": "page"
        }
     }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

Vic*_*ing 5

您可以使用 must_not 子句:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "_all": "fashion"
                    }
                },
                {
                    "term": {
                        "post_status": "publish"
                    }
                }
            ],
            "must_not": {
                "term": {
                    "post_type": "page"
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,我建议使用匹配过滤器而不是 query_string,因为 query_string 需要更严格的 Lucene 语法(因此更容易出错),而 match 更像是一个搜索框:它会自动将人类可读的查询转换为Lucene 查询。