node.js - 在 Elastic 搜索中同时使用 NOT 和 OR

标签 node.js elasticsearch

这是我在 Elasticsearch 中使用的查询,用于过滤满足一个条件或不满足其他条件的记录。

{
    "query": 
    {
        "query_string": 
        {
            "query": "(NOT col1: \"val1\") OR (col2: val2)",
            "analyze_wildcard": true
}}}

问题是我无法在 Nodejs 中编写等效的语法来提取信息。我们不能在这里使用must_not,因为它是一个OR条件

最佳答案

您将在 should 数组中设置这两个条件,因为如果有人匹配您,您将在结果中获得记录,您将必须使用 must matchmust_not match

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must_not": {
              "match": {
                "col1": {
                  "query": "val1",
                  "type": "phrase"
                }
              }
            }
          }
        },
        {
          "match": {
            "col2": {
              "query": "val2",
              "type": "phrase"
            }
          }
        }
      ]
    }
  }
}

关于node.js - 在 Elastic 搜索中同时使用 NOT 和 OR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47546424/

相关文章:

jquery - TS-2304 错误 - 在 'Iterable' 文件中导入 "jquery"时在 TypeScript 中找不到名称 ".ts"

node.js - postman 上的 POST 请求未完成

elasticsearch - 当要查找的数据为空时,是否需要从 Elasticsearch 查询中删除 “query”部分?

java - 无法使用 java InetAddress 找到主机,但可以使用 curl 找到主机

angularjs - Angular 套接字 io 未定义

javascript - 带有 Node.js + Express + Hogan.js 的部分

mysql - 即使有 IP 地址,MagicMirror SQL 连接也会出现 ENOTFOUND 错误

macos - 通过 Homebrew 软件安装时,elasticsearch监听哪个端口?

java - Elasticsearch-在java中创建节点/索引时出错

symfony - 将Elastica与PagerFanta和动态querybuilder一起使用