List of usage examples for org.apache.lucene.search IndexSearcher doc
public Document doc(int docID) throws IOException
.getIndexReader().document(docID)
From source file:com.andreig.jetty.Search.java
License:GNU General Public License
public Document[] search(String dbid, String k, String v, int count) throws IOException, ParseException { Term t = new Term(k, v); Query q = new TermQuery(t); Query q2 = add_dbid(q, dbid); TopScoreDocCollector collector = TopScoreDocCollector.create(count, true); IndexSearcher searcher = sm.acquire(); Document docs[] = null;//from w w w . j a v a2 s . c om try { searcher.search(q2, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; if (hits.length == 0) return null; docs = new Document[hits.length]; for (int i = 0; i < hits.length; i++) { int doc_id = hits[i].doc; docs[i] = searcher.doc(doc_id); } } finally { sm.release(searcher); } return docs; }
From source file:com.andreig.jetty.Search.java
License:GNU General Public License
public Document[] search2(String dbid, String q, int count) throws IOException, ParseException { Query query = tl.get().parse(QueryParser.escape(q)); Query q2 = add_dbid(query, dbid); TopScoreDocCollector collector = TopScoreDocCollector.create(count, true); IndexSearcher searcher = sm.acquire(); Document docs[] = null;//from ww w.j av a 2s .c om try { searcher.search(q2, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; if (hits.length == 0) return null; docs = new Document[hits.length]; for (int i = 0; i < hits.length; i++) { int doc_id = hits[i].doc; docs[i] = searcher.doc(doc_id); } } finally { sm.release(searcher); } return docs; }
From source file:com.aperigeek.dropvault.web.service.IndexService.java
License:Open Source License
public List<String> search(String username, String password, String query) throws IndexException { try {// w ww. j a v a 2 s.c o m IndexSearcher searcher = getIndexSearcher(username, password); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_33); QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_33, new String[] { "title", "body" }, analyzer); Query luceneQuery = parser.parse(query); TopDocs docs = searcher.search(luceneQuery, 10); List<String> results = new ArrayList<String>(); for (ScoreDoc doc : docs.scoreDocs) { results.add(searcher.doc(doc.doc).getFieldable("id").stringValue()); } searcher.close(); return results; } catch (IOException ex) { throw new IndexException(ex); } catch (ParseException ex) { throw new IndexException("Invalid query syntax", ex); } }
From source file:com.aurel.track.lucene.search.associatedFields.AbstractAssociatedFieldSearcher.java
License:Open Source License
/** * Get the OR separated IDs which match the specified field's user entered string * @param analyzer/*from w w w . j av a 2s.co m*/ * @param fieldName * @param fieldValue * @param fieldID * @param locale * @return */ @Override protected String searchExplicitField(Analyzer analyzer, String fieldName, String fieldValue, Integer fieldID, Locale locale) { IndexSearcher indexSearcher = null; try { Query query = getAssociatedFieldQuery(analyzer, fieldValue); if (query == null) { return fieldValue; } indexSearcher = LuceneSearcher.getIndexSearcher(getIndexSearcherID()); if (indexSearcher == null) { return fieldValue; } ScoreDoc[] scoreDocs; try { TopDocsCollector<ScoreDoc> collector = TopScoreDocCollector.create(LuceneSearcher.MAXIMAL_HITS); indexSearcher.search(query, collector); scoreDocs = collector.topDocs().scoreDocs; } catch (IOException e) { LOGGER.warn("Searching the " + getLuceneFieldName() + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); return fieldValue; } if (scoreDocs == null || scoreDocs.length == 0) { return fieldValue; } if (scoreDocs.length > LuceneSearcher.MAX_BOOLEAN_CLAUSES) { LOGGER.warn("Maximum number of boolean clauses was exceeded"); } Set<Integer> workItemIDs = new HashSet<Integer>(); for (int i = 0; i < scoreDocs.length; i++) { int docID = scoreDocs[i].doc; Document doc; try { doc = indexSearcher.doc(docID); } catch (IOException e) { LOGGER.error("Getting the documents from index searcher for " + getLuceneFieldName() + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); return fieldValue; } String workItemFieldName = getWorkItemFieldName(); String workItemIDStr = doc.get(workItemFieldName); Integer workItemID = null; if (workItemIDStr != null) { try { workItemID = Integer.valueOf(workItemIDStr); workItemIDs.add(workItemID); } catch (Exception e) { LOGGER.debug(e); } } //by links there are two workitems for bidirectional links String additionalWorkItemFieldName = getAdditionalWorkItemFieldName(doc); if (additionalWorkItemFieldName != null) { workItemIDStr = doc.get(additionalWorkItemFieldName); workItemID = null; if (workItemIDStr != null) { try { workItemID = Integer.valueOf(workItemIDStr); workItemIDs.add(workItemID); } catch (Exception e) { } } } } return LuceneSearcher.createORDividedIDs(workItemIDs); } catch (Exception e) { LOGGER.warn("Getting the " + getLuceneFieldName() + " field " + fieldValue + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); return fieldValue; } finally { LuceneSearcher.closeIndexSearcherAndUnderlyingIndexReader(indexSearcher, getLuceneFieldName()); } }
From source file:com.aurel.track.lucene.search.listFields.AbstractListFieldSearcher.java
License:Open Source License
/** * Finds the list options which match the user entered string in link descriptions * @param analyzer/*from www . j a va 2 s. com*/ * @param fieldName * @param label * @param fieldID * @param locale * @return */ @Override protected String searchExplicitField(Analyzer analyzer, String fieldName, String label, Integer fieldID, Locale locale) { IndexSearcher indexSearcher = null; try { Query query = getExplicitFieldQuery(analyzer, fieldName, label, fieldID, locale); if (query == null) { return label; } indexSearcher = LuceneSearcher.getIndexSearcher(getIndexSearcherID()); if (indexSearcher == null) { return label; } ScoreDoc[] scoreDocs; try { TopDocsCollector<ScoreDoc> collector = TopScoreDocCollector.create(LuceneSearcher.MAXIMAL_HITS); indexSearcher.search(query, collector); scoreDocs = collector.topDocs().scoreDocs; } catch (IOException e) { LOGGER.warn("Searching by fieldName " + fieldName + " and fieldValue " + label + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); return label; } if (scoreDocs == null || scoreDocs.length == 0) { return label; } if (scoreDocs.length > LuceneSearcher.MAX_BOOLEAN_CLAUSES) { LOGGER.warn("Maximum number of boolean clauses was exceeded"); } Set<Integer> listOptionIDs = new HashSet<Integer>(); for (int i = 0; i < scoreDocs.length; i++) { int docID = scoreDocs[i].doc; Document doc; try { doc = indexSearcher.doc(docID); } catch (IOException e) { LOGGER.error("Getting the documents from index searcher for fieldName " + fieldName + " and fieldValue " + label + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); return label; } String listOptionIDStr = doc.get(getValueFieldName()); Integer listOptionID = null; if (listOptionIDStr != null) { try { listOptionID = Integer.valueOf(listOptionIDStr); listOptionIDs.add(listOptionID); } catch (Exception e) { } } } return LuceneSearcher.createORDividedIDs(listOptionIDs); } catch (Exception e) { LOGGER.error("Getting the fieldName " + fieldName + " and fieldValue " + label + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); return label; } finally { LuceneSearcher.closeIndexSearcherAndUnderlyingIndexReader(indexSearcher, fieldName); } }
From source file:com.aurel.track.lucene.search.listFields.AbstractListFieldSearcher.java
License:Open Source License
/** * Get the workItemIDs which match the user entered string * @param analyzer/*from w ww .ja v a 2 s .c o m*/ * @param fieldValue * @param locale * @return */ @Override protected String searchNoExplicitField(Analyzer analyzer, String toBeProcessedString, Locale locale) { IndexSearcher indexSearcher = null; Map<Integer, Set<Integer>> result = new HashMap<Integer, Set<Integer>>(); try { Query lookupQuery = getNoExlplicitFieldQuery(analyzer, toBeProcessedString, locale); if (lookupQuery == null) { return ""; } indexSearcher = LuceneSearcher.getIndexSearcher(getIndexSearcherID()); if (indexSearcher == null) { return ""; } ScoreDoc[] scoreDocs; try { TopDocsCollector<ScoreDoc> collector = TopScoreDocCollector.create(LuceneSearcher.MAXIMAL_HITS); indexSearcher.search(lookupQuery, collector); scoreDocs = collector.topDocs().scoreDocs; } catch (IOException e) { return ""; } if (scoreDocs == null || scoreDocs.length == 0) { return ""; } if (scoreDocs.length > LuceneSearcher.MAX_BOOLEAN_CLAUSES) { LOGGER.warn("Maximum number of boolean clauses was exceeded by not localized lookup"); } Document doc; for (int i = 0; i < scoreDocs.length; i++) { int docID = scoreDocs[i].doc; try { doc = indexSearcher.doc(docID); } catch (IOException e) { LOGGER.error("Getting the documents from index searcher for fieldValue " + toBeProcessedString + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); return ""; } String typeStr = doc.get(getTypeFieldName()); String idStr = doc.get(getValueFieldName()); Integer type = null; Integer id = null; try { type = Integer.valueOf(typeStr); id = Integer.valueOf(idStr); Set<Integer> ids = result.get(type); if (ids == null) { ids = new HashSet<Integer>(); result.put(type, ids); } ids.add(id); } catch (NumberFormatException ex) { continue; } } } catch (Exception ex) { } finally { LuceneSearcher.closeIndexSearcherAndUnderlyingIndexReader(indexSearcher, "no field"); } Set<Integer> types = result.keySet(); StringBuffer directQuery = new StringBuffer(); for (Integer type : types) { Set<Integer> ids = result.get(type); String orDividedIDs = LuceneSearcher.createORDividedIDs(ids); String[] workItemFieldNames = getWorkItemFieldNames(type); for (int i = 0; i < workItemFieldNames.length; i++) { if (i > 0) { directQuery.append(" OR "); } if (ids.size() > 1) { directQuery.append(workItemFieldNames[i] + LuceneSearcher.FIELD_NAME_VALUE_SEPARATOR + "(" + orDividedIDs + ")"); } else { directQuery.append( workItemFieldNames[i] + LuceneSearcher.FIELD_NAME_VALUE_SEPARATOR + orDividedIDs); } } } return directQuery.toString(); }
From source file:com.aurel.track.lucene.search.LuceneSearcher.java
License:Open Source License
private static int[] getQueryResults(Query query, String userQueryString, String preprocessedQueryString, Map<Integer, String> highlightedTextMap) { int[] hitIDs = new int[0]; IndexSearcher indexSearcher = null; try {/* w w w . ja va2s . c o m*/ long start = 0; if (LOGGER.isDebugEnabled()) { start = new Date().getTime(); } indexSearcher = getIndexSearcher(LuceneUtil.INDEXES.WORKITEM_INDEX); if (indexSearcher == null) { return hitIDs; } ScoreDoc[] scoreDocs; try { TopDocsCollector<ScoreDoc> collector = TopScoreDocCollector.create(MAXIMAL_HITS); indexSearcher.search(query, collector); scoreDocs = collector.topDocs().scoreDocs; } catch (IOException e) { LOGGER.warn("Getting the workitem search results failed with failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); return hitIDs; } if (LOGGER.isDebugEnabled()) { long end = new Date().getTime(); LOGGER.debug("Found " + scoreDocs.length + " document(s) (in " + (end - start) + " milliseconds) that matched the user query '" + userQueryString + "' the preprocessed query '" + preprocessedQueryString + "' and the query.toString() '" + query.toString() + "'"); } QueryScorer queryScorer = new QueryScorer(query/*, LuceneUtil.HIGHLIGHTER_FIELD*/); Fragmenter fragmenter = new SimpleSpanFragmenter(queryScorer); Highlighter highlighter = new Highlighter(queryScorer); // Set the best scorer fragments highlighter.setTextFragmenter(fragmenter); // Set fragment to highlight hitIDs = new int[scoreDocs.length]; for (int i = 0; i < scoreDocs.length; i++) { int docID = scoreDocs[i].doc; Document doc = null; try { doc = indexSearcher.doc(docID); } catch (IOException e) { LOGGER.error("Getting the workitem documents failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } if (doc != null) { Integer itemID = Integer.valueOf(doc.get(LuceneUtil.getFieldName(SystemFields.ISSUENO))); if (itemID != null) { hitIDs[i] = itemID.intValue(); if (highlightedTextMap != null) { String highligherFieldValue = doc.get(LuceneUtil.HIGHLIGHTER_FIELD); TokenStream tokenStream = null; try { tokenStream = TokenSources.getTokenStream(LuceneUtil.HIGHLIGHTER_FIELD, null, highligherFieldValue, LuceneUtil.getAnalyzer(), -1); } catch (Exception ex) { LOGGER.debug(ex.getMessage()); } if (tokenStream != null) { String fragment = highlighter.getBestFragment(tokenStream, highligherFieldValue); if (fragment != null) { highlightedTextMap.put(itemID, fragment); } } } } } } return hitIDs; } catch (BooleanQuery.TooManyClauses e) { LOGGER.error("Searching the query resulted in too many clauses. Try to narrow the query results. " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); throw e; } catch (Exception e) { LOGGER.error("Searching the workitems failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); return hitIDs; } finally { closeIndexSearcherAndUnderlyingIndexReader(indexSearcher, "workItem"); } }
From source file:com.b2international.snowowl.snomed.api.impl.ClassificationRunIndex.java
License:Apache License
private List<Document> search(final Query query, final int limit) throws IOException { IndexSearcher searcher = null; try {//w w w. j a v a 2 s. c o m searcher = manager.acquire(); final TopDocs docs = searcher.search(query, limit, Sort.INDEXORDER, false, false); final ImmutableList.Builder<Document> resultBuilder = ImmutableList.builder(); for (final ScoreDoc scoreDoc : docs.scoreDocs) { resultBuilder.add(searcher.doc(scoreDoc.doc)); } return resultBuilder.build(); } finally { if (null != searcher) { manager.release(searcher); } } }
From source file:com.barchart.feed.ddf.resolver.provider.ResolverDDF.java
License:BSD License
private List<Document> searchDocument(final Query query) throws Exception { final IndexSearcher searcher = getSearcher(); final TopScoreDocCollector collector = TopScoreDocCollector.create(limit, true); searcher.search(query, collector);/*from ww w. ja v a2 s . c o m*/ final ScoreDoc[] hits = collector.topDocs().scoreDocs; final int size = Math.min(hits.length, limit); log.debug("hits size : {}", size); final List<Document> list = new ArrayList<Document>(size); for (int k = 0; k < size; k++) { final int index = hits[k].doc; final Document doc = searcher.doc(index); list.add(doc); } return list; }
From source file:com.basistech.lucene.tools.LuceneQueryTool.java
License:Apache License
private void runQuery(String queryString, final PrintStream out) throws IOException, org.apache.lucene.queryparser.classic.ParseException { final IndexSearcher searcher = new IndexSearcher(indexReader); docsPrinted = 0;// w ww . ja va 2s.c o m Query query; if (queryString == null) { query = new MatchAllDocsQuery(); } else { if (!queryString.contains(":") && defaultField == null) { throw new RuntimeException("query has no ':' and no query-field defined"); } QueryParser queryParser = new QueryParser(defaultField, analyzer); queryParser.setLowercaseExpandedTerms(false); query = queryParser.parse(queryString).rewrite(indexReader); Set<Term> terms = Sets.newHashSet(); query.createWeight(searcher, false).extractTerms(terms); List<String> invalidFieldNames = Lists.newArrayList(); for (Term term : terms) { if (!allFieldNames.contains(term.field())) { invalidFieldNames.add(term.field()); } } if (!invalidFieldNames.isEmpty()) { throw new RuntimeException("Invalid field names: " + invalidFieldNames); } } final Set<String> fieldSet = Sets.newHashSet(fieldNames); // use a Collector instead of TopDocs for memory efficiency, especially // for the %all query class MyCollector extends SimpleCollector { private Scorer scorer; private long totalHits; private int docBase; @Override protected void doSetNextReader(LeafReaderContext context) throws IOException { docBase = context.docBase; } @Override public void collect(int id) throws IOException { totalHits++; if (docsPrinted >= outputLimit) { return; } id += docBase; Document doc = fieldSet.isEmpty() ? searcher.doc(id) : searcher.doc(id, fieldSet); boolean passedFilter = regexField == null; if (regexField != null) { String value = doc.get(regexField); if (value != null && regex.matcher(value).matches()) { passedFilter = true; } } if (passedFilter) { float score = scorer.score(); printDocument(doc, id, score, out); } } @Override public boolean needsScores() { return true; } @Override public void setScorer(Scorer scorer) throws IOException { this.scorer = scorer; } } MyCollector collector = new MyCollector(); searcher.search(query, collector); if (showHits) { out.println("totalHits: " + collector.totalHits); out.println(); } }