说说NUTCH插件

这几天的回顾NUTCH项目的过程中,需要对lastModified进行搜索,这下就痛苦啦,用lucene的用法lastModified:[20110101-20120111]拼命的搜啊,搜啊。无耐国足不给力啊,始终搜不出来。于是只能开源代码,拼命地看,总算找到了插件那里实现了搜索的功能。

每一个基本的非范围搜索都可以由NUTCH来实现,但当我们希望它可以实现范围搜索的时候,我们就需要自己实现插件来完成这个功能。

1)我们查看插件的组织目录,发现NUTCH把很多功能都作为插件来进行插入:

2)我们可以看到conf文件夹内的nutch-default.xml文件,打开它,我们可以看到如下代码:

<property>
  <name>plugin.includes</name>
  <value>protocol-http|urlfilter-regex|parse-(text|html|js|tika)|index-(basic|anchor|more)|query-(basic|site|url|lastModified)|response-(json|xml)|summary-basic|scoring-opic|urlnormalizer-(pass|regex|basic)</value>
  <description>Regular expression naming plugin directory names to
  include.  Any plugin not matching this expression is excluded.
  In any case you need at least include the nutch-extensionpoints plugin. By
  default Nutch includes crawling just HTML and plain text via HTTP,
  and basic indexing and search plugins. In order to use HTTPS please enable 
  protocol-httpclient, but be aware of possible intermittent problems with the 
  underlying commons-httpclient library. Nutch now also includes integration with Tika
  to leverage Tika's parsing capabilities for multiple content types. The existing Nutch
  parser implementations will likely be phased out in the next release or so, as such, it is
  a good idea to begin migrating away from anything not provided by parse-tika.
  </description>
</property>

    如果我们需要添加什么插件,只需要在此处进行添加即可。

3)下面我们进行正题,如何开发NUTCH插件:

就以我开发queryLastModified这个插件来说:

新建一个类,名字随便啦,我这时就命名为LastModifiedQueryFilter,代码如下:

private Configuration conf;

	public LastModifiedQueryFilter() {
		super("lastmodified");
	}

	public Configuration getConf() {
		return this.conf;
	}

	private static final String FIELD_NAME = "lastmodified";

	// query syntax is defined as date:yyyymmdd-yyyymmdd
	private static final Pattern pattern = Pattern
			.compile("(\\d{8})\\s(\\d{8})$");

	public BooleanQuery filter(Query input, BooleanQuery output)
			throws QueryException {
		// examine each clause in the Nutch query
		Clause[] clauses = input.getClauses();

		for (int i = 0; i < clauses.length; i++) {
			Clause c = clauses[i];

			// skip if not date clauses
			if (!c.getField().equals(FIELD_NAME))
				continue;

			String x = c.toString();

			x = x.substring(x.indexOf(":")+1).replaceAll("\"","");
			
			Matcher matcher = pattern.matcher(x);
			if (!matcher.matches()) {
				throw new QueryException("Wrong query syntax " + FIELD_NAME
						+ ":" + x);
			}
			
			// inclusive
			TermRangeQuery rangeQuery = new TermRangeQuery(FIELD_NAME, matcher
					.group(1), matcher.group(2), true, true);
			
			
			rangeQuery.setBoost(0.0f); // trigger filterization

			output.add(rangeQuery, BooleanClause.Occur.MUST);

		}

		return output;
	}

中间那几句代码,我们可以看到它实现上是调用了lucene的TermRangeQuery。类写完后当然就是要写plugin.xml,这是插件的必备文件,它通知NUTCH加载此插件。

文件内容大概如下:

<plugin
   id="query-lastModified"
   name="LastModified Query Filter"
   version="1.0.0"
   provider-name="nutch.org">

   <runtime>
      <library name="query-lastModified.jar">
         <export name="*"/>
      </library>
   </runtime>

   <requires>
      <import plugin="nutch-extensionpoints"/>
   </requires>

   <extension id="org.apache.nutch.searcher.lastModified"
              name="Nutch LastModified Query Filter"
              point="org.apache.nutch.searcher.QueryFilter">
      <implementation id="LastModifiedQueryFilter"
                      class="org.apache.nutch.searcher.lastModified.LastModifiedQueryFilter">
        <parameter name="fields" value="lastmodified"/>
      </implementation>
      
   </extension>

</plugin>

  id那些大家用过spring那些应该都清楚的,是插件的名字。extension中的point是指插件点,是指插件继承哪个类,name只是描述而已。implementation中的id也只是名字而已,而class表明类的具体类名,要包括包名,parameter表明它是针对哪个field,这有有raw-fields和fields两种。这时我暂时没有用到raw-fields,没怎么了解,一般情况下都用fields。

4)当写完配置文件和插件文件后就需要写编译文件build.xml,很简单,只有几行,内容如下:

<project name="query-lastModified" default="jar-core">
  <import file="../build-plugin.xml"/>
</project>

  这里的name表明生成的jar包的名称,jar-core表明生成主要的jar包,由于ant没怎么用过,就先不研究了。

import是指把文件导入,也可以理解成在编译buil-plugin.xml时,会自动到这里进行查找,然后进行编译生成JAR包。

5)这里的配置,我们知道,肯定还要再配置build-plugin.xml。

有两个地方需要添加,<target name="deploy"> 内添加<ant dir="query-lastModified" target="deploy"/>表明它要进行部署。

另外在<target name="clean">内添加<ant dir="query-lastModified" target="clean"/>表明在部署前进行清理。

6)这些东西配置完成之后就需要更改nutch-default,把我们添加的插件配置到前面所讲的plugins.include标签内。

7)这样完成之后我们就可以用ant进行编译生成jar包和job包,拷贝过去覆盖原来的即可。

这样,我们的插件开发就完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值