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"
        }
      }
    }
  }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM