use of org.apache.lucene.queryparser.classic.ParseException in project zeppelin by apache.
the class LuceneSearch method query.
/* (non-Javadoc)
* @see org.apache.zeppelin.search.Search#query(java.lang.String)
*/
@Override
public List<Map<String, String>> query(String queryStr) {
if (null == ramDirectory) {
throw new IllegalStateException("Something went wrong on instance creation time, index dir is null");
}
List<Map<String, String>> result = Collections.emptyList();
try (IndexReader indexReader = DirectoryReader.open(ramDirectory)) {
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
Analyzer analyzer = new StandardAnalyzer();
MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] { SEARCH_FIELD_TEXT, SEARCH_FIELD_TITLE }, analyzer);
Query query = parser.parse(queryStr);
LOG.debug("Searching for: " + query.toString(SEARCH_FIELD_TEXT));
SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));
result = doSearch(indexSearcher, query, analyzer, highlighter);
indexReader.close();
} catch (IOException e) {
LOG.error("Failed to open index dir {}, make sure indexing finished OK", ramDirectory, e);
} catch (ParseException e) {
LOG.error("Failed to parse query " + queryStr, e);
}
return result;
}
use of org.apache.lucene.queryparser.classic.ParseException in project OpenGrok by OpenGrok.
the class SearchHelper method prepareExec.
/**
* Create the searcher to use wrt. to currently set parameters and the given
* projects. Does not produce any {@link #redirect} link. It also does
* nothing if {@link #redirect} or {@link #errorMsg} have a
* none-{@code null} value.
* <p>
* Parameters which should be populated/set at this time: <ul>
* <li>{@link #builder}</li> <li>{@link #dataRoot}</li>
* <li>{@link #order} (falls back to relevance if unset)</li>
* <li>{@link #parallel} (default: false)</li> </ul> Populates/sets: <ul>
* <li>{@link #query}</li> <li>{@link #searcher}</li> <li>{@link #sort}</li>
* <li>{@link #projects}</li> <li>{@link #errorMsg} if an error occurs</li>
* </ul>
*
* @param projects project to use query. If empty, a no-project setup
* is assumed (i.e. DATA_ROOT/index will be used instead of possible
* multiple DATA_ROOT/$project/index).
* @return this instance
*/
public SearchHelper prepareExec(SortedSet<String> projects) {
if (redirect != null || errorMsg != null) {
return this;
}
// the Query created by the QueryBuilder
try {
indexDir = new File(dataRoot, IndexDatabase.INDEX_DIR);
query = builder.build();
if (projects == null) {
errorMsg = "No project selected!";
return this;
}
this.projects = projects;
if (projects.isEmpty()) {
// no project setup
FSDirectory dir = FSDirectory.open(indexDir.toPath());
searcher = new IndexSearcher(DirectoryReader.open(dir));
closeOnDestroy = true;
} else {
// We use MultiReader even for single project. This should
// not matter given that MultiReader is just a cheap wrapper
// around set of IndexReader objects.
closeOnDestroy = false;
MultiReader multireader = RuntimeEnvironment.getInstance().getMultiReader(projects, searcherList);
if (multireader != null) {
searcher = new IndexSearcher(multireader);
} else {
errorMsg = "Failed to initialize search. Check the index.";
}
}
// Most probably they are not reused. SearcherLifetimeManager might help here.
switch(order) {
case LASTMODIFIED:
sort = new Sort(new SortField(QueryBuilder.DATE, SortField.Type.STRING, true));
break;
case BY_PATH:
sort = new Sort(new SortField(QueryBuilder.FULLPATH, SortField.Type.STRING));
break;
default:
sort = Sort.RELEVANCE;
break;
}
checker = new DirectSpellChecker();
} catch (ParseException e) {
errorMsg = PARSE_ERROR_MSG + e.getMessage();
} catch (FileNotFoundException e) {
// errorMsg = "Index database(s) not found: " + e.getMessage();
errorMsg = "Index database(s) not found.";
} catch (IOException e) {
errorMsg = e.getMessage();
}
return this;
}
use of org.apache.lucene.queryparser.classic.ParseException in project titan by thinkaurelius.
the class LuceneIndex method query.
@Override
public Iterable<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
Query q;
try {
q = new QueryParser("_all", analyzer).parse(query.getQuery());
} catch (ParseException e) {
throw new PermanentBackendException("Could not parse raw query: " + query.getQuery(), e);
}
try {
IndexSearcher searcher = ((Transaction) tx).getSearcher(query.getStore());
//Index does not yet exist
if (searcher == null)
return ImmutableList.of();
long time = System.currentTimeMillis();
//TODO: can we make offset more efficient in Lucene?
final int offset = query.getOffset();
int adjustedLimit = query.hasLimit() ? query.getLimit() : Integer.MAX_VALUE - 1;
if (adjustedLimit < Integer.MAX_VALUE - 1 - offset)
adjustedLimit += offset;
else
adjustedLimit = Integer.MAX_VALUE - 1;
TopDocs docs = searcher.search(q, adjustedLimit);
log.debug("Executed query [{}] in {} ms", q, System.currentTimeMillis() - time);
List<RawQuery.Result<String>> result = new ArrayList<RawQuery.Result<String>>(docs.scoreDocs.length);
for (int i = offset; i < docs.scoreDocs.length; i++) {
result.add(new RawQuery.Result<String>(searcher.doc(docs.scoreDocs[i].doc).getField(DOCID).stringValue(), docs.scoreDocs[i].score));
}
return result;
} catch (IOException e) {
throw new TemporaryBackendException("Could not execute Lucene query", e);
}
}
use of org.apache.lucene.queryparser.classic.ParseException in project textdb by TextDB.
the class FuzzyTokenMatcherSourceOperator method createLuceneQueryObject.
public static Query createLuceneQueryObject(FuzzyTokenPredicate predicate) throws DataFlowException {
try {
/*
* By default the boolean query takes 1024 # of clauses as the max
* limit. Since our input query has no limitaion on the number of
* tokens, we have to put a check.
*/
if (predicate.getThreshold() > 1024)
BooleanQuery.setMaxClauseCount(predicate.getThreshold() + 1);
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.setMinimumNumberShouldMatch(predicate.getThreshold());
MultiFieldQueryParser qp = new MultiFieldQueryParser(predicate.getAttributeNames().stream().toArray(String[]::new), LuceneAnalyzerConstants.getLuceneAnalyzer(predicate.getLuceneAnalyzerStr()));
for (String s : predicate.getQueryTokens()) {
builder.add(qp.parse(s), Occur.SHOULD);
}
return builder.build();
} catch (ParseException e) {
throw new DataFlowException(e);
}
}
use of org.apache.lucene.queryparser.classic.ParseException in project geode by apache.
the class LuceneIndexCreationIntegrationTest method canCreateLuceneIndexForRegionWithEvictionWithOverflowToDisk.
@Test
public void canCreateLuceneIndexForRegionWithEvictionWithOverflowToDisk() throws IOException, ParseException {
try {
createIndex("field1", "field2", "field3");
RegionFactory regionFactory = this.cache.createRegionFactory(RegionShortcut.PARTITION);
regionFactory.setEvictionAttributes(EvictionAttributes.createLRUHeapAttributes(null, EvictionAction.OVERFLOW_TO_DISK));
regionFactory.create(REGION_NAME);
} catch (Exception e) {
fail("Should have been able to create Lucene Index");
e.printStackTrace();
}
}
Aggregations