当没有提供搜索词时,如何在 Zend lucene 搜索中获取前 20 个文档?

发布于 2025-01-02 20:45:00 字数 816 浏览 2 评论 0原文

我对 Zend_Search_Lucene 以及如何构建和搜索添加到索引的文档有了相当新的了解,但我想知道如果根本没有提供搜索词,是否可以返回文档。

我知道这听起来很奇怪,并且可能违背了 lucene 搜索的目的,但我遇到过这样一种情况:如果用户只是单击“搜索”而不返回前 20 个文档,则首选将前 20 个文档返回给用户,而不是什么也不返回。输入任何搜索词。

所以这就是我的问题:我可以提供什么搜索词 Zend_Search_Lucene 来返回它在没有提供搜索词时遇到的前 20 个文档,而不是看到一些结果而不是看到什么都没有。

我已经做得很好了:

<?php

    Zend_Search_Lucene::setResultSetLimit(20);

    $index = Zend_Search_Lucene::open("some/path/to/index");

    $search_term = trim($_POST["search_term"]);

    if ($search_term == "")
    {
        // adjust the search term to return any documents...
        // will obviously be limited to the first 20...
    }

    $hits = $index->find($search_term);

    // display the results...
    // ...

?>

非常感谢您的宝贵时间以及任何帮助/建议!

I have a fairly new grasp of Zend_Search_Lucene, and how to build and search through documents added to indicies, but I want to know if it is possible to return documents if no search term is provided at all.

I know that sounds strange, and probably goes against the purpose of the lucene search, but I've run into a scenario where returning the first 20 documents to the user is preferred, rather than nothing, if they just click on 'Search' without typing in any search term.

So therein lies my question: what search term could I provide Zend_Search_Lucene that would return the first 20 documents it encounters when no search term is provided to rather see some results vs. seeing nothing at all.

I've already got this working great:

<?php

    Zend_Search_Lucene::setResultSetLimit(20);

    $index = Zend_Search_Lucene::open("some/path/to/index");

    $search_term = trim($_POST["search_term"]);

    if ($search_term == "")
    {
        // adjust the search term to return any documents...
        // will obviously be limited to the first 20...
    }

    $hits = $index->find($search_term);

    // display the results...
    // ...

?>

Thank you so much for your time, and any help / suggestions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜中书 2025-01-09 20:45:00

当没有提供搜索词时,通过搜索 ids 1 到 20 的范围来搜索 20 条记录(但您的文档必须有一个 id 字段)

$from = new Zend_Search_Lucene_Index_Term(1, 'id');
$to   = new Zend_Search_Lucene_Index_Term(20, 'id');
$query = new Zend_Search_Lucene_Search_Query_Range(
             $from, $to, true // inclusive
         );
$hits  = $index->find($query);

when no search term is provided search for the 20 records by searching range of ids 1 to 20 (but your document has to have an id field)

$from = new Zend_Search_Lucene_Index_Term(1, 'id');
$to   = new Zend_Search_Lucene_Index_Term(20, 'id');
$query = new Zend_Search_Lucene_Search_Query_Range(
             $from, $to, true // inclusive
         );
$hits  = $index->find($query);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文