阅读 30

es - elasticsearch search - paginate -1

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :paginate有哪些,有什么特点?
答 :
在这里插入图片描述
问 :如何分页?
答 :

# paginate 
PUT /paginate_1_test
{
  "mappings" : {
    "dynamic"    : "strict",
    "properties" : {
      "name" : {
        "type"   : "text",
        "fields" : {
          "keyword" : {
            "type" : "keyword"
          }
        }
      },
      "id" : {
        "type" : "integer"
      }
    }
  }
}

# 索引
POST /paginate_1_test/_doc/1
{
  "name" : "hello good",
  "id"   : 1
}

# 索引
POST /paginate_1_test/_doc/2
{
  "name" : "hello good",
  "id"   : 2
}

# 索引
POST /paginate_1_test/_doc/3
{
  "name" : "hello good",
  "id"   : 3
}

# 索引
POST /paginate_1_test/_doc/4
{
  "name" : "hello good",
  "id"   : 4
}

# 索引
POST /paginate_1_test/_doc/5
{
  "name" : "hello good",
  "id"   : 5
}

# 索引
POST /paginate_1_test/_doc/6
{
  "name" : "hello good",
  "id"   : 6
}


# 搜索,from - size,相当于 skip - limit
GET /paginate_1_test/_search
{
  "from"  : 1,
  "size"  : 5,
  "query" : {
    "match" : {
      "name" : "hello"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : 0.074107975,
    "hits" : [
      {
        "_index" : "paginate_1_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.074107975,
        "_source" : {
          "name" : "hello good",
          "id" : 2
        }
      },
      {
        "_index" : "paginate_1_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.074107975,
        "_source" : {
          "name" : "hello good",
          "id" : 3
        }
      },
      {
        "_index" : "paginate_1_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.074107975,
        "_source" : {
          "name" : "hello good",
          "id" : 4
        }
      },
      {
        "_index" : "paginate_1_test",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 0.074107975,
        "_source" : {
          "name" : "hello good",
          "id" : 5
        }
      },
      {
        "_index" : "paginate_1_test",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 0.074107975,
        "_source" : {
          "name" : "hello good",
          "id" : 6
        }
      }
    ]
  }
}


# 加 _pit, 获取id
POST /paginate_1_test/_pit?keep_alive=1000m

# 结果
{
  "id" : "p66xAwEPcGFnaW5hdGVfMV90ZXN0Fl9mTmc4N3NxVGRLeENHTnBFRGZNdkEAFlZLMEdGMUt5UUdLbU1fMEt2Z0hIbncAAAAAAAAGmyIWN0U1bnBkLVFRNFdvLUFweDFoLUIzdwEWX2ZOZzg3c3FUZEt4Q0dOcEVEZk12QQAA"
}

# 搜索,search_after,将_pit获取的id放入到pit.id,搜索时无需指定index
GET /_search
{
  "size"  : 3,
  "query" : {
    "match" : {
      "name" : "hello"
    }
  },
  "pit" : {
    "id" : "p66xAwEPcGFnaW5hdGVfMV90ZXN0Fl9mTmc4N3NxVGRLeENHTnBFRGZNdkEAFlZLMEdGMUt5UUdLbU1fMEt2Z0hIbncAAAAAAAAGmnEWN0U1bnBkLVFRNFdvLUFweDFoLUIzdwEWX2ZOZzg3c3FUZEt4Q0dOcEVEZk12QQAA",
    "keep_alive" : "1000m"
  },
  "sort" : [
    {
      "id" : {
        "order" : "desc"
      }
    }
  ]
}

# 结果
{
  "pit_id" : "p66xAwEPcGFnaW5hdGVfMV90ZXN0Fl9mTmc4N3NxVGRLeENHTnBFRGZNdkEAFlZLMEdGMUt5UUdLbU1fMEt2Z0hIbncAAAAAAAAGmnEWN0U1bnBkLVFRNFdvLUFweDFoLUIzdwEWX2ZOZzg3c3FUZEt4Q0dOcEVEZk12QQAA",
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "paginate_1_test",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : null,
        "_source" : {
          "name" : "hello good",
          "id" : 6
        },
        "sort" : [
          6
        ]
      },
      {
        "_index" : "paginate_1_test",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "name" : "hello good",
          "id" : 5
        },
        "sort" : [
          5
        ]
      },
      {
        "_index" : "paginate_1_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "name" : "hello good",
          "id" : 4
        },
        "sort" : [
          4
        ]
      }
    ]
  }
}


# 搜索,search_after next
GET /_search
{
  "size"  : 3,
  "query" : {
    "match" : {
      "name" : "hello"
    }
  },
  "pit" : {
    "id" : "p66xAwEPcGFnaW5hdGVfMV90ZXN0Fl9mTmc4N3NxVGRLeENHTnBFRGZNdkEAFlZLMEdGMUt5UUdLbU1fMEt2Z0hIbncAAAAAAAAGmnEWN0U1bnBkLVFRNFdvLUFweDFoLUIzdwEWX2ZOZzg3c3FUZEt4Q0dOcEVEZk12QQAA",
    "keep_alive" : "1000m"
  },
  "sort" : [
    {
      "id" : {
        "order" : "desc"
      }
    }
  ],
  "search_after" : [
    4
  ]
}

# 结果
{
  "pit_id" : "p66xAwEPcGFnaW5hdGVfMV90ZXN0Fl9mTmc4N3NxVGRLeENHTnBFRGZNdkEAFlZLMEdGMUt5UUdLbU1fMEt2Z0hIbncAAAAAAAAGmnEWN0U1bnBkLVFRNFdvLUFweDFoLUIzdwEWX2ZOZzg3c3FUZEt4Q0dOcEVEZk12QQAA",
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "paginate_1_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "hello good",
          "id" : 3
        },
        "sort" : [
          3
        ]
      },
      {
        "_index" : "paginate_1_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "hello good",
          "id" : 2
        },
        "sort" : [
          2
        ]
      },
      {
        "_index" : "paginate_1_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "hello good",
          "id" : 1
        },
        "sort" : [
          1
        ]
      }
    ]
  }
}
复制代码
文章分类
后端
文章标签