Nutch 是一個(gè)搜索引擎,昨天剛從一個(gè)朋友那里知道,前一陣子接觸了 lucene ,對搜索的東西躍躍欲試,趁著(zhù)周末試用了一把,感覺(jué)蠻新鮮,網(wǎng)上的例子多是基于 0.7 版本的,找到了一些 0.8 的就是跑不起來(lái),忽悠忽悠試了半天,寫(xiě)下一點(diǎn)感覺(jué) ~~
系統環(huán)境: Tomcat
使用過(guò)程:
1. 因為 nutch 的運行需要 unix 環(huán)境,所以對于 windows 用戶(hù),要先下載一個(gè) cygwin ,它是一個(gè)自由軟件,可在 windows 下模擬 unix 環(huán)境,你可以到 http://www.cygwin.com/ 下載在線(xiàn)安裝程序,也可以到 http://www-inst.eecs.berkeley.edu/~instcd/iso/ 下載完整安裝程序(我下下來(lái)有
2. 下載 nutch
3. 在 nutch-
4. 打開(kāi) nutch-
# accept hosts in MY.DOMAIN.NAME
+^http://([a-z0-9]*\.)*MY.DOMAIN.NAME/
紅色部分 是一個(gè)正則,你要搜索的網(wǎng)址要與其匹配,在這里我改為 +^http://([a-z0-9]*\.)*apache.org/
5. OK ,下面開(kāi)始對搜索網(wǎng)址建立索引,運行 cygwin ,會(huì )打開(kāi)一個(gè)命令窗口,輸入 ”cd cygdrive/d/ nutch-
6. 執行 ”bin/nutch crawl urls -dir crawled-depth 2 -threads 5 >& crawl.log”
參數意義如下(來(lái)自 apache 網(wǎng)站 http://lucene.apache.org/nutch/tutorial8.html ):
-dir dir names the directory to put the crawl in.
-threads threads determines the number of threads that will fetch in parallel.
-depth depth indicates the link depth from the root page that should be crawled.
-topN N determines the maximum number of pages that will be retrieved at each level up to the depth.
crawl.log : 日志文件
執行后可以看到 nutch-
① / ② crawldb/ linkdb : web link 目錄,存放 url 及 url 的互聯(lián)關(guān)系,作為爬行與重新爬行的依據,頁(yè)面默認 30 天過(guò)期 (可以在 nutch-site.xml 中配置,后面會(huì )提到)
③ segments : 一存放抓取的頁(yè)面,與上面鏈接深度 depth 相關(guān), depth 設為 2 則在 segments 下生成兩個(gè)以時(shí)間命名的子文件夾,比如 ” 20061014163012” ,打開(kāi)此文件夾可以看到,它下面還有 6 個(gè)子文件夾,分別是(來(lái)自 apache 網(wǎng)站 http://lucene.apache.org/nutch/tutorial8.html ):
crawl_generate : names a set of urls to be fetched
crawl_fetch : contains the status of fetching each url
content : contains the content of each url
parse_text : contains the parsed text of each url
parse_data : contains outlinks and metadata parsed from each url
crawl_parse : contains the outlink urls, used to update the crawldb
④ indexes : 索引目錄,我運行時(shí)生成了一個(gè) ” part
⑤ index : lucene 的索引目錄( nutch 是基于 lucene 的 ,在 nutch-
7. 進(jìn)行簡(jiǎn)單測試,在 cygwin 中輸入 ”bin/nutch org.apache.nutch.searcher.NutchBean apache” ,即調用 NutchBean 的 main 方法搜索關(guān)鍵字 ”apache” ,在 cygwin 可以看到搜索出: Total hits: 29 ( hits 相當于 JDBC 的 results )
注意: 如果發(fā)現搜索結果始終為 0 ,則需要配置一下 nutch-
8. 下面我們要在 Tomcat 下進(jìn)行測試, nutch-
9. 打開(kāi) nutch\WEB-INF\classes 下 nutch-site.xml 文件,下面紅色為需要新增的內容,其他為原 nutch-site.xml 內容
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!-- Put site-specific property overrides in this file. -->
<configuration>
<!-- HTTP properties -->
<property>
<name>http.agent.name</name>
<value>*</value>
<description></description>
</property>
<!-- file properties -->
<property>
<name>searcher.dir</name>
<value>D:\nutch-
<description></description>
</property>
</configuration>
http.agent.name :必須,如果去掉這個(gè) property 查詢(xún)結果始終為 0
searcher.dir :指定前面在 cygwin 中生成的 crawled 路徑
其中我們還可以設置重新爬行時(shí)間(在過(guò)程 6 提到:頁(yè)面默認 30 天過(guò)期 )
<property>
<name>fetcher.max.crawl.delay</name>
<value>30</value>
<description></description>
</property>
另外還有很多參數可以在 nutch-
10. 打開(kāi) http://localhost:8081/nutch ,輸入 ”apache” ,可以看到 ” 共有 29 項查詢(xún)結果 ” ,和上面在過(guò)程 7 進(jìn)行簡(jiǎn)單測試的結果一致
Luke 介紹:
Luke 是查詢(xún) lucene 索引文件的圖形化工具,可以比較直觀(guān)的看到索引創(chuàng )建情況,它需要結合 lucene 包一起用
使用過(guò)程:
1. 下載地址 http://www.getopt.org/luke 它提供 3 種下載:
standalone full JAR : lukeall.jar
standalone minimal JAR : lukemin.jar
separate JARs : luke.jar (~113kB)
lucene-1.9-rc1-dev.jar (~380kB)
analyzers-dev.jar (~348kB)
snowball-1.1-dev.jar (~88kB)
js.jar (~492kB)
我們只需下載 ”separate JARs” 的 luke.jar 即可
2. 下載后新建一個(gè)文件夾,比如叫 ”luke_run” ,把 luke.jar 放在文件夾下,同時(shí)從 nutch-
3. 在 cmd 命令行中轉到 ”luke_run” 目錄,輸入 ” java -classpath luke.jar;lucene-core-
4. 附上一點(diǎn)閑言:)使用中發(fā)現一個(gè)問(wèn)題(在 lucene-core-
Exception in thread "Thread-12" java.lang.NoSuchMethodError: org.apache.lucene.d
ocument.Field.<init>(Ljava/lang/String;Ljava/lang/String;ZZZZ)V
at org.getopt.luke.Luke$2.run(Unknown Source)
呵呵,我用的是 lucene-core-
聯(lián)系客服