package lucene_index;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.print.Doc;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class MainTest {
public static void main(String[] args) throws IOException, ParseException {
LineIterator it = FileUtils.lineIterator(new File("E://searchwork_custom//data_index//ConfigFile//ExpansionWord.csv"),"gbk");
Map<String, String []> map = new HashMap<String, String[]>();
while (it.hasNext()) {
String word = it.nextLine();
String [] wordArr = word.replace("-,", "").trim().split("\\,");
if(map.containsKey(wordArr[0]))
continue;
map.put(wordArr[0], wordArr);
}
Analyzer analyzer = new StopWrodsAnalyse(map);
Directory directory = FSDirectory.open(new File("E:\\luceneindex"));
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_48, analyzer);
IndexWriter writer = new IndexWriter(directory, config);
Collection<Document> coll = new ArrayList<Document>();
for (Map.Entry<String, String []> entry : map.entrySet()) {
Document docss = new Document();
Field field = new Field("name", entry.getKey(),Store.YES,Index.ANALYZED);
docss.add(field);
coll.add(docss);
}
writer.addDocuments(coll);
writer.commit();
writer.close();
IndexSearcher searcher = new IndexSearcher(IndexReader.open(FSDirectory.open(new File("E:\\luceneindex"))));
// QueryParser parser = new QueryParser(Version.LUCENE_48, "name", analyzer);
search(searcher);
//WordInfo.getWordInfo(word, analyzer);
}
public static void search(IndexSearcher searcher) throws IOException{
Query q = new TermQuery(new Term("name","中国建设银行"));
System.out.println(q);
TopDocs doc = searcher.search(q, 10);
ScoreDoc [] docs = doc.scoreDocs;
for (int i = 0; i < docs.length; i++) {
Document d = searcher.doc(docs[i].doc);
System.out.println(d.get("name"));
}
}
}