天天看點

【ELK】Elasticsearch入門05 --基礎文法 綜合條件查詢 排序 分頁

【ELK】Elasticsearch入門05 --基礎文法 綜合條件查詢 排序 分頁

綜合條件查詢

GET /test/_search
{
 "_source": {"excludes":"sex"},
  "query" : {
    "bool": {
    ## 條件:must,表示[]中條件都需要符合
    ## 條件:should,表示[]中條件隻需要符合一個即可查出
    ## 條件:must_not,表示[]中條件一個都不符合即可查出
      "must" :     
      [
        {
          "match" : 
          {"name" : "zhang san"}
        },
        {
        ## 範圍查詢
          "range" : 
          {
            "age":{
              "from" : "22",
              "to" : "33"
            }
          }
        }
      ]
    }
  }
}
           
GET /test/_search
{
 "_source": {"excludes":"sex"},
  "query" : {
    "bool": {
      "must" : 
      [
        {
          "match" : 
          {"name" : "zhang san"}
        },
        {
          "range" : 
          {
            "age":{
              "from" : "22",
              "to" : "33"
            }
          }
        }
      ]
    }
  }
}
傳回:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.9983525,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.9983525,
        "_source" : {
          "name" : "zhang san",
          "age" : "22"
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.8416345,
        "_source" : {
          "name" : "zhang san li",
          "age" : "22"
        }
      }
    ]
  }
}

           

注意:

在條件查詢中,在must中的條件會影響檢索結果的最終得分_source,是以像未進行分詞的字段,如數值類型,keyword不建議放在bool的{}中作為條件。推薦使用filter進行過濾

過濾查詢

過濾查詢不會影響字段的_source得分

GET /test/_search
{
 "_source": {"excludes":"sex"},
  "query" : {
    "bool": {
      "must" : 
      [
        {
          "match" : 
          {"name" : "zhang san"}
        }
      ],
    "filter":{
      "range" : {
        "字段名": {
          "from":"開始值",
          "to":"結束值"
         }
        }
      }
    }
  },
    "from": 目前頁碼,
    "size": 每頁長度
}
           
GET /test/_search
{
 "_source": {"excludes":"sex"},
  "query" : {
    "bool": {
      "must" : 
      [
        {
          "match" : 
          {"name" : "zhang san"}
        }
      ],
      ##過濾 按照age欄位值的範圍22-33
    "filter":{
      "range" : {
        "age": {
          "from":"22",
          "to":"33"
         }
        }
      }
    }
  },
  ## 分頁第0頁,每頁長度為1
    "from": 0,
    "size": 1
}
傳回:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.9983525,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.9983525,
        "_source" : {
          "name" : "zhang san",
          "age" : "22"
        }
      }
    ]
  }
}

           

按照字段進行排序

如果進行排序那麼傳回的_source得分會變為null

GET /test/_search
{
 "_source": {"excludes":"sex"},
  "query" : {
    "bool": {
      "must" : 
      [
        {
          "match" : 
          {"name" : "zhang san"}
        }
      ],
    "filter":{
      "range" : {
        "age": {
          "from":"22",
          "to":"33"
         }
        }
      }
    }
  },
  ##排序 排序方式分為desc 和ase
  "sort": [
      {
        "字段名": {
          "order":"排序方式"
        }
      }
    ],
    "from": 0,
    "size": 1
}
           
GET /test/_search
{
 "_source": {"excludes":"sex"},
  "query" : {
    "bool": {
      "must" : 
      [
        {
          "match" : 
          {"name" : "zhang san"}
        }
      ],
    "filter":{
      "range" : {
        "age": {
          "from":"22",
          "to":"33"
         }
        }
      }
    }
  },
  "sort": [
      {
        "age": {
          "order":"desc"
        }
      }
    ],
    "from": 0,
    "size": 1
}
傳回:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "zhang san",
          "age" : "22"
        },
        "sort" : [
          22
        ]
      }
    ]
  }
}

           

繼續閱讀