List of usage examples for org.apache.lucene.search Sort setSort
public void setSort(SortField... fields)
From source file:SearchHelpDocs.java
License:Open Source License
/** * Default search, sort by score and date *//* w w w. j a va 2 s .c o m*/ private static Sort createSort() throws Exception { Sort sort = new Sort(); SortField fields[] = { SortField.FIELD_SCORE, new SortField("yyyymmdd", SortField.STRING, true) }; sort.setSort(fields); return sort; }
From source file:com.ideabase.repository.core.search.impl.RepositoryItemSearchImpl.java
License:Open Source License
private void applySortingFilter(final Query pQuery, final RepositoryItemSearchImpl.RepositoryHitCollectorImpl pHitCollector, final LuceneSearcher pSearcher) throws IOException { Sort sort = new Sort(); final List<SortField> sortFields = new ArrayList<SortField>(); for (final Map.Entry<String, Boolean> entry : pQuery.getSortableFields().entrySet()) { // verify special field like relevant and indexoredered final String key = entry.getKey(); final Boolean decending = entry.getValue(); if (SORT_RELEVANT.equalsIgnoreCase(key)) { LOG.debug("Applying relevance sorting."); sort = Sort.RELEVANCE;/*w ww.j a v a 2 s. co m*/ break; } else if (SORT_INDEXORDERED.equalsIgnoreCase(key)) { LOG.debug("Applying Index ordered sorting."); sort = Sort.INDEXORDER; break; } else { if (LOG.isDebugEnabled()) { LOG.debug("Applying sort field - " + key); } if (key.startsWith(CommonConstants.FIELD_PREFIX_PRICE) || key.endsWith(CommonConstants.FIELD_SUFFIX_ID) || key.endsWith(CommonConstants.FIELD_SUFFIX_DATE)) { sortFields.add(new SortField(key, mNumberSortComparator, decending.booleanValue())); } else { sortFields.add(new SortField(key, decending.booleanValue())); } } } if (!sortFields.isEmpty()) { sort.setSort(sortFields.toArray(new SortField[] {})); } if (LOG.isDebugEnabled()) { LOG.debug("Sortable fields - " + sort); } // perform lucene search and collect the search hits. populateHitCollector(pHitCollector, pSearcher.search(pQuery.buildQuery(), sort)); }
From source file:com.mysema.query.lucene.LuceneSerializer.java
License:Apache License
public Sort toSort(List<? extends OrderSpecifier<?>> orderBys) { List<SortField> sorts = new ArrayList<SortField>(orderBys.size()); for (OrderSpecifier<?> order : orderBys) { if (!(order.getTarget() instanceof Path<?>)) { throw new IllegalArgumentException("argument was not of type Path."); }/*www .jav a 2 s.c om*/ Class<?> type = order.getTarget().getType(); boolean reverse = !order.isAscending(); Path<?> path = getPath(order.getTarget()); if (Number.class.isAssignableFrom(type)) { sorts.add(new SortField(toField(path), sortFields.get(type), reverse)); } else { sorts.add(new SortField(toField(path), sortLocale, reverse)); } } Sort sort = new Sort(); sort.setSort(sorts.toArray(new SortField[sorts.size()])); return sort; }
From source file:com.querydsl.lucene4.LuceneSerializer.java
License:Apache License
public Sort toSort(List<? extends OrderSpecifier<?>> orderBys) { List<SortField> sorts = new ArrayList<SortField>(orderBys.size()); for (OrderSpecifier<?> order : orderBys) { if (!(order.getTarget() instanceof Path<?>)) { throw new IllegalArgumentException("argument was not of type Path."); }/*from ww w. ja va2 s . c o m*/ Class<?> type = order.getTarget().getType(); boolean reverse = !order.isAscending(); Path<?> path = getPath(order.getTarget()); if (Number.class.isAssignableFrom(type)) { sorts.add(new SortField(toField(path), sortFields.get(type), reverse)); } else { sorts.add(new SortField(toField(path), SortField.Type.STRING, reverse)); } } Sort sort = new Sort(); sort.setSort(sorts.toArray(new SortField[sorts.size()])); return sort; }
From source file:com.querydsl.lucene5.LuceneSerializer.java
License:Apache License
public Sort toSort(List<? extends OrderSpecifier<?>> orderBys) { List<SortField> sorts = new ArrayList<SortField>(orderBys.size()); for (OrderSpecifier<?> order : orderBys) { if (!(order.getTarget() instanceof Path<?>)) { throw new IllegalArgumentException("argument was not of type Path."); }/*from w w w . j a v a 2 s .com*/ Class<?> type = order.getTarget().getType(); boolean reverse = !order.isAscending(); Path<?> path = getPath(order.getTarget()); if (Number.class.isAssignableFrom(type)) { sorts.add(new SortedNumericSortField(toField(path), sortFields.get(type), reverse)); } else { sorts.add(new SortField(toField(path), SortField.Type.STRING, reverse)); } } Sort sort = new Sort(); sort.setSort(sorts.toArray(new SortField[sorts.size()])); return sort; }
From source file:de.uni_koeln.spinfo.maalr.lucene.core.Dictionary.java
License:Apache License
public QueryResult query(MaalrQuery maalrQuery) throws InvalidQueryException, NoIndexAvailableException, BrokenIndexException, IOException, InvalidTokenOffsetsException { long start = System.nanoTime(); validateQuery(maalrQuery);// w w w .j ava2s . co m int pageSize = maalrQuery.getPageSize(); long s1 = System.nanoTime(); Query query = indexManager.buildQuery(maalrQuery); TopDocs docs = null; // TODO: Make this configurable! Sort sort = new Sort(); String[] items = null; if (maalrQuery.getValue("language") != null && maalrQuery.getValue("language").equals(description.getLanguageName(false))) { items = description.getSortList(false); } else { items = description.getSortList(true); } SortField[] fields = new SortField[items.length + 1]; fields[0] = SortField.FIELD_SCORE; for (int i = 0; i < items.length; i++) { String item = items[i]; fields[i + 1] = new SortField(item, sortTypes.get(item)); } sort.setSort(fields); QueryResult result = null; int pageNr = maalrQuery.getPageNr(); long e1 = System.nanoTime(); try { long s2 = System.nanoTime(); docs = indexProvider.getSearcher().search(query, pageSize * (pageNr + 1), sort); long e2 = System.nanoTime(); result = toQueryResult(docs, pageSize * pageNr, maalrQuery.getPageSize()); if (logger.isDebugEnabled()) { logger.debug("Time to build query: " + (e1 - s1) / 1000000 + ", Time to execute query: " + ((e2 - s2) / 1000000)); } } catch (IOException e) { throw new BrokenIndexException("Failed to access index", e); } long end = System.nanoTime(); double time = (end - start) / 1000000D; // Warn if query takes more than 100 ms. if (time > 100) { logger.warn("Slow query: " + formatter.format(time) + " ms for " + maalrQuery); } else if (logger.isDebugEnabled()) { logger.debug("Processed query in " + formatter.format(time) + " ms :" + maalrQuery); } return result; }
From source file:org.ala.dao.TaxonConceptSHDaoImpl.java
License:Open Source License
/** * Perform Lucene search with params for sorting and paging * //from w w w.j ava2 s . co m * @param searchQuery * @param startIndex * @param pageSize * @param sortDirection * @param sortField * @return * @throws IOException * @throws Exception */ private SearchResultsDTO sortPageSearch(Query searchQuery, Integer startIndex, Integer pageSize, String sortField, String sortDirection) throws IOException, Exception { boolean direction = false; if (sortDirection != null && !sortDirection.isEmpty() && sortDirection.equalsIgnoreCase("desc")) { direction = true; } Sort sort = new Sort(); if (sortField != null && !sortField.isEmpty() && !sortField.equalsIgnoreCase("score")) { SortField sf = new SortField(sortField, SortField.Type.STRING, direction); sort.setSort(sf); } else { sort = Sort.RELEVANCE; } TopDocs topDocs = getTcIdxSearcher().search(searchQuery, null, startIndex + pageSize, sort); // TODO ues sortField here logger.debug("Total hits: " + topDocs.totalHits); List<SearchTaxonConceptDTO> tcs = new ArrayList<SearchTaxonConceptDTO>(); for (int i = 0; i < topDocs.scoreDocs.length; i++) { if (i >= startIndex) { ScoreDoc scoreDoc = topDocs.scoreDocs[i]; Document doc = getTcIdxSearcher().doc(scoreDoc.doc); tcs.add(createTaxonConceptFromIndex(doc, scoreDoc.score)); } } SearchResultsDTO searchResults = new SearchResultsDTO(tcs); searchResults.setTotalRecords(topDocs.totalHits); searchResults.setStartIndex(startIndex); searchResults.setStatus("OK"); searchResults.setSort(sortField); searchResults.setDir(sortDirection); searchResults.setQuery(searchQuery.toString()); return searchResults; }
From source file:org.apache.james.mailbox.lucene.search.LuceneMessageSearchIndex.java
License:Apache License
private Sort createSort(List<SearchQuery.Sort> sorts) { Sort sort = new Sort(); List<SortField> fields = new ArrayList<SortField>(); for (SearchQuery.Sort s : sorts) { boolean reverse = s.isReverse(); SortField sf = null;/* w w w . jav a 2 s .c o m*/ switch (s.getSortClause()) { case Arrival: if (reverse) { sf = ARRIVAL_MAILBOX_SORT_REVERSE; } else { sf = ARRIVAL_MAILBOX_SORT; } break; case SentDate: if (reverse) { sf = SENT_DATE_SORT_REVERSE; } else { sf = SENT_DATE_SORT; } break; case MailboxCc: if (reverse) { sf = FIRST_CC_MAILBOX_SORT_REVERSE; } else { sf = FIRST_CC_MAILBOX_SORT; } break; case MailboxFrom: if (reverse) { sf = FIRST_FROM_MAILBOX_SORT_REVERSE; } else { sf = FIRST_FROM_MAILBOX_SORT; } break; case Size: if (reverse) { sf = SIZE_SORT_REVERSE; } else { sf = SIZE_SORT; } break; case BaseSubject: if (reverse) { sf = BASE_SUBJECT_SORT_REVERSE; } else { sf = BASE_SUBJECT_SORT; } break; case MailboxTo: if (reverse) { sf = FIRST_TO_MAILBOX_SORT_REVERSE; } else { sf = FIRST_TO_MAILBOX_SORT; } break; case Uid: if (reverse) { sf = UID_SORT_REVERSE; } else { sf = UID_SORT; } break; case DisplayFrom: if (reverse) { sf = FIRST_FROM_MAILBOX_DISPLAY_SORT_REVERSE; } else { sf = FIRST_FROM_MAILBOX_DISPLAY_SORT; } break; case DisplayTo: if (reverse) { sf = FIRST_TO_MAILBOX_DISPLAY_SORT_REVERSE; } else { sf = FIRST_TO_MAILBOX_DISPLAY_SORT; } break; default: break; } if (sf != null) { fields.add(sf); // Add the uid sort as tie-breaker if (sf == SENT_DATE_SORT) { fields.add(UID_SORT); } else if (sf == SENT_DATE_SORT_REVERSE) { fields.add(UID_SORT_REVERSE); } } } // add the uid sorting as last so if no other sorting was able todo the job it will get sorted by the uid fields.add(UID_SORT); sort.setSort(fields.toArray(new SortField[0])); return sort; }
From source file:org.apache.solr.uninverting.TestFieldCacheSort.java
License:Apache License
/** test sorts when there's nothing in the index */ public void testEmptyIndex() throws Exception { IndexSearcher empty = newSearcher(new MultiReader()); Query query = new TermQuery(new Term("contents", "foo")); Sort sort = new Sort(); TopDocs td = empty.search(query, 10, sort, true, true); assertEquals(0, td.totalHits);// www . j a v a 2s. co m sort.setSort(SortField.FIELD_DOC); td = empty.search(query, 10, sort, true, true); assertEquals(0, td.totalHits); sort.setSort(new SortField("int", SortField.Type.INT), SortField.FIELD_DOC); td = empty.search(query, 10, sort, true, true); assertEquals(0, td.totalHits); sort.setSort(new SortField("string", SortField.Type.STRING, true), SortField.FIELD_DOC); td = empty.search(query, 10, sort, true, true); assertEquals(0, td.totalHits); sort.setSort(new SortField("string_val", SortField.Type.STRING_VAL, true), SortField.FIELD_DOC); td = empty.search(query, 10, sort, true, true); assertEquals(0, td.totalHits); sort.setSort(new SortField("float", SortField.Type.FLOAT), new SortField("string", SortField.Type.STRING)); td = empty.search(query, 10, sort, true, true); assertEquals(0, td.totalHits); }
From source file:org.capelin.mvc.utils.LuceneBuilder.java
License:GNU General Public License
public Sort buildSort(SearchFormObject form) { if (null == form.getSortTerm() || StaticStrings.EMPLTY.equals(form.getSortTerm())) return null; SortField sf;//from ww w.j a va2 s . c o m if (WebViewUtils.YEAR.equals(form.getSortTerm())) { sf = new SortField(form.getSortTerm(), SortField.STRING, true); } else if (WebViewUtils.ID.equals(form.getSortTerm())) { sf = new SortField(form.getSortTerm(), SortField.INT, true); } else { sf = new SortField(form.getSortTerm(), SortField.STRING); } Sort sort = new Sort(); sort.setSort(sf); return sort; }