(说明:使用FieldSelector的LoadFirstSelector;类是可以正确加载的)
代码:
package extral;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.document.LoadFirstFieldSelector;
import org.apache.lucene.document.MapFieldSelector;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
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 FiledSelkectors {
public static void main(String args[]) throws IOException{
File file=new File("D:/unreal_path/all_index/changedir2");
Directory dir=FSDirectory.open(file);
FieldSelector selector=new MapFieldSelector();
selector.accept("content");
selector.accept("datatime");
//LoadFirstFieldSelector load=new LoadFirstFieldSelector();
IndexWriter writer=new IndexWriter(dir,new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);
IndexReader reader=writer.getReader();
IndexSearcher sou=new IndexSearcher(dir);
MatchAllDocsQuery all=new MatchAllDocsQuery();
TopDocs docs=sou.search(all, 10);
Document doc=null;
for(ScoreDoc score : docs.scoreDocs){
doc=sou.doc(score.doc,selector);
List<Fieldable> list=doc.getFields();
Iterator iter=list.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
}
sou.close();
System.out.println("ok");
}
}
@2:
package high;
/**
* Copyright Manning Publications Co.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific lan
*/
import java.io.File;
import java.io.FileWriter;
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.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.Field.TermVector;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.vectorhighlight.BaseFragmentsBuilder;
import org.apache.lucene.search.vectorhighlight.FastVectorHighlighter;
import org.apache.lucene.search.vectorhighlight.FieldQuery;
import org.apache.lucene.search.vectorhighlight.FragListBuilder;
import org.apache.lucene.search.vectorhighlight.FragmentsBuilder;
import org.apache.lucene.search.vectorhighlight.ScoreOrderFragmentsBuilder;
import org.apache.lucene.search.vectorhighlight.SimpleFragListBuilder;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
// From chapter 8
public class FastVectorHighlighterSample {
static final String[] DOCS = { // #A
"the quick brown fox jumps over the lazy dog", // #A
"the quick gold fox jumped over the lazy black dog", // #A
"the quick fox jumps over the black dog", // #A
"the red fox jumped over the lazy dark gray dog" // #A
};
static final String QUERY = "quick OR fox OR \"lazy dog\"~1"; // #B
static final String F = "f";
static Directory dir ;
static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
static IndexWriter writer;
static IndexReader reader;
public static void main(String[] args) throws Exception {
dir=FSDirectory.open(new File("D:/unreal_path/all_index/indexdir5"));
makeIndex();
}
static void makeIndex() throws IOException, ParseException {
writer = new IndexWriter(dir, analyzer, true, MaxFieldLength.UNLIMITED);
for (String d : DOCS) {
Document doc = new Document();
doc.add(new Field(F, d, Store.YES, Index.ANALYZED,
TermVector.WITH_POSITIONS_OFFSETS));
writer.addDocument(doc);
System.out.println("1");
}
writer.close();
IndexSearcher searcher = new IndexSearcher(dir);
IndexReader reader=searcher.getIndexReader();
QueryParser parser = new QueryParser(Version.LUCENE_30, F, analyzer);
Query query = parser.parse(QUERY);
FastVectorHighlighter highlighter = getHighlighter(); // #C
FieldQuery fieldQuery = highlighter.getFieldQuery(query);
// #D
TopDocs docs = searcher.search(query, 10);
// FileWriter writer = new FileWriter(filename);
// writer.write("<html>");
// writer.write("<body>");
// writer.write("<p>QUERY : " + QUERY + "</p>");
for (ScoreDoc scoreDoc : docs.scoreDocs) {
String snippet = highlighter.getBestFragment( // #E
fieldQuery, searcher.getIndexReader(), // #E
scoreDoc.doc, F, 100); // #E
if (snippet != null) {
// writer.write(scoreDoc.doc + " : " + snippet + "<br/>");
}
else{
System.out.println(snippet+"12");
}
}
// writer.write("</body></html>");
// writer.close();
searcher.close();
reader.close();
// writer.commit();
}
static void searchIndex() throws Exception {
}
static FastVectorHighlighter getHighlighter() {
FragListBuilder fragListBuilder = new SimpleFragListBuilder(); // #F
FragmentsBuilder fragmentBuilder = // #F
new ScoreOrderFragmentsBuilder( // #F
BaseFragmentsBuilder.COLORED_PRE_TAGS, // #F
BaseFragmentsBuilder.COLORED_POST_TAGS); // #F
return new FastVectorHighlighter(true, true, // #F
fragListBuilder, fragmentBuilder); // #F
}
}
/*
* #A Index these documents #B Run this query #C Get FastVectorHighlighter #D
* Create FieldQuery #E Highlight top fragment #F Create FastVectorHighlighter
*/
mapfieldselector构造函数没看,哎~~~