【昆山人在上海】

生在昆山_人在上海

用户操作
[即时聊天] [发私信] [加为好友]
月影传说ID:kunshan_shenbin
51319次访问,排名2299,好友48人,关注者44人。
暂无
kunshan_shenbin的文章
原创 314 篇
翻译 1 篇
转载 60 篇
评论 54 篇
最近评论
For_suzhen:说错了
number 的小数部分大于等于 0.5,返回值是大于 number 的最小整数
是这一句。呵呵
For_suzhen:如果是-1.6 应该返回是 -2还是-1呢?
是-2
======================
返回与给出的数值表达式最接近的整数。
=====================
这句话不对,最接近是数轴意义上的最接近吗?
kunshan_shenbin:注意:
keystorePass="changeit" 中密码changeit是可以改变的。
如果需要改变的话,所有执行的命令行里面都需要改变。
kunshan_shenbin:有一点需要注意:
如果你更改了Apache配置文件httpd.conf中的DocumentRoot以及其对应的Directory以更改工程发布的默认路径的话,在extra/httpd-ssl.conf中DocumentRoot也同样需要更改。
其他属性,如ServerName等也是一样的。
kunshan_shenbin:html,doc,execl,pdf是富文本文档,不做转换的话Lunece很难对他们进行索引。
换句话说,在你对他们进行索引之前需要先把他们的内容转换成纯文本的格式。
转换的工具我倒是可以推荐一些:
html:
NekoHTML (http://sourceforge.net/projects/nekohtml)
JTidy (http://……
文章分类
收藏
    相册
    新婚九寨之旅
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 使用Lucene2.4时的一些注意点(新版本的修改点)收藏

    新一篇: 重拾Compass:全文检索Lucene的最佳伴侣 | 旧一篇: SQL研究(MS-SQL)之二:分组显示

    Lucene2.4版本已经在很多地方有了修改,可能是为了迎接Lucene3.0的到来所做的调整吧。
    以前写的Blog(http://blog.csdn.net/kunshan_shenbin/archive/2008/06/02/2503388.aspx)在2.3.2版本下测试通过,但在2.4下必须修改。

    代码如下:

    Indexer.java

    1. package com.lucene.index;
    2. import java.io.File;
    3. import java.io.FileReader;
    4. import java.io.IOException;
    5. import org.apache.lucene.analysis.standard.StandardAnalyzer;
    6. import org.apache.lucene.document.Document;
    7. import org.apache.lucene.document.Field;
    8. import org.apache.lucene.index.IndexWriter;
    9. public class Indexer {
    10.     public static void main(String[] args) throws IOException {
    11.         File indexDir = new File("C:\\test\\index");
    12.         File dataDir = new File("C:\\test\\data");
    13.         int numIndexed = index(indexDir, dataDir);
    14.         System.out.println(numIndexed);
    15.     }
    16.     public static int index(File indexDir, File dataDir) throws IOException {
    17.         if (!indexDir.exists() || !dataDir.isDirectory()) {
    18.             throw new IOException();
    19.         }
    20.         IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
    21.         writer.setUseCompoundFile(false);
    22.         indexDirectory(writer, dataDir);
    23.         int numIndexed = writer.maxDoc();
    24.         writer.optimize();
    25.         writer.close();
    26.         return numIndexed;
    27.     }
    28.     private static void indexDirectory(IndexWriter writer, File dir)
    29.             throws IOException {
    30.         File[] files = dir.listFiles();
    31.         for (int i = 0; i < files.length; i++) {
    32.             File f = files[i];
    33.             if (f.isDirectory()) {
    34.                 indexDirectory(writer, f);
    35.             } else if (f.getName().toLowerCase().endsWith(".txt")) {
    36.                 indexFile(writer, f);
    37.             }
    38.         }
    39.     }
    40.     public static void indexFile(IndexWriter writer, File f) throws IOException {
    41.         if (f.isHidden() || !f.exists() || !f.canRead()) {
    42.             return;
    43.         }
    44.         System.out.println("Indexing " + f.getCanonicalPath());
    45.         Document doc = new Document();
    46.         doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES,
    47.                 Field.Index.NOT_ANALYZED));
    48.         doc.add(new Field("contents"new FileReader(f)));
    49.         writer.addDocument(doc);
    50.     }
    51. }

    Searcher.java

    1. package com.lucene.search;
    2. import java.io.File;
    3. import java.io.IOException;
    4. import org.apache.lucene.analysis.standard.StandardAnalyzer;
    5. import org.apache.lucene.document.Document;
    6. import org.apache.lucene.queryParser.QueryParser;
    7. import org.apache.lucene.search.IndexSearcher;
    8. import org.apache.lucene.search.Query;
    9. import org.apache.lucene.search.ScoreDoc;
    10. import org.apache.lucene.search.TopDocs;
    11. import org.apache.lucene.store.Directory;
    12. import org.apache.lucene.store.FSDirectory;
    13. public class Searcher {
    14.     public static void main(String[] args) throws Exception {
    15.         File indexDir = new File("C:\\test\\index");
    16.         String q = "111";
    17.         if (!indexDir.exists() || !indexDir.isDirectory()) {
    18.             throw new IOException();
    19.         }
    20.         search(indexDir, q);
    21.     }
    22.     public static void search(File indexDir, String q) throws Exception {
    23.         Directory fsDir = FSDirectory.getDirectory(indexDir);
    24.         IndexSearcher searcher = new IndexSearcher(fsDir);
    25.         QueryParser parser = new QueryParser("contents"new StandardAnalyzer());
    26.         Query query = parser.parse(q);
    27.         
    28.         TopDocs topDocs = searcher.search(query, 100);//100是显示队列的Size
    29.         ScoreDoc[] hits = topDocs.scoreDocs;
    30.         System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length + "条");
    31.         for (int i = 0; i < hits.length; i++) {
    32.             int DocId = hits[i].doc;
    33.             Document document = searcher.doc(DocId);
    34.             System.out.println(DocId + ":" + document.get("filename"));
    35.         }
    36.     }
    37. }

    发表于 @ 2008年10月28日 11:07:00|评论(loading...)|收藏

    新一篇: 重拾Compass:全文检索Lucene的最佳伴侣 | 旧一篇: SQL研究(MS-SQL)之二:分组显示

    评论

    #liuzhihao0619 发表于2008-11-20 17:16:02  IP: 222.131.50.*
    谢谢分享
    但是我照抄了你的,不过是路径换了一下,数据路径E:\\bak,索引路径E:\\index,而且索引创建成功了.但是搜索的时候有问题...我那里面是exe文件和一些bat,mis啥的,我在索引里创建的Field是path(文件路径)和fileName(文件名)但是,在搜索的时候keyword比如是一个w,无论是以哪个字段检索,都是maxDoc个文件,0条命中..郁闷了好久了..

    您有这样的情况吗?或者是知道该怎么弄么.
    我尝试过全部用txt文件,同样
    我尝试过改为2.2版本的JAR和写法,同样
    我尝试过用其他方式查询比如TermQuery,同样
    ......
    望赐教,谢谢
    E-mail:sofaaaaa@gmail.com
    QQ:56789340
    不胜感激!
    #liuzhihao0619 发表于2008-11-20 17:16:25  IP: 222.131.50.*
    谢谢分享
    但是我照抄了你的,不过是路径换了一下,数据路径E:\\bak,索引路径E:\\index,而且索引创建成功了.但是搜索的时候有问题...我那里面是exe文件和一些bat,mis啥的,我在索引里创建的Field是path(文件路径)和fileName(文件名)但是,在搜索的时候keyword比如是一个w,无论是以哪个字段检索,都是maxDoc个文件,0条命中..郁闷了好久了..

    您有这样的情况吗?或者是知道该怎么弄么.
    我尝试过全部用txt文件,同样
    我尝试过改为2.2版本的JAR和写法,同样
    我尝试过用其他方式查询比如TermQuery,同样
    ......
    望赐教,谢谢
    E-mail:sofaaaaa@gmail.com
    QQ:56789340
    不胜感激!
    #liuzhihao0619 发表于2008-11-20 18:04:48  IP: 222.131.50.*
    sorry,重复提交了.没注意~
    #kunshan_shenbin 发表于2008-11-21 16:58:06  IP: 218.242.150.*
    Lucene只能检索文本信息或者能够转换为文本的文件资源。把.exe,mis啥的作为数据源进行索引没有意义的。
    至于全部用txt文件还是没有办法进行正常的查找的话的确奇怪了。我回去试试看吧。
    #kunshan_shenbin 发表于2008-11-21 16:59:20  IP: 218.242.150.*
    你可以先看看这个:
    http://blog.csdn.net/kunshan_shenbin/archive/2008/11/07/3247337.aspx
    #kunshan_shenbin 发表于2008-11-21 17:11:32  IP: 218.242.150.*
    刚才做了一个简单的尝试:
    C:\test\data下2个txt文件
    --------------------------------
    1.txt
    内容:
    hello world very munch
    --------------------------------
    2.txt
    内容:
    very good hello world
    --------------------------------
    注意:
    C:\test\index是需要手工新建的,不然会报异常。
    当然路径变换是没有关系的,请自行确认。

    Searcher.java的main函数中修改查询条件,譬如:
    ......
    String q = "very";
    ......

    输出的结果为:
    共有2条索引,命中2条
    0:C:\test\data\1.txt
    1:C:\test\data\2.txt

    你可以尝试一下看看能不能匹配到数据。
    #liuzhihao0619 发表于2008-11-21 17:21:32  IP: 123.119.210.*
    感谢您的解答,其实下面的代码跟我写的东西没什么太大区别..但是一句话我记住了:
    Lucene只能检索文本信息或者能够转换为文本的文件资源。把.exe,mis啥的作为数据源进行索引没有意义的。

    有这句就知道了.
    #kunshan_shenbin 发表于2008-11-22 10:43:11  IP: 121.227.214.*
    @liuzhihao0619
    你的来信我已经收到了。你的疑问:
    ----------------------------------------------------------
    可是我现在想要实现的一个功能:

    把一个目录下所有无论什么类型的文件,exe,msi,txt,docx...把他们索引起来

    然后在一个页面上进行检索,类似百度硬盘搜索的那么一个东西,根据关键字搜索相关的文件,显示他们的文件名和路径.

    您说Lucene只能检索文本信息或者能够转换为文本的文件资源,这句话我的理解是:

    Lucene并非简单的根据给定的关键字和字段名称去索引中检索相关的数据.而且跟被索引文件的格式有关系.

    可是我在创建索引的时候只是把这些文件的路径和文件名进行了索引,这也会有关系吗?
    ----------------------------------------------------------
    其实你需要的功能只要对上面的代码稍作修改即可实现。索引在建立时,在contents段中放入文件路径即可(上诉代码中通过doc.add(new Field("contents", new FileReader(f))); 放入了文件的文本信息)。
    修改如下:
    ================================
    private static void indexDirectory(IndexWriter writer, File dir)
    throws IOException {

    File[] files = dir.listFiles();

    for (int i = 0; i < files.length; i++) {
    File f = files[i];
    if (f.isDirectory()) {
    indexDirectory(writer, f);
    //} else if (f.getName().toLowerCase().endsWith(".txt")) {
    } else {
    indexFile(writer, f);
    }
    }
    }

    public static void indexFile(IndexWriter writer, File f) throws IOException {

    if (f.isHidden() || !f.exists() || !f.canRead()) {
    return;
    }

    System.out.println("Indexing " + f.getName());

    #kunshan_shenbin 发表于2008-11-22 10:44:04  IP: 121.227.214.*
    Document doc = new Document();
    doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES,
    Field.Index.NOT_ANALYZED));
    //doc.add(new Field("contents", new FileReader(f)));
    doc.add(new Field("contents", f.getName(), Field.Store.YES,
    Field.Index.ANALYZED));
    writer.addDocument(doc);
    }
    #kunshan_shenbin 发表于2008-11-22 10:51:57  IP: 121.227.214.*
    提示一点:
    查询时请用文件全名,包括后缀。
    因为在索引建立时只使用了StandardAnalyzer分词器。
    #liuzhihao0619 发表于2008-11-22 11:00:14  IP: 221.220.224.*
    非常感谢!

    StandardAnalyzer分词器是不支持模糊查询的,对吗?

    我马上测试一下...

    非常感谢!
    #liuzhihao0619 发表于2008-11-22 11:06:39  IP: 221.220.224.*
    乖乖...真的有效~~~
    你是怎么做到的!!!

    原来这个问题出在分词器上,还是容我先去仔细研究一下各个分词器的适用范围好了...

    谢谢大哥如此耐心,热情的指教!
    #liuzhihao0619 发表于2008-11-22 11:10:26  IP: 221.220.224.*
    另外我发现:
    doc.add(new Field("fileName", file.getName(), Field.Store.YES, Field.Index.ANALYZED));
    这里Field.Index参数一定要是ANALYZED,如果改为NOT_....
    就不好使了
    #liuzhihao0619 发表于2008-11-22 11:12:06  IP: 221.220.224.*
    是不是可以理解为
    只有ANALYZED的Field才能够作为query field去做搜索呢?
    #kunshan_shenbin 发表于2008-11-22 11:56:55  IP: 121.227.214.*
    是的,Field的构造函数后两个参数,一个是表明是否存储,后一个是表明是否需要使用分词器进行分析。
    #kunshan_shenbin 发表于2008-11-22 12:12:09  IP: 121.227.214.*
    提示一点:
    Lucene默认的分词器都是针对英文或与之类似语种的。
    中文分词器推荐你用庖丁解牛:
    http://groups.google.com/group/paoding/browse_thread/thread/9771c8d495786fee
    #liuzhihao0619 发表于2008-11-22 16:13:53  IP: 123.113.229.*
    呵呵,昨天尝试了一下庖丁解牛,但是因为刚刚接触,很多东西还不理解,我觉得我还是按部就班一点一点研究吧.

    真是多谢您了.

    2.4用的人很少,文档又残缺不够规范,谢谢哈.以后还要多向您请教!
    #fengsky491 发表于2008-12-06 09:22:36  IP: 121.204.188.*
    您好,我在data目录下放html文件,怎么检索不到,
    输出:共有0条索引,命中0条

    是不是像html,doc,execl,pdf等,都需要专门的解析器,还是查询器,能帮帮我吗?
    #kunshan_shenbin 发表于2008-12-08 08:54:19  IP: 58.209.26.*
    html,doc,execl,pdf是富文本文档,不做转换的话Lunece很难对他们进行索引。
    换句话说,在你对他们进行索引之前需要先把他们的内容转换成纯文本的格式。
    转换的工具我倒是可以推荐一些:
    html:
    NekoHTML (http://sourceforge.net/projects/nekohtml)
    JTidy (http://jtidy.sourceforge.net/)
    --------------------------------------------------------
    doc, excel:
    POI (http://poi.apache.org/)
    当然,个人感觉Jxl对Excel的支持也不错。
    请参阅:http://blog.csdn.net/kunshan_shenbin/archive/2007/12/08/1924279.aspx
    --------------------------------------------------------
    pdf:
    PdfBox (http://www.pdfbox.org/)

    当然,有一点令我很疑惑,仅就Html而言,他的源代码也是纯文本的格式的(这个和其他的几种类型不同)。按理就算不进行解析也能被索引到的。
    我写的代码中因为是限定了文件的后缀的:
    ......
    } else if (f.getName().toLowerCase().endsWith(".txt")) {
    ......
    你检查一下是不是你的代码里也是这样写的。这样写的话只有后缀是.txt的文件才能被索引。
    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © 月影传说