阅读 46

es - elasticsearch search - paginate - 2

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

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

# paginate scroll
PUT /paginate_2_test
{
  "mappings" : {
    "dynamic"    : "strict",
    "properties" : {
      "name" : {"type" : "text"}
    }
  }
}

# 索引
POST /paginate_2_test/_doc/1
{
  "name" : "hello"
}

# 索引
POST /paginate_2_test/_doc/2
{
  "name" : "hello good"
}

# 索引
POST /paginate_2_test/_doc/3
{
  "name" : "hello 3"
}

# 索引
POST /paginate_2_test/_doc/4
{
  "name" : "hello 4"
}

# 索引
POST /paginate_2_test/_doc/5
{
  "name" : "hello 5"
}

# 索引
POST /paginate_2_test/_doc/6
{
  "name" : "hello 6"
}

# 搜索
GET /paginate_2_test/_search?scroll=1000m
{
  "size"  : 2,
  "query" : {
    "match" : {
      "name" : "hello"
    }
  },
  "sort" : [
    "_doc"
  ]
}

# 结果
{
  "_scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjdFNW5wZC1RUTRXby1BcHgxaC1CM3cAAAAAAAcapxZWSzBHRjFLeVFHS21NXzBLdmdISG53",
  "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_2_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "hello"
        },
        "sort" : [
          0
        ]
      },
      {
        "_index" : "paginate_2_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "hello good"
        },
        "sort" : [
          1
        ]
      }
    ]
  }
}

# 滚动搜索 scroll_id
GET /_search/scroll
{
  "scroll"    : "1000m",
  "scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjdFNW5wZC1RUTRXby1BcHgxaC1CM3cAAAAAAAcapxZWSzBHRjFLeVFHS21NXzBLdmdISG53"
}

# 结果之一
{
  "_scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjdFNW5wZC1RUTRXby1BcHgxaC1CM3cAAAAAAAcapxZWSzBHRjFLeVFHS21NXzBLdmdISG53",
  "took" : 1,
  "timed_out" : false,
  "terminated_early" : true,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "paginate_2_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "hello 3"
        },
        "sort" : [
          2
        ]
      },
      {
        "_index" : "paginate_2_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "name" : "hello 4"
        },
        "sort" : [
          3
        ]
      }
    ]
  }
}

# 删除scroll_id
DELETE /_search/scroll
{
  "scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjdFNW5wZC1RUTRXby1BcHgxaC1CM3cAAAAAAAcapxZWSzBHRjFLeVFHS21NXzBLdmdISG53"
}

# 结果
{
  "succeeded" : true,
  "num_freed" : 1
}

# 使用slices
GET /paginate_2_test/_search?scroll=1000m
{
  "size"  : 2,
  "slice" : {
    "id"  : 0,
    "max" : 2
  },
  "query" : {
    "match" : {
      "name" : "hello"
    }
  },
  "sort" : [
    "_doc"
  ]
}

# 结果
{
  "_scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjdFNW5wZC1RUTRXby1BcHgxaC1CM3cAAAAAAAca4hZWSzBHRjFLeVFHS21NXzBLdmdISG53",
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "paginate_2_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "hello 3"
        },
        "sort" : [
          2
        ]
      },
      {
        "_index" : "paginate_2_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "name" : "hello 4"
        },
        "sort" : [
          3
        ]
      }
    ]
  }
}


GET /paginate_2_test/_search?scroll=1000m
{
  "size"  : 2,
  "slice" : {
    "id"  : 1,
    "max" : 2
  },
  "query" : {
    "match" : {
      "name" : "hello"
    }
  },
  "sort" : [
    "_doc"
  ]
}

# 结果
{
  "_scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjdFNW5wZC1RUTRXby1BcHgxaC1CM3cAAAAAAAca_RZWSzBHRjFLeVFHS21NXzBLdmdISG53",
  "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" : "paginate_2_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "hello"
        },
        "sort" : [
          0
        ]
      },
      {
        "_index" : "paginate_2_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "hello good"
        },
        "sort" : [
          1
        ]
      }
    ]
  }
}


# scroll slice - 0
GET /_search/scroll
{
  "scroll"    : "1000m",
  "scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjdFNW5wZC1RUTRXby1BcHgxaC1CM3cAAAAAAAca4hZWSzBHRjFLeVFHS21NXzBLdmdISG53" 
}

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


# scroll slice - 1
GET /_search/scroll
{
  "scroll"    : "1000m",
  "scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjdFNW5wZC1RUTRXby1BcHgxaC1CM3cAAAAAAAca_RZWSzBHRjFLeVFHS21NXzBLdmdISG53"
}

# 结果
{
  "_scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjdFNW5wZC1RUTRXby1BcHgxaC1CM3cAAAAAAAca_RZWSzBHRjFLeVFHS21NXzBLdmdISG53",
  "took" : 1,
  "timed_out" : false,
  "terminated_early" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

# 删除scroll_id
DELETE /_search/scroll
{
  "scroll_id" : [
    "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjdFNW5wZC1RUTRXby1BcHgxaC1CM3cAAAAAAAca4hZWSzBHRjFLeVFHS21NXzBLdmdISG53",
    "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFjdFNW5wZC1RUTRXby1BcHgxaC1CM3cAAAAAAAca_RZWSzBHRjFLeVFHS21NXzBLdmdISG53"
  ]
}

# 结果
{
  "succeeded" : true,
  "num_freed" : 2
}
复制代码
文章分类
后端
文章标签