【问题标题】:How can I query elasticsearch for only one type of record?如何仅查询一种类型的记录的弹性搜索?
【发布时间】:2012-04-12 12:41:43
【问题描述】:

我正在向 elasticsearch 发出查询,并且我得到了多种记录类型。如何将结果限制为一种类型?

【问题讨论】:

  • 一般来说,最好包含一个您向 ES 发出的查询示例...

标签: api search schema dsl elasticsearch


【解决方案1】:

您还可以使用查询 dsl 过滤掉特定类型的结果,如下所示:

$ curl -XGET 'http://localhost:9200/_search' -d '{
    "query": {
        "filtered" : {
            "filter" : {
                "type" : { "value" : "my_type" }
            }
        }
    }
}
'

6.1 版更新: 类型过滤器现在替换为类型查询:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-type-query.html 您可以在 Query 和 Filter 上下文中使用它。

【讨论】:

  • 在 2.0.0-beta1 中已弃用。使用 bool 查询代替查询的 must 子句和过滤器的 filter 子句。
【解决方案2】:

2.3版本上可以查询_type fieldlike:

{
  "query": {
    "terms": {
      "_type": [ "type_1", "type_2" ] 
    }
  }
}

或者如果你想排除一个类型:

{
  "query": {
    "bool" : {
      "must_not" : {
        "term" : {
          "_type" : "Hassan"
        }
      }
    }
  }
}

【讨论】:

    【解决方案3】:
    {
    "query" : {
          "filtered" : { 
             "filter" : {
                "bool" : {
                "must" :[{"term":{"_type":"UserAudit"}}, {"term" : {"eventType": "REGISTRATION"}}]
               }
             }
          }
       },
       "aggs":{
          "monthly":{
             "date_histogram":{
                "field":"timestamp",
                "interval":"1y"
             },
             "aggs":{
                "existing_visitor":{
                   "terms":{
                      "field":"existingGuest"
                   }
                }
             }
          }
       }
    }
    

    "_type":"UserAudit" 条件将只查看特定于类型的记录

    【讨论】:

      【解决方案4】:

      以下查询会将结果限制为“your_type”类型的记录:

      curl - XGET 'http://localhost:9200/_all/your_type/_search?q=your_query'

      更多详情请见http://www.elasticsearch.org/guide/reference/api/search/indices-types.html

      【讨论】:

      • 链接重定向到 Elastic 主页。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      相关资源
      最近更新 更多