本文整理汇总了Java中org.apache.lucene.analysis.WhitespaceAnalyzer类的典型用法代码示例。如果您正苦于以下问题:Java WhitespaceAnalyzer类的具体用法?Java WhitespaceAnalyzer怎么用?Java WhitespaceAnalyzer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WhitespaceAnalyzer类属于org.apache.lucene.analysis包,在下文中一共展示了WhitespaceAnalyzer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getIndexWriter
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
@Override
protected IndexWriter getIndexWriter() {
if (++useCount % 1000 == 0) {
close();
writer = null;
}
if (writer == null) {
try {
boolean createIndex = true;
File testIndexPath = new File(indexPath);
if (!testIndexPath.exists())
testIndexPath.mkdirs();
if (testIndexPath.isDirectory()) {
if (testIndexPath.list().length > 0)
createIndex = false;
writer = new IndexWriter(indexPath, new WhitespaceAnalyzer(), createIndex);
}
} catch (Exception ex) {
log.fatal(ex);
}
}
return writer;
}
示例2: select
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
/**
* Executes a select query.
* @param query The Lucene query
* @return An array of record IDs; an empty array if an error occured
* @throws IOException
*/
public int[] select(String query, String sortPrefix) throws ParseException{
try {
WhitespaceAnalyzer sa= new WhitespaceAnalyzer();
BooleanQuery.setMaxClauseCount(20000);//zbog heap-a
QueryParser p = new QueryParser("KW", sa);
p.setDefaultOperator(QueryParser.Operator.AND); //default operator je AND a ne OR kao sto je inace inicijalno
Query q = p.parse(query);
return select(q, sortPrefix);
} catch (Exception ex) {
if (ex instanceof ParseException )
throw (ParseException)ex;
log.warn(ex);
return new int[0];
}
}
示例3: getIndexWriter
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
/**
* Returns a new Lucene index writer. Creates the index if necessary.
* @return
*/
protected IndexWriter getIndexWriter() {
try {
boolean createIndex = true;
File testIndexPath = new File(indexPath);
if (!testIndexPath.exists())
testIndexPath.mkdirs();
if (testIndexPath.isDirectory()) {
if (testIndexPath.list().length > 0)
createIndex = false;
return new IndexWriter(indexPath, new WhitespaceAnalyzer(), createIndex);
}
} catch (Exception ex) {
log.fatal(ex);
}
return null;
}
示例4: init
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
@Override
public void init() throws IOException, IllegalStateException {
Files.createDirectories(Paths.get(mentionsFolder));
Files.createDirectories(Paths.get(resourcesFolder));
writingOperations.put(KS.RESOURCE, new AtomicInteger(0));
writingOperations.put(KS.MENTION, new AtomicInteger(0));
try {
writers.put(KS.RESOURCE, new IndexWriter(FSDirectory.open(new File(resourcesFolder)), new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED));
writers.put(KS.MENTION, new IndexWriter(FSDirectory.open(new File(mentionsFolder)), new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED));
// writers.get(KS.RESOURCE).setUseCompoundFile(true);
// writers.get(KS.MENTION).setUseCompoundFile(true);
writers.get(KS.RESOURCE).optimize(MAX_LUCENE_SEGMENTS);
writers.get(KS.MENTION).optimize(MAX_LUCENE_SEGMENTS);
readers.put(KS.RESOURCE, writers.get(KS.RESOURCE).getReader());
readers.put(KS.MENTION, writers.get(KS.MENTION).getReader());
} catch (Exception e) {
logger.error(e.getMessage());
}
}
示例5: clearIndex
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
protected static boolean clearIndex(File path) {
try {
if (path == null || !path.exists())
return true; // if path doesnt exist, then there is nothing to
// clear
FSDirectory indexDir = new FSDirectoryProvider().getDirectory();
IndexWriter writer = new IndexWriter(indexDir.open(path),
new IndexWriterConfig(Version.LUCENE_CURRENT,
new WhitespaceAnalyzer(Version.LUCENE_CURRENT)));
writer.deleteAll();
writer.close();
return true;
} catch (Exception ex) {
org.webdsl.logging.Logger.error(
"Error while clearing index on location: " + path, ex);
return false;
}
}
示例6: setAutoCompleteIndex
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
/**
* Use a different index as the auto completer index or re-open
* the existing index if <code>autocompleteIndex</code> is the same value
* as given in the constructor.
* @param autocompleteIndexDir the autocomplete directory to use
* @throws AlreadyClosedException if the Autocompleter is already closed
* @throws IOException if autocompleter can not open the directory
*/
// TODO: we should make this final as it is called in the constructor
public void setAutoCompleteIndex(Directory autocompleteIndexDir) throws IOException {
// this could be the same directory as the current autocompleteIndex
// modifications to the directory should be synchronized
synchronized (modifyCurrentIndexLock) {
ensureOpen();
if (!IndexReader.indexExists(autocompleteIndexDir)) {
IndexWriter writer = new IndexWriter(autocompleteIndexDir,
new IndexWriterConfig(Version.LUCENE_CURRENT,
new WhitespaceAnalyzer(Version.LUCENE_CURRENT)));
writer.close();
}
swapSearcher(autocompleteIndexDir);
}
}
示例7: setUp
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
protected void setUp() throws Exception {
Directory directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory,
new WhitespaceAnalyzer(Version.LUCENE_41),
IndexWriter.MaxFieldLength.UNLIMITED);
Document doc1 = new Document();
doc1.add(new Field("field",
"the quick brown fox jumped over the lazy dog",
Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc1);
Document doc2 = new Document();
doc2.add(new Field("field",
"the fast fox hopped over the hound",
Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc2);
writer.close();
searcher = new IndexSearcher(directory);
}
示例8: setUp
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
protected void setUp() throws Exception {
directory = new RAMDirectory();
IndexWriter writer =
new IndexWriter(directory, new WhitespaceAnalyzer(Version.LUCENE_41),
IndexWriter.MaxFieldLength.UNLIMITED);
addPoint(writer, "El Charro", "restaurant", 1, 2);
addPoint(writer, "Cafe Poca Cosa", "restaurant", 5, 9);
addPoint(writer, "Los Betos", "restaurant", 9, 6);
addPoint(writer, "Nico's Taco Shop", "restaurant", 3, 8);
writer.close();
searcher = new IndexSearcher(directory);
query = new TermQuery(new Term("type", "restaurant"));
}
示例9: testAnalyzer
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
public void testAnalyzer() throws Exception {
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_41);
String queryString = "category:/philosophy/eastern";
Query query = new QueryParser(Version.LUCENE_41,
"contents",
analyzer).parse(queryString);
assertEquals("path got split, yikes!",
"category:\"philosophy eastern\"",
query.toString("contents"));
PerFieldAnalyzerWrapper perFieldAnalyzer =
new PerFieldAnalyzerWrapper(analyzer);
perFieldAnalyzer.addAnalyzer("category",
new WhitespaceAnalyzer(Version.LUCENE_41));
query = new QueryParser(Version.LUCENE_41,
"contents",
perFieldAnalyzer).parse(queryString);
assertEquals("leave category field alone",
"category:/philosophy/eastern",
query.toString("contents"));
}
示例10: createAnalyzer
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
public static Analyzer createAnalyzer() {
final PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper(new KeywordAnalyzer());
analyzer.addAnalyzer(DocumentUtil.FIELD_IDENTS, new WhitespaceAnalyzer());
analyzer.addAnalyzer(DocumentUtil.FIELD_FEATURE_IDENTS, new WhitespaceAnalyzer());
analyzer.addAnalyzer(DocumentUtil.FIELD_CASE_INSENSITIVE_FEATURE_IDENTS, new DocumentUtil.LCWhitespaceAnalyzer());
return analyzer;
}
示例11: getQueryFilter
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
public static Filter getQueryFilter(String query){
try{
WhitespaceAnalyzer sa= new WhitespaceAnalyzer();
QueryParser p = new QueryParser("contents",sa);
Query q = p.parse(query);
Filter filter = new QueryWrapperFilter(q);
return filter;
}catch (Exception e){
return null;
}
}
示例12: selectExpand
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
public List<String> selectExpand(String query, String prefix,String text){
try {
WhitespaceAnalyzer sa= new WhitespaceAnalyzer();
BooleanQuery.setMaxClauseCount(Integer.MAX_VALUE);
QueryParser p = new QueryParser("contents", sa);
Query q = p.parse(query);
Searcher searcher = new IndexSearcher(indexPath);
StopWatch clock=new StopWatch();
clock.start();
Hits hits = searcher.search(q);
int n = hits.length();
List <String> expandList = new ArrayList<String>();
Field[] tmp = null;
String pom="";
for (int i = 0; i < n; i++) {
tmp = hits.doc(i).getFields(prefix);
if (tmp != null){
for (int j = 0; j<tmp.length; j++){
pom=tmp[j].stringValue().replace("0start0 ", "");
pom=pom.replace(" 0end0", "");
if(pom.startsWith(text)&&(!expandList.contains(pom))){
expandList.add(pom);
}
}
}
}
clock.stop();
searcher.close();
return expandList;
} catch (Exception ex) {
log.fatal(ex);
return null;
}
}
示例13: loadModel
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
@Override
public void loadModel(String modelPath) throws IOException {
docsSearcher = new IndexSearcher((new File(modelPath, "docs"))
.getAbsolutePath());
String[] fields = { "doc_id", "content", "user_id", "tag" };
queryParser = new MultiFieldQueryParser(fields,
new WhitespaceAnalyzer());
}
示例14: clearIndex
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
/**
* Removes all terms from the auto complete index.
* @throws IOException
* @throws AlreadyClosedException if the Autocompleter is already closed
*/
public void clearIndex() throws IOException {
synchronized (modifyCurrentIndexLock) {
ensureOpen();
final Directory dir = this.autoCompleteIndex;
final IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(
Version.LUCENE_CURRENT,
new WhitespaceAnalyzer(Version.LUCENE_CURRENT))
.setOpenMode(OpenMode.CREATE));
writer.close();
swapSearcher(dir);
}
}
示例15: indexDictionary
import org.apache.lucene.analysis.WhitespaceAnalyzer; //导入依赖的package包/类
/**
* Indexes the data from the given reader.
* @param reader Source index reader, from which autocomplete words are obtained for the defined field
* @param field the field of the source index reader to index for autocompletion
* @param mergeFactor mergeFactor to use when indexing
* @param ramMB the max amount or memory in MB to use
* @param optimize whether or not the autocomplete index should be optimized
* @throws AlreadyClosedException if the Autocompleter is already closed
* @throws IOException
*/
public final void indexDictionary(IndexReader reader, String field, int mergeFactor, int ramMB, boolean optimize) throws IOException {
synchronized (modifyCurrentIndexLock) {
ensureOpen();
final Directory dir = this.autoCompleteIndex;
final Dictionary dict = new LuceneDictionary(reader, field);
final IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(Version.LUCENE_CURRENT, new WhitespaceAnalyzer(Version.LUCENE_CURRENT)).setRAMBufferSizeMB(ramMB));
IndexSearcher indexSearcher = obtainSearcher();
final List<IndexReader> readers = new ArrayList<IndexReader>();
if (searcher.maxDoc() > 0) {
ReaderUtil.gatherSubReaders(readers, searcher.getIndexReader());
}
//clear the index
writer.deleteAll();
try {
Iterator<String> iter = dict.getWordsIterator();
while (iter.hasNext()) {
String word = iter.next();
// ok index the word
Document doc = createDocument(word, reader.docFreq(new Term(field, word)));
writer.addDocument(doc);
}
} finally {
releaseSearcher(indexSearcher);
}
// close writer
if (optimize)
writer.optimize();
writer.close();
// also re-open the autocomplete index to see our own changes when the next suggestion
// is fetched:
swapSearcher(dir);
}
}