List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:org.apache.jackrabbit.core.query.lucene.SearchIndex.java
License:Apache License
/** * Executes the query on the search index. * * @param session the session that executes the query. * @param queryImpl the query impl. * @param query the lucene query. * @param orderProps name of the properties for sort order. * @param orderSpecs the order specs for the sort order properties. * <code>true</code> indicates ascending order, * <code>false</code> indicates descending. * @param orderFuncs functions for the properties for sort order. * @param resultFetchHint a hint on how many results should be fetched. @return the query hits. * @throws IOException if an error occurs while searching the index. *///w w w . j a va 2s . com public MultiColumnQueryHits executeQuery(SessionImpl session, AbstractQueryImpl queryImpl, Query query, Path[] orderProps, boolean[] orderSpecs, String[] orderFuncs, long resultFetchHint) throws IOException { checkOpen(); Sort sort = new Sort(createSortFields(orderProps, orderSpecs, orderFuncs)); final IndexReader reader = getIndexReader(queryImpl.needsSystemTree()); JackrabbitIndexSearcher searcher = new JackrabbitIndexSearcher(session, reader, getContext().getItemStateManager()); searcher.setSimilarity(getSimilarity()); return new FilterMultiColumnQueryHits( searcher.execute(query, sort, resultFetchHint, QueryImpl.DEFAULT_SELECTOR_NAME)) { public void close() throws IOException { try { super.close(); } finally { Util.closeOrRelease(reader); } } }; }
From source file:org.apache.jackrabbit.oak.plugins.index.lucene.LucenePropertyIndex.java
License:Apache License
private static Sort getSort(IndexPlan plan) { List<OrderEntry> sortOrder = plan.getSortOrder(); if (sortOrder == null || sortOrder.isEmpty()) { return null; }//from www. ja va 2 s . c om List<SortField> fieldsList = newArrayListWithCapacity(sortOrder.size()); PlanResult planResult = getPlanResult(plan); for (int i = 0; i < sortOrder.size(); i++) { OrderEntry oe = sortOrder.get(i); if (!isNativeSort(oe)) { PropertyDefinition pd = planResult.getOrderedProperty(i); boolean reverse = oe.getOrder() != OrderEntry.Order.ASCENDING; String propName = oe.getPropertyName(); propName = FieldNames.createDocValFieldName(propName); fieldsList.add(new SortField(propName, toLuceneSortType(oe, pd), reverse)); } } if (fieldsList.isEmpty()) { return null; } else { return new Sort(fieldsList.toArray(new SortField[0])); } }
From source file:org.apache.james.mailbox.lucene.search.LuceneMessageSearchIndex.java
License:Apache License
/** * Return a {@link Query} which is build based on the given {@link SearchQuery.FlagCriterion}. This is kind of a hack * as it will do a search for the flags in this method and * //from ww w.j a v a 2 s . c o m * @param crit * @return query * @throws UnsupportedSearchException */ private Query createFlagQuery(String flag, boolean isSet, Query inMailboxes, Collection<MessageUid> recentUids) throws MailboxException, UnsupportedSearchException { BooleanQuery query = new BooleanQuery(); if (isSet) { query.add(new TermQuery(new Term(FLAGS_FIELD, flag)), BooleanClause.Occur.MUST); } else { // lucene does not support simple NOT queries so we do some nasty hack here BooleanQuery bQuery = new BooleanQuery(); bQuery.add(new PrefixQuery(new Term(FLAGS_FIELD, "")), BooleanClause.Occur.MUST); bQuery.add(new TermQuery(new Term(FLAGS_FIELD, flag)), BooleanClause.Occur.MUST_NOT); query.add(bQuery, BooleanClause.Occur.MUST); } query.add(inMailboxes, BooleanClause.Occur.MUST); IndexSearcher searcher = null; try { Set<MessageUid> uids = new HashSet<MessageUid>(); searcher = new IndexSearcher(IndexReader.open(writer, true)); // query for all the documents sorted by uid TopDocs docs = searcher.search(query, null, maxQueryResults, new Sort(UID_SORT)); ScoreDoc[] sDocs = docs.scoreDocs; for (ScoreDoc sDoc : sDocs) { MessageUid uid = MessageUid.of(Long.valueOf(searcher.doc(sDoc.doc).get(UID_FIELD))); uids.add(uid); } // add or remove recent uids if (flag.equalsIgnoreCase("\\RECENT")) { if (isSet) { uids.addAll(recentUids); } else { uids.removeAll(recentUids); } } List<MessageRange> ranges = MessageRange.toRanges(new ArrayList<MessageUid>(uids)); UidRange[] nRanges = new UidRange[ranges.size()]; for (int i = 0; i < ranges.size(); i++) { MessageRange range = ranges.get(i); nRanges[i] = new UidRange(range.getUidFrom(), range.getUidTo()); } return createUidQuery((UidCriterion) SearchQuery.uid(nRanges)); } catch (IOException e) { throw new MailboxException("Unable to search mailbox " + inMailboxes, e); } finally { if (searcher != null) { try { searcher.close(); } catch (IOException e) { // ignore on close } } } }
From source file:org.apache.oodt.cas.filemgr.catalog.LuceneCatalog.java
License:Apache License
private Product getProductByName(String productName, boolean getRefs) throws CatalogException { IndexSearcher searcher = null;// w ww . ja v a 2s .c o m try { try { reader = DirectoryReader.open(indexDir); } catch (IOException e) { e.printStackTrace(); } searcher = new IndexSearcher(reader); Term productIdTerm = new Term("product_name", productName); org.apache.lucene.search.Query query = new TermQuery(productIdTerm); Sort sort = new Sort(new SortField("CAS.ProductReceivedTime", SortField.Type.STRING, true)); //TODO FIX NUMBER OF RECORDS TopDocs check = searcher.search(query, 1, sort); if (check.totalHits > 0) { TopDocs topDocs = searcher.search(query, check.totalHits, sort); ScoreDoc[] hits = topDocs.scoreDocs; // should be > 0 hits if (hits.length > 0) { // just get the first hit back Document productDoc = searcher.doc(hits[0].doc); CompleteProduct prod = toCompleteProduct(productDoc, getRefs, false); return prod.getProduct(); } else { LOG.log(Level.FINEST, "Request for product by name: [" + productName + "] returned no results"); return null; } } else { return null; } } catch (IOException e) { LOG.log(Level.WARNING, "IOException when opening index directory: [" + indexFilePath + "] for search: Message: " + e.getMessage()); throw new CatalogException(e.getMessage(), e); } finally { if (searcher != null) { try { //TODO CLOSE SEARCHER } catch (Exception ignore) { } } } }
From source file:org.apache.oodt.cas.filemgr.catalog.LuceneCatalog.java
License:Apache License
private List<Product> getProducts(boolean getRefs) throws CatalogException { IndexSearcher searcher = null;// ww w . j a va 2 s. c o m List<Product> products = null; try { try { reader = DirectoryReader.open(indexDir); } catch (IOException e) { e.printStackTrace(); } searcher = new IndexSearcher(reader); Term productIdTerm = new Term("myfield", "myvalue"); org.apache.lucene.search.Query query = new TermQuery(productIdTerm); Sort sort = new Sort(new SortField("CAS.ProductReceivedTime", SortField.Type.STRING, true)); //TODO FIX NUMBER OF RECORDS TopDocs check = searcher.search(query, 1, sort); TopDocs topDocs = searcher.search(query, check.totalHits, sort); ScoreDoc[] hits = topDocs.scoreDocs; // should be > 0 hits if (hits.length > 0) { products = new Vector<Product>(hits.length); for (ScoreDoc hit : hits) { Document productDoc = searcher.doc(hit.doc); CompleteProduct prod = toCompleteProduct(productDoc, getRefs, false); products.add(prod.getProduct()); } } else { LOG.log(Level.FINEST, "Request for products returned no results"); return null; } } catch (IOException e) { LOG.log(Level.WARNING, "IOException when opening index directory: [" + indexFilePath + "] for search: Message: " + e.getMessage()); throw new CatalogException(e.getMessage(), e); } finally { if (searcher != null) { try { //TODO close searcher } catch (Exception ignore) { } } } return products; }
From source file:org.apache.oodt.cas.filemgr.catalog.LuceneCatalog.java
License:Apache License
private List<Product> getProductsByProductType(ProductType type, boolean getRefs) throws CatalogException { IndexSearcher searcher = null;/* w ww. j a v a 2 s. c o m*/ List<Product> products = null; try { try { reader = DirectoryReader.open(indexDir); } catch (IOException e) { e.printStackTrace(); } searcher = new IndexSearcher(reader); Term productIdTerm = new Term("product_type_id", type.getProductTypeId()); org.apache.lucene.search.Query query = new TermQuery(productIdTerm); Sort sort = new Sort(new SortField("CAS.ProductReceivedTime", SortField.Type.STRING, true)); //TODO FIX NUMBER OF RECORDS TopDocs check = searcher.search(query, 1, sort); TopDocs topDocs = searcher.search(query, check.totalHits, sort); ScoreDoc[] hits = topDocs.scoreDocs; // should be > 0 hits if (hits.length > 0) { products = new Vector<Product>(hits.length); for (ScoreDoc hit : hits) { Document productDoc = searcher.doc(hit.doc); CompleteProduct prod = toCompleteProduct(productDoc, getRefs, false); products.add(prod.getProduct()); } } else { LOG.log(Level.FINEST, "Request for products by type: [" + type.getProductTypeId() + "] returned no results"); return null; } } catch (IOException e) { LOG.log(Level.WARNING, "IOException when opening index directory: [" + indexFilePath + "] for search: Message: " + e.getMessage()); throw new CatalogException(e.getMessage(), e); } finally { if (searcher != null) { try { //TODO CLOSE } catch (Exception ignore) { } } } return products; }
From source file:org.apache.oodt.cas.filemgr.catalog.LuceneCatalog.java
License:Apache License
public List<Product> getTopNProducts(int n) throws CatalogException { List<Product> products = null; IndexSearcher searcher = null;/*from w w w.jav a 2 s.c o m*/ try { try { reader = DirectoryReader.open(indexDir); } catch (IOException e) { e.printStackTrace(); } searcher = new IndexSearcher(reader); // construct a Boolean query here BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); TermQuery tq = new TermQuery(new Term("myfield", "myvalue")); booleanQuery.add(tq, BooleanClause.Occur.MUST); Sort sort = new Sort(new SortField("CAS.ProductReceivedTime", SortField.Type.STRING, true)); LOG.log(Level.FINE, "Querying LuceneCatalog: q: [" + booleanQuery + "]"); //TODO FIX NUMBER OF RECORDS TopDocs check = searcher.search(booleanQuery.build(), 1, sort); if (check.totalHits > 0) { TopDocs topDocs = searcher.search(booleanQuery.build(), check.totalHits, sort); ScoreDoc[] hits = topDocs.scoreDocs; if (hits.length > 0) { products = new Vector<Product>(n); int i = 0; while (products.size() < Math.min(n, hits.length)) { Document productDoc = searcher.doc(hits[i].doc); CompleteProduct prod = toCompleteProduct(productDoc, false, false); products.add(prod.getProduct()); i++; } } else { LOG.log(Level.WARNING, "Top N query produced no products!"); } } else { return null; } } catch (IOException e) { LOG.log(Level.WARNING, "IOException when opening index directory: [" + indexFilePath + "] for search: Message: " + e.getMessage()); throw new CatalogException(e.getMessage(), e); } finally { if (searcher != null) { try { //TODO CLOSE } catch (Exception ignore) { } } } return products; }
From source file:org.apache.oodt.cas.filemgr.catalog.LuceneCatalog.java
License:Apache License
private List<Product> paginateQuery(Query query, ProductType type, int pageNum, ProductPage page) throws CatalogException { List<Product> products = null; IndexSearcher searcher = null;//from www . ja v a 2 s . co m boolean doSkip = true; if (pageNum == -1) { doSkip = false; } try { reader = DirectoryReader.open(indexDir); } catch (IOException e) { e.printStackTrace(); } try { searcher = new IndexSearcher(reader); // construct a Boolean query here BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); // add the product type as the first clause TermQuery prodTypeTermQuery = new TermQuery(new Term("product_type_id", type.getProductTypeId())); booleanQuery.add(prodTypeTermQuery, BooleanClause.Occur.MUST); //convert filemgr query into a lucene query for (QueryCriteria queryCriteria : query.getCriteria()) { booleanQuery.add(this.getQuery(queryCriteria), BooleanClause.Occur.MUST); } Sort sort = new Sort(new SortField("CAS.ProductReceivedTime", SortField.Type.STRING, true)); LOG.log(Level.FINE, "Querying LuceneCatalog: q: [" + booleanQuery + "]"); //TODO FIX NUMBER OF RECORDS TopDocs check = searcher.search(booleanQuery.build(), 1, sort); TopDocs topDocs = searcher.search(booleanQuery.build(), check.totalHits, sort); // Calculate page size and set it while we have the results if (page != null) { page.setTotalPages(PaginationUtils.getTotalPage(topDocs.totalHits, pageSize)); } ScoreDoc[] hits = topDocs.scoreDocs; if (hits.length > 0) { int startNum = (pageNum - 1) * pageSize; if (doSkip) { if (startNum > hits.length) { startNum = 0; } products = new Vector<Product>(pageSize); for (int i = startNum; i < Math.min(hits.length, (startNum + pageSize)); i++) { Document productDoc = searcher.doc(hits[i].doc); CompleteProduct prod = toCompleteProduct(productDoc, false, false); products.add(prod.getProduct()); } } else { products = new Vector<Product>(hits.length); for (int i = 0; i < hits.length; i++) { Document productDoc = searcher.doc(hits[i].doc); CompleteProduct prod = toCompleteProduct(productDoc, false, false); products.add(prod.getProduct()); } } } else { LOG.log(Level.WARNING, "Query: [" + query + "] for Product Type: [" + type.getProductTypeId() + "] returned no results"); } } catch (Exception e) { LOG.log(Level.SEVERE, e.getMessage()); LOG.log(Level.WARNING, "IOException when opening index directory: [" + indexFilePath + "] for search: Message: " + e.getMessage()); throw new CatalogException(e.getMessage()); } finally { if (searcher != null) { try { //TODO CLOSE } catch (Exception ignore) { } } } return products; }
From source file:org.apache.oodt.cas.filemgr.tools.RangeQueryTester.java
License:Apache License
public List doRangeQuery(String productTypeId) { List products = null;// ww w .j a v a2 s . c om IndexSearcher searcher = null; try { reader = DirectoryReader.open(FSDirectory.open(Paths.get(this.indexPath))); } catch (IOException e) { e.printStackTrace(); } try { searcher = new IndexSearcher(reader); // construct a Boolean query here BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); // add the product type as the first clause TermQuery prodTypeTermQuery = new TermQuery(new Term("product_type_id", productTypeId)); booleanQuery.add(prodTypeTermQuery, BooleanClause.Occur.MUST); Term startFieldStartTerm = null, startFieldEndTerm = null; if (this.startFieldStartValue != null) { startFieldStartTerm = new Term(this.startFieldName, this.startFieldStartValue); } if (this.startFieldEndValue != null) { startFieldEndTerm = new Term(this.startFieldName, this.startFieldEndValue); } TermRangeQuery query1 = new TermRangeQuery(startFieldEndTerm.field(), startFieldStartTerm.bytes(), startFieldEndTerm.bytes(), true, true); booleanQuery.add(query1, BooleanClause.Occur.MUST); if (this.endFieldName != null && (this.endFieldStartValue != null || this.endFieldEndValue != null)) { Term endFieldEndTerm = null, endFieldStartTerm = null; if (this.endFieldStartValue != null) { endFieldStartTerm = new Term(this.endFieldName, this.endFieldStartValue); } if (this.endFieldEndValue != null) { endFieldEndTerm = new Term(this.endFieldName, this.endFieldEndValue); } TermRangeQuery query2 = new TermRangeQuery(endFieldEndTerm.field(), endFieldStartTerm.bytes(), endFieldEndTerm.bytes(), true, true); booleanQuery.add(query2, BooleanClause.Occur.MUST); } Sort sort = new Sort(new SortField("CAS.ProductReceivedTime", SortField.Type.STRING, true)); //TODO Fix number TopFieldDocs topDocs = searcher.search(booleanQuery.build(), 1, sort); ScoreDoc[] hits = topDocs.scoreDocs; if (topDocs.totalHits > 0) { products = new Vector(topDocs.totalHits); for (int i = 0; i < topDocs.totalHits; i++) { Document productDoc = searcher.doc(hits[i].doc); products.add(productDoc.get("reference_data_store")); } } else { LOG.log(Level.WARNING, "Query: [" + query1 + "] for Product Type: [" + productTypeId + "] returned no results"); } } catch (IOException e) { LOG.log(Level.WARNING, "IOException when opening index directory: [" + this.indexPath + "] for search: Message: " + e.getMessage()); throw new RuntimeException(e.getMessage()); } finally { if (searcher != null) { try { //TODO CLOSE SEARCH // searcher.close(); } catch (Exception ignore) { } } } return products; }
From source file:org.apache.oodt.cas.workflow.instrepo.LuceneWorkflowInstanceRepository.java
License:Apache License
public int getNumWorkflowInstances() throws InstanceRepositoryException { IndexSearcher searcher = null;/* w w w . j a va 2s. c o m*/ int numInsts = -1; try { reader = DirectoryReader.open(indexDir); } catch (IOException e) { e.printStackTrace(); } try { searcher = new IndexSearcher(reader); Term instIdTerm = new Term("myfield", "myvalue"); org.apache.lucene.search.Query query = new TermQuery(instIdTerm); Sort sort = new Sort(new SortField("workflow_inst_startdatetime", SortField.Type.STRING, true)); TopDocs topDocs = searcher.search(query, 1, sort); numInsts = topDocs.totalHits; } catch (IOException e) { LOG.log(Level.WARNING, "IOException when opening index directory: [" + idxFilePath + "] for search: Message: " + e.getMessage()); throw new InstanceRepositoryException(e.getMessage()); } finally { if (searcher != null) { try { //TODO Shutdown searcher } catch (Exception ignore) { } } } return numInsts; }