Perl Search::Elasticsearch模块使用经验总结
在搭建Elasticsearch数据库的过程中,首先使用了其推荐的Logstash工具导入数据,但是用起来非常不顺手,所以想用Perl擅长的正则表达式对数据进行过滤分类,然后导入Elasticsearch,于是搜索CPAN找到了Search::Elasticsearch模块。
该模块在CPAN上的文档写的比较简洁,于是将使用过程中的经验总结如下:
一、逐条数据写入:
useSearch::Elasticsearch;my$e=Search::Elasticsearch->new(nodes=>['localhost:9200']);$e->index(index=>"$index_name",type=>"$type_name",id=>"$id_name",body=>{title=>"$data_name",data=>"$data"});
二、批量数据写入:
useSearch::Elasticsearch;my$e=Search::Elasticsearch->new(nodes=>['localhost:9200']);my$bulk=$e->bulk_helper(index=>"$index_name",type=>"$type_name");my$i=0;while(...){#dosomething$bulk->add_action(index=>{id=>$id_name,source=>{title=>$data_name,data=>$data}});if($i>999){$bulk->flush;$i=0;}$i++;}
三、读取一条记录:
useSearch::Elasticsearch;my$e=Search::Elasticsearch->new(nodes=>['localhost:9200']);my$doc=$e->get(index=>"$index_name",type=>"$type_name",id=>"$id_name");my$data=$doc->{_source}->{$data_name};#dosomething
四、依次读取全部记录:
useSearch::Elasticsearch;my$e=Search::Elasticsearch->new(nodes=>['localhost:9200']);my$scroll=$e->scroll_helper(index=>"$index_name",type=>"$type_name",body=>{query=>{match_all=>{}},size=>5000});while(my$doc=$scroll->next){my$id=$doc->{_id};my$data=$doc->{_source}->{$data_name};#dosomething}
五、跳转到第$n条数据开始读取
my$doc=$scroll->next($n);
六、基本数据查询
usestrict;useSearch::Elasticsearch;my$e=Search::Elasticsearch->new(nodes=>['localhost:9200']);my$results=$e->search(index=>$index_name,body=>{query=>{query_string=>{query=>$search}}});print$results->{hits}->{hits}->[0]->{_source}->{word};
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。