我应该怎样做搜索结果页面由elasticsearch使用elasticquent

时间:2016-06-07 09:29:52

标签: laravel search elasticsearch

这是我的代码, 通过elasticsearch搜索一些产品使用packagist elasticquent:

$query = [
    "body" => [
        "query" => [
            "function_score" => [
                "query" => [
                    "multi_match" => [
                        "query" => $keyword,
                        "analyzer"=>"standard",
                        "type" => "best_fields",
                        "fields" => [
                            0 => "name^5",
                            1 => "content^1",
                        ],
                        "fuzziness" => "AUTO",
                        "prefix_length" => 2
                    ],
                ],
                "field_value_factor" => [
                    "field" => "popular",
                    "modifier" => "log1p",
                    "factor" => "0.1",
                ],
                "boost_mode" => "sum",
                "max_boost" => "1.5",
            ],
        ],
    ],
];

$products = Product::complexSearch($query)->paginate(3);

//paging
$products->setPath('');

dd($products);

浏览器中的结果是:

ElasticquentPaginator {#795 ▼
  #total: 290
  #lastPage: 97
  #items: Collection {#790 ▼
    #items: array:10 [▼
      0 => Product {#801 ▶}
      1 => Product {#804 ▶}
      2 => Product {#807 ▶}
      3 => Product {#779 ▶}
      4 => Product {#789 ▶}
      5 => Product {#785 ▶}
      6 => Product {#791 ▶}
      7 => Product {#782 ▶}
      8 => Product {#798 ▶}
      9 => Product {#788 ▶}
    ]
  }
  #perPage: 3
  #currentPage: 1
  #path: ""
  #query: []
  #fragment: null
  #pageName: "page"
  +"hits": array:3 [▶]
}

结果数字只有10,但总数是290,paginate(3),3不起作用,我该怎么办? 谢谢!

1 个答案:

答案 0 :(得分:2)

Elasticsearch搜索大小默认为10,您可以在https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-from-size.html中看到。

您可以通过将 size 参数设置为所需的数字来要求更多结果。请注意,从版本2.0开始,您无法将大小设置为> = 10000.如果您希望检索与查询匹配的所有结果,则必须使用滚动API:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html