Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl Plucene索引搜索_Perl_Lucene - Fatal编程技术网

Perl Plucene索引搜索

Perl Plucene索引搜索,perl,lucene,Perl,Lucene,在Perl Plucene模块上玩得更多,在创建了索引之后,我现在尝试搜索它并返回结果 我创建索引的代码在这里…您可以跳过此代码继续阅读: #usr/bin/perl use Plucene::Document; use Plucene::Document::Field; use Plucene::Index::Writer; use Plucene::Analysis::SimpleAnalyzer; use Plucene::Search::HitCollector; use Plucene

在Perl Plucene模块上玩得更多,在创建了索引之后,我现在尝试搜索它并返回结果

我创建索引的代码在这里…您可以跳过此代码继续阅读:

#usr/bin/perl
use Plucene::Document;
use Plucene::Document::Field;
use Plucene::Index::Writer;
use Plucene::Analysis::SimpleAnalyzer;
use Plucene::Search::HitCollector;
use Plucene::Search::IndexSearcher;
use Plucene::QueryParser;
use Try::Tiny;
my $content = $ARGV[0];
my $doc = Plucene::Document->new;
my $i=0;
$doc->add(Plucene::Document::Field->Text(content => $content));
my $analyzer = Plucene::Analysis::SimpleAnalyzer->new();

if (!(-d "solutions" )) {
        $i = 1;
}

if ($i)
{
    my $writer = Plucene::Index::Writer->new("solutions", $analyzer, 1); #Third param is 1 if creating new index, 0 if adding to existing
    $writer->add_document($doc);
    my $doc_count = $writer->doc_count;
    undef $writer; # close
}
else
{
    my $writer = Plucene::Index::Writer->new("solutions", $analyzer, 0);
    $writer->add_document($doc);
    my $doc_count = $writer->doc_count;
    undef $writer; # close
}
它创建了一个名为“solutions”的文件夹和各种文件…我假设我创建的文档有索引文件。现在我想搜索我的索引…但我没有找到任何东西。以下是我的尝试,以Plucene::CPAN的简单示例为指导。这是在我使用命令行中的参数“lol”运行上述操作之后

#usr/bin/perl  

  use Plucene::Simple;

  my $plucy = Plucene::Simple->open("solutions");
  my @ids = $plucy->search("content : lol"); 
  foreach(@ids)
  {
    print $_;
  }

遗憾的是,没有打印任何内容。我觉得查询索引应该很简单,但也许我自己的愚蠢限制了我这样做的能力。

我及时发现了三件事:

  • Plucene是一个非常低效的概念验证工具,如果您打算使用这个工具,那么Lucene的Java实现是一个很好的选择。这里有一些证据:
  • Lucy是一个优秀的选择,做同样的事情,并且有更多的文档和社区(根据对这个问题的评论)
  • 如何做我在这个问题上的要求
我将共享两个脚本——一个用于将文件导入新的Plucene索引,另一个用于搜索该索引并检索它。一个真正有效的Plucen示例…在互联网上很难找到它。此外,我在注册这些模块时遇到了巨大的困难…所以我最终去了注册会计师网站(只是谷歌),获取了tar并将它们放在我的Perl库中(我在草莓Perl上,Windows 7上),不管多么随意。然后我会尝试运行它们,并对它所需要的所有依赖项进行CPAN。这是一种草率的做事方式……但我就是这样做的,现在它起作用了

#usr/bin/perl
use strict;
use warnings;
use Plucene::Simple;
my $content_1 = $ARGV[0];
my $content_2 = $ARGV[1];
my %documents;

 %documents = (
"".$content_2 => { 

                     content => $content_1
                   }
);

print $content_1;
my $index = Plucene::Simple->open( "solutions" );
for my $id (keys %documents) 
{
        $index->add($id => $documents{$id});
}
 $index->optimize;
那么这是怎么做的呢?您使用两个您选择的命令行参数调用脚本-它创建一个形式为“second argument”=>“first argument”的键值对。可以像apache站点教程中的XMLs那样考虑这一点(http://lucene.apache.org/solr/api/doc-files/tutorial.html). 第二个参数是字段名

不管是谁,这将在脚本运行所在的目录中创建一个文件夹-该文件夹中的文件将由lucene创建-这是您的索引!!我们现在需要做的就是使用Lucene的强大功能搜索索引,Plucene让搜索变得简单。脚本如下所示:

#usr/bin/perl  
use strict;
use warnings;
use Plucene::Simple;
my $content_1 = $ARGV[0];
my $index = Plucene::Simple->open( "solutions" );


my (@ids, $error);
my $query = $content_1;
@ids = $index->search($query);
foreach(@ids)
{
    print $_."---seperator---";
}

通过使用一个参数从命令行调用该脚本来运行该脚本——例如,为了方便起见,让它成为与调用上一个脚本相同的第一个参数。如果您这样做,您将看到它打印了前面示例中的第二个参数!因此,您已检索到该值!如果您有其他具有相同值的键值对,这也将打印它们!在他们之间有一个“---分隔符----”

我在上一篇文章中提到了这一点,但值得重复。(née KinoSearch)是一个比雨生期更好的选择。它的速度提高了50倍,并且正在积极开发中,开发人员反应非常迅速,社区也相当不错。