package com.chineseall.kevoo;import java.io.File;
import java.io.IOException;import jeasy.analysis.MMAnalyzer;import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;public class Y_index { public static void main(String args[]) {
String str = "国土资源部执法监察局局长李建勤在国土部新闻发布会上表示:国土部等14个部委已经联合成立小产权房整治处理领导小组,目前正在研究制订小产权房处理政策方案。对2007年12月30日以前销售的已建成违规房屋,待全国小产权房处理政策出台后一并处理。";
Directory dir;
try {
dir = FSDirectory.open(new File("d:/temp"));
//dir = new RAMDirectory();
//Analyzer any = new ChineseAnalyzer();
Analyzer any = new MMAnalyzer();
IndexWriter iw = new IndexWriter(dir, any, true, IndexWriter.MaxFieldLength.UNLIMITED);
Document doc = new Document();
Field field = new Field("text", str.getBytes(), Field.Store.YES);
doc.add(field);
iw.addDocument(doc);
iw.close();
IndexSearcher is = new IndexSearcher(FSDirectory.open(new File("d:/temp")), true);
QueryParser qp = new QueryParser(Version.LUCENE_29, "text", any);
Query q = qp.parse("资源");
Hits hits = is.search(q);
System.out.println("命中:" + hits.length());
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
为什么命中的是0?
import java.io.IOException;import jeasy.analysis.MMAnalyzer;import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;public class Y_index { public static void main(String args[]) {
String str = "国土资源部执法监察局局长李建勤在国土部新闻发布会上表示:国土部等14个部委已经联合成立小产权房整治处理领导小组,目前正在研究制订小产权房处理政策方案。对2007年12月30日以前销售的已建成违规房屋,待全国小产权房处理政策出台后一并处理。";
Directory dir;
try {
dir = FSDirectory.open(new File("d:/temp"));
//dir = new RAMDirectory();
//Analyzer any = new ChineseAnalyzer();
Analyzer any = new MMAnalyzer();
IndexWriter iw = new IndexWriter(dir, any, true, IndexWriter.MaxFieldLength.UNLIMITED);
Document doc = new Document();
Field field = new Field("text", str.getBytes(), Field.Store.YES);
doc.add(field);
iw.addDocument(doc);
iw.close();
IndexSearcher is = new IndexSearcher(FSDirectory.open(new File("d:/temp")), true);
QueryParser qp = new QueryParser(Version.LUCENE_29, "text", any);
Query q = qp.parse("资源");
Hits hits = is.search(q);
System.out.println("命中:" + hits.length());
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
为什么命中的是0?
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;public class Y_index { public static void main(String args[]) {
String str = "国土资源部执法监察局局长李建勤在国土部新闻发布会上表示:国土部等14个部委已经联合成立小产权房整治处理领导小组,目前正在研究制订小产权房处理政策方案。对2007年12月30日以前销售的已建成违规房屋,待全国小产权房处理政策出台后一并处理。";
Directory dir;
try {
dir = FSDirectory.getDirectory(new File("d:/temp"));
// Analyzer any = new MMAnalyzer();
Analyzer any = new StandardAnalyzer();
IndexWriter iw = new IndexWriter(dir, any, true); Document doc = new Document();
Field field = new Field("text", str, Field.Store.YES,
Field.Index.TOKENIZED);
doc.add(field);
iw.addDocument(doc);
iw.close(); IndexSearcher is = new IndexSearcher("d:/temp");
QueryParser qp = new QueryParser("text", any);
Query q = qp.parse("资源");
Hits hits = is.search(q);
System.out.println("命中:" + hits.length()); } catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} }
}
这样就可以了