LUNECE1.X与2.X版本差异导致编译错误的通用解决办法

 由一封邮件引出的解决方案
Lucene in Action examples complie problem
原文地址:
http://www.nabble.com/Lucene-in-Action-examples-complie-problem-to6742147.html
l    Lucene in Action examples complie problem
by Serhiy Polyakov Oct 11, 2006; 03:13am
Hi, I started to study Lucene following the book Lucene in Action. I am trying to compile book examples downloaded from the book site: http://www.manning.com/hatcher2/ When I am trying to compile first example (Indexer.java) it gives me the following error: LuceneInAction/src/lia/meetlucene/Indexer.java:80: cannot find symbol symbol  : method Text(java.lang.String,java.io.FileReader) location: class org.apache.lucene.document.Field
doc.add(Field.Text("contents", new FileReader(f))); I know that file lucene-core-2.0.0.jar is in CLASSPATH and other classes like org.apache.lucene.index.IndexWriter are working OK. The only class I am having problem with is org.apache.lucene.document.Field; Can you suggest something? Thanks, Serhiy
邮件的发起者提出的问题其实就是 1.X 版本的代码在 2.X 的类库下产生的编译错误问题
 
l    Re: Lucene in Action examples complie problem
by Erik Hatcher Oct 11, 2006; 03:05am
I have long been meaning to publish an updated codebase for Lucene in Action compatible with Lucene 2.0. I adjusted the code a while ago, but I haven't published it yet (and I think the plan is to wait until LIA2 is finished to do so). I did make notes when I made these changes, and I'm pasting them below for all to benefit from.
Ø        Replace all BooleanQuery.add's, e.g.:
    - subjectQuery.add(tq, false, false);
    + subjectQuery.add(tq, BooleanClause.Occur.SHOULD);
Ø        Substitute RangeFilter for DateFilter usage, e.g.:
    - DateFilter filter = new DateFilter("modified",jan1, dec31);
    + RangeFilter filter = new RangeFilter("modified",jan1,dec31, true, true);
NOTE: The dates are now String's generated by DateUtils.dateToString() and incompatible with DateField
Ø        Replace all Field.Keyword/UnStored/Text/UnIndexed with the enumerated types, e.g.:
    -   doc.add(Field.Keyword("animal", animal));
    +   doc.add(newField("animal",animal,Field.Store.YES,
Field.Index.UN_TOKENIZED));
Ø        Rename PhrasePrefixQuery -> MultiPhraseQuery
Ø        Use instance of QueryParser instead of static parse method, e.g.:
    - Query query = QueryParser.parse(expression, "contents", analyzer);
    + Query query = new QueryParser("contents", analyzer).parse (expression)
Ø        QueryParser subclasses adjusted for overridden getXXXQuery method signatures.
Ø        IndexReader.delete() updated to be IndexReader.deleteDocument/.deleteDocuments()
Ø        IndexWriter internal configuration values now accessed through getters/setters rather than the fields directly, with minMergeDocs renamed as setMaxBufferedDocs().
Ø        QueryParser.setLowercaseWildcardTerms() replaced with .setLowercaseExpandedTerms()
Ø        QueryParser.getRangeQuery() still uses DateField when constructing a RangeQuery. If your index is built using DateTools, you will need to subclass and override, as shown in QueryParserTest.testRangeQuery().
Erik Hatcher 给出了 1.X 2.X 版本一些主要的区别 .
 
l    Re: Lucene in Action examples complie problem
By Doron Cohen Oct 11, 2006; 04:26am
I wonder if this should be in the FAQ entry "How do i get code written for Lucene 1.4.x to work with Lucene 2.x", Or perhaps just adding there a link to your post here - http://www.nabble.com/Lucene-in-Action-examples-complie-problem-tf2418478.html#a6743189    Doron Cohen 提出该问题的答案应该可以从 LUCENE FAQ 中找到
 
 
l    Re: Lucene in Action examples complie problem
by Doron Cohen Oct 11, 2006; 03:32am
Field.Text()  was deprecated in Lucene 1.9 and then removed in 2.0. The book examples were not updated for 2.0 yet. You should now use Field(String, String, Field.Store, Field.Index). To have the same behavior as old Field.Text use: Field(name, value, Field.Store.YES, Field.Index.TOKENIZED). For other release issues - see the FAQ entry http://wiki.apache.org/jakarta-lucene/LuceneFAQ#head-d09fdfc8a6335eab4e3f3dc8ac41a40a3666318e Regards, Doron
Doron 的答案基本一致——查看 FAQ ,并给出 FAQ 地址
 
FAQ 中关于此问题的解答:
原文地址:
How do I get code written for Lucene 1.4.x to work with Lucene 2.x?
The upgrade path for Lucene 2.0 was designed around the notion of clear deprecation warnings. Any code designed to use the APIs in Lucene 1.4.x should compile/function with Lucene 1.9 -- however many compile time deprecation warnings will be generated identifying methods that should no longer be used, and what new methods should be used instead.
If you have code that worked with Lucene 1.4.x, and you want to "port" it to Lucene 2.x you should start by downloading the 1.9 release of Lucene, and compile the code against it. Make sure deprecation warnings are turned on in your development environment, and gradually change your code until all deprecation warnings go away (the DateField class is an exception, it has not been removed in Lucene 2.0 yet).
At that point, your code should work fine with Lucene 2.x.
If you are looking at example code (in an article or book perhaps) and just need to understand how the example would change to work with 2.0 (without needing to actually compile it) you can review the javadocs for Lucene 1.9 and lookup any methods used in the examples that are no longer part of Lucene. The 1.9 javadocs will have a clear deprecation message explaining how to get the same effect using the 2.x methods.
答案很简单:把LUCENE 1.9 的java doc文档下载下来,碰到1.X的代码在2.X类库下编译不过的情况,只要从1.9的帮助文档中查看相应的类或方法,必定是1.9中Deprecated(不鼓励使用)的类或方法(这些类或方法从2.0版本开始被删除,因此会产生编译错),同时在文档中会给出正确的使用方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值