【问题标题】:Multiple types in Elasticsearch Query for specific types onlyElasticsearch 查询中的多种类型仅针对特定类型
【发布时间】:2015-05-13 13:57:38
【问题描述】:

我创建了名为“testindex”的索引和名为 type1、type2、type3 的三种类型。我需要这三种类型的过滤数据。我是这样过滤的。

GET testindex/type1,type2,type3/_search
{"query":{
    "filtered":{
        "query":{
            "match_phrase_prefix":{"title":"c"}
        },
        "filter":{
            "bool":{
                "must":[
                    {
                        "term":{
                          "status": "1"
                        }    
                    }
                    ]
            }

        }

    }

} 

这工作正常,但问题是 type3 没有 status 字段。我只需要考虑 type1 和 type2 的过滤器,而不是 type3 的过滤器。我需要帮助来解决这个问题。

【问题讨论】:

  • “type3 没有状态字段是什么意思。我需要考虑仅对 type1 和 type2 进行过滤,而不是对 type3 进行过滤”?如果 type3 的 status 字段不存在,您预计会发生什么?

标签: php elasticsearch


【解决方案1】:

一种方法是使用or 过滤器,它基本上会选择:

  • test1test2 类型的文档具有 status = 1
  • test3 类型的文档,status 字段没有条件

这个查询应该可以工作:

{
  "query": {
    "filtered": {
      "query": {
        "match_phrase_prefix": {
          "title": "h"
        }
      },
      "filter": {
        "or": [
          {
            "bool": {
              "must": [
                {
                  "term": {
                    "status": "1"
                  }
                },
                {
                  "terms": {
                    "_type": [
                      "test1",
                      "test2"
                    ]
                  }
                }
              ]
            }
          },
          {
            "term": {
              "_type": "test3"
            }
          }
        ]
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 2011-09-28
    相关资源
    最近更新 更多