publicclassAnalyzingInfixSuggesterextendsLookupimplementsCloseable {//建立索引的域名/** Field name used for the indexed text. */protectedfinalstatic String TEXT_FIELD_NAME = "text";
//查找关键词的域名/** Field name used for the indexed text, as a
* StringField, for exact lookup. */protectedfinalstatic String EXACT_TEXT_FIELD_NAME = "exacttext";
//用于过滤的域名/** Field name used for the indexed context, as a
* StringField and a SortedSetDVField, for filtering. */protectedfinalstatic String CONTEXTS_FIELD_NAME = "contexts";
/** Analyzer used at search time */protectedfinal Analyzer queryAnalyzer;
/** Analyzer used at index time */protectedfinal Analyzer indexAnalyzer;
final Version matchVersion;
//最小前缀长度,防止数量太多,内存溢出privatefinal Directory dir;
finalint minPrefixChars;
//是否全部关键词都需要匹配上privatefinalboolean allTermsRequired;
privatefinalboolean highlight;
privatefinalboolean commitOnBuild;
/** Used for ongoing NRT additions/updates. */private IndexWriter writer;
/** {@link IndexSearcher} used for lookups. */protected SearcherManager searcherMgr;
....
}
建立索引
@Overridepublicvoidbuild(InputIterator iter) throws IOException {
if (searcherMgr != null) {
searcherMgr.close();
searcherMgr = null;
}
if (writer != null) {
writer.close();
writer = null;
}
boolean success = false;
try {
// First pass: build a temporary normal Lucene index,// just indexing the suggestions as they iterate:
writer = new IndexWriter(dir,
getIndexWriterConfig(getGramAnalyzer(), IndexWriterConfig.OpenMode.CREATE));
//long t0 = System.nanoTime();// TODO: use threads?
BytesRef text;
while ((text = iter.next()) != null) {
BytesRef payload;
if (iter.hasPayloads()) {
payload = iter.payload();
} else {
payload = null;
}
add(text, iter.contexts(), iter.weight(), payload);
}
//System.out.println("initial indexing time: " + ((System.nanoTime()-t0)/1000000) + " msec");if (commitOnBuild) {
commit();
}
searcherMgr = new SearcherManager(writer, null);
success = true;
} finally {
if (success == false && writer != null) {
writer.rollback();
writer = null;
}
}
}
InputIterator
publicinterfaceInputIteratorextendsBytesRefIterator {//权重/** A term's weight, higher numbers mean better suggestions. */publiclongweight();
//payload是用来存储一个额外信息,并以字节byte[]的形式写入索引中,当搜索返回后,你可以通过LookupResult结果对象的payload属性获取到该值/** An arbitrary byte[] to record per suggestion. See
* {@link LookupResult#payload} to retrieve the payload
* for each suggestion. */public BytesRef payload();
/** Returns true if the iterator has payloads */publicbooleanhasPayloads();
/**
* A term's contexts context can be used to filter suggestions.
* May return null, if suggest entries do not have any context
* */public Set<BytesRef> contexts();
/** Returns true if the iterator has contexts */publicbooleanhasContexts();
...
}