elasticsearch - 有没有办法使用 Bodybuilder ElasticSearch 构建带有 has_child 的 "or"

标签 elasticsearch

{
  "query":
  {
    "bool":
    {
      "should": [
       {   
          "has_child": {
            "inner_hits":{},
            "type": "emp",
            "query": {
                "match": {
                  "name": "xyz"
                }
              }
            }
          },
          {
            "bool":
            {
              "must":
              [
                  {
                    "match" : { "name" : "xyz" }
                  },
                  {
                    "match" : { "_type" : "emps" }
                  }
              ],
              "must_not": [
                {
                  "has_child": {
                    "type": "emp",
                    "query": {
                      "exists":
                            {
                              "field": "name"
                            }
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
}

最佳答案

不幸的是,不。

事实上,是的。我错了。 OP发布his question在 Bodybuilder 的 github 上得到了以下答案:

It's a bit quirky but this is how I'd write it:

var inner = bodybuilder()
    .query('match', 'name', 'xyz')  
    .query('match', '_type', 'emps')    
    .notQuery('has_child', {type: 'emp'}, (q) => {
        return q.query('exists', 'field', 'name')
    })
    .build()

bodybuilder()
    .orQuery('bool', inner.query.bool)
        .orQuery('has_child', {inner_hits: {}, type: 'emp'}, (q) => {
        return q.query('match', 'name', 'xyz')  
    })
    .build()

Something to watch out for: the order of the orQuerys matters here, it doesn't work if these are switched, which isn't ideal. I think that's because of how the bool clauses are being merged. Let me know is this isn't working for you.

关于elasticsearch - 有没有办法使用 Bodybuilder ElasticSearch 构建带有 has_child 的 "or",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42419058/

相关文章:

nginx - 使用Nginx作为Docker的代理

azure - Logstash 无法向 Elasticsearch Master 发送加入请求

java - Elasticsearch 特殊字符问题

json - ElasticSearch:在字段中包含字段的日期范围

java - 使用聚合从 Elasticsearch 检索数据,其中值包含连字符

elasticsearch - 全文匹配的ElasticSearch “Match”

symfony - 无法连接到主机,elasticsearch 关闭? Symfony 4 elastica/rulfin 客户端。 docker

elasticsearch - 使用 PHP curl 在 ElasticSearch 中索引文档

python - 有没有办法以原子方式删除和重新创建 Elasticsearch 别名?

java - 将现有的Cassandra数据集成到Elassandra