Elasticsearch 搜索多个index和多个type下的数据

/_search:所有索引,所有type下的所有数据都搜索出来

GET /_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "tradingDay": "20200520"
        }
      }
    }
  }
}

 

/index1/_search:指定一个index,搜索其下所有type的数据

GET /stk_his_2020/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "tradingDay": "20200520"
        }
      }
    }
  },"_source": false
}

_source 关键字类似与mysql中的select fileds,只查询需要的字段,此处使用 "_source": false 避免了搜索无用信息,大大的加快了搜索速度。

 

/index1,index2/_search:同时搜索两个index下的数据

GET /stk_his_2019,stk_his_2020/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "tradingDay": {
              "gte": "20190125",
              "lte": "20200525"
            }
          }
        }
      ], 
      "filter": {
        "term": {
          "stkId": "601162.SH"
        }
      }
    }
  }, 
  "_source": false, 
  "aggs": {
    "total": {
      "stats": {
        "field": "hqVo.qTDailyFlow.sellValueBig"
      }
    }
  }
}

此处的使用场景为:计算区间内的流出额,但是我们在存储数据时,2019年和2020年处在不同的标签,那么我们使用多标签搜索+range定位时间+aggs.stats即可算出区间内的数据。

 

/*1,*2/_search:按照通配符去匹配多个索引

GET /stk_his_*/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "tradingDay": {
              "gte": "20190617",
              "lte": "20200617"
            }
          }
        }
      ], 
      "filter": {
        "term": {
          "stkId": "000001.SZ"
        }
      }
    }
  }
}

这里用*代表了全部年份,应用场景和上面那个问题是一样的,但是index更多,会慢一些。

 

/index1/type1/_search:搜索一个index下指定的type的数据

GET /stk_his_2019/hits/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "tradingDay": {
              "gte": "20190617",
              "lte": "20200617"
            }
          }
        }
      ], 
      "filter": {
        "term": {
          "stkId": "000001.SZ"
        }
      }
    }
  },"_source": false
}

 

/index1/type1,type2/_search:可以搜索一个index下多个type的数据

 

GET /stk_his_2019/hits,_shards/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "tradingDay": {
              "gte": "20190617",
              "lte": "20200617"
            }
          }
        }
      ], 
      "filter": {
        "term": {
          "stkId": "000001.SZ"
        }
      }
    }
  }
}

 

/index1,index2/type1,type2/_search:搜索多个index下的多个type的数据

 

GET /stk_his_2019,stk_his_2020/hits,_shards/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "tradingDay": {
              "gte": "20190617",
              "lte": "20200617"
            }
          }
        }
      ], 
      "filter": {
        "term": {
          "stkId": "000001.SZ"
        }
      }
    }
  }
}

 

/_all/type1,type2/_search:_all,可以代表搜索所有index下的指定type的数据

 

GET /_all/hits,_shards/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "tradingDay": {
              "gte": "20190617",
              "lte": "20200617"
            }
          }
        }
      ], 
      "filter": {
        "term": {
          "stkId": "000001.SZ"
        }
      }
    }
  }
}

 

 

posted @ 2020-06-09 15:26  KoenigSEA  阅读(4965)  评论(1编辑  收藏  举报
Back to HOME