List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:com.vmware.dcp.services.common.LuceneQueryConverter.java
License:Open Source License
static Sort convertToLuceneSort(QueryTask.QuerySpecification querySpecification) { validateSortTerm(querySpecification.sortTerm); if (querySpecification.sortOrder == null) { querySpecification.sortOrder = QueryTask.QuerySpecification.SortOrder.ASC; }/*from ww w .j av a2 s .com*/ boolean order = querySpecification.sortOrder != QueryTask.QuerySpecification.SortOrder.ASC; return new Sort(new SortField(querySpecification.sortTerm.propertyName, convertToLuceneType(querySpecification.sortTerm.propertyType), order)); }
From source file:com.vmware.xenon.services.common.LuceneBlobIndexService.java
License:Open Source License
@Override public void handleStart(final Operation post) { // The service index searcher and maintenance semantics rely on a single thread // making all updates. If the executor ever is changed to use multiple threads, this // service should mirror the logic in LuceneDocumentIndexService this.singleThreadedExecutor = getHost().allocateExecutor(this, 1); super.setMaintenanceIntervalMicros(getHost().getMaintenanceIntervalMicros() * 5); File directory = new File(new File(getHost().getStorageSandbox()), this.indexDirectory); this.timeSort = new Sort(new SortField(URI_PARAM_NAME_UPDATE_TIME, SortField.Type.LONG, true)); try {/*from w w w. j a va 2 s . c o m*/ this.writer = createWriter(directory); } catch (Throwable e) { logSevere("Failure creating index writer on directory %s: %s", directory, Utils.toString(e)); post.fail(e); return; } post.complete(); }
From source file:com.vmware.xenon.services.common.LuceneDocumentIndexBackupService.java
License:Open Source License
private void performTimeSnapshotRecovery(Long timeSnapshotBoundaryMicros, IndexWriter newWriter) throws IOException { // For documents with metadata indexing enabled, the version which was current at // the restore time may have subsequently been marked as not current. Update the // current field for any such documents. IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(newWriter, true, true)); Query updateTimeQuery = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS, timeSnapshotBoundaryMicros + 1, Long.MAX_VALUE); Sort selfLinkSort = new Sort(new SortField( LuceneIndexDocumentHelper.createSortFieldPropertyName(ServiceDocument.FIELD_NAME_SELF_LINK), SortField.Type.STRING)); final int pageSize = 10000; Set<String> prevPageLinks = new HashSet<>(); ScoreDoc after = null;//from ww w. j a v a 2s. c om while (true) { TopDocs results = searcher.searchAfter(after, updateTimeQuery, pageSize, selfLinkSort, false, false); if (results == null || results.scoreDocs == null || results.scoreDocs.length == 0) { break; } Set<String> pageLinks = new HashSet<>(); DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor(); for (ScoreDoc sd : results.scoreDocs) { visitor.reset(ServiceDocument.FIELD_NAME_SELF_LINK); searcher.doc(sd.doc, visitor); if (prevPageLinks.contains(visitor.documentSelfLink)) { pageLinks.add(visitor.documentSelfLink); continue; } if (!pageLinks.add(visitor.documentSelfLink)) { continue; } updateCurrentAttributeForSelfLink(searcher, timeSnapshotBoundaryMicros, visitor.documentSelfLink, newWriter); } if (results.scoreDocs.length < pageSize) { break; } after = results.scoreDocs[results.scoreDocs.length - 1]; prevPageLinks = pageLinks; } // Now that metadata indexing attributes have been updated appropriately, delete any // documents which were created after the restore point. Query luceneQuery = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS, timeSnapshotBoundaryMicros + 1, Long.MAX_VALUE); newWriter.deleteDocuments(luceneQuery); }
From source file:com.vmware.xenon.services.common.LuceneDocumentIndexBackupService.java
License:Open Source License
private void updateCurrentAttributeForSelfLink(IndexSearcher searcher, long timeSnapshotBoundaryMicros, String selfLink, IndexWriter newWriter) throws IOException { Query selfLinkClause = new TermQuery(new Term(ServiceDocument.FIELD_NAME_SELF_LINK, selfLink)); Query updateTimeClause = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS, 0, timeSnapshotBoundaryMicros); Query booleanQuery = new BooleanQuery.Builder().add(selfLinkClause, Occur.MUST) .add(updateTimeClause, Occur.MUST).build(); Sort versionSort = new Sort( new SortedNumericSortField(ServiceDocument.FIELD_NAME_VERSION, SortField.Type.LONG, true)); TopDocs results = searcher.search(booleanQuery, 1, versionSort, false, false); if (results == null || results.scoreDocs == null || results.scoreDocs.length == 0) { return;/*from www . j av a2s . co m*/ } DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor(); visitor.reset(LuceneIndexDocumentHelper.FIELD_NAME_INDEXING_ID); searcher.doc(results.scoreDocs[0].doc, visitor); if (visitor.documentIndexingId == null) { return; } Term indexingIdTerm = new Term(LuceneIndexDocumentHelper.FIELD_NAME_INDEXING_ID, visitor.documentIndexingId); newWriter.updateNumericDocValue(indexingIdTerm, LuceneIndexDocumentHelper.FIELD_NAME_INDEXING_METADATA_VALUE_TOMBSTONE_TIME, LuceneIndexDocumentHelper.ACTIVE_DOCUMENT_TOMBSTONE_TIME); }
From source file:com.vmware.xenon.services.common.LuceneDocumentIndexService.java
License:Open Source License
private void initializeInstance() { this.searchSync = new Object(); this.searcher = null; this.searchersForPaginatedQueries.clear(); this.searchersPendingClose.clear(); this.versionSort = new Sort( new SortedNumericSortField(ServiceDocument.FIELD_NAME_VERSION, SortField.Type.LONG, true)); this.fieldsToLoadNoExpand = new HashSet<>(); this.fieldsToLoadNoExpand.add(ServiceDocument.FIELD_NAME_SELF_LINK); this.fieldsToLoadNoExpand.add(ServiceDocument.FIELD_NAME_VERSION); this.fieldsToLoadNoExpand.add(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS); this.fieldsToLoadNoExpand.add(ServiceDocument.FIELD_NAME_UPDATE_ACTION); this.fieldsToLoadNoExpand.add(ServiceDocument.FIELD_NAME_EXPIRATION_TIME_MICROS); this.fieldsToLoadWithExpand = new HashSet<>(this.fieldsToLoadNoExpand); this.fieldsToLoadWithExpand.add(LUCENE_FIELD_NAME_JSON_SERIALIZED_STATE); this.fieldsToLoadWithExpand.add(LUCENE_FIELD_NAME_BINARY_SERIALIZED_STATE); }
From source file:com.vmware.xenon.services.common.LuceneQueryConverter.java
License:Open Source License
static Sort convertToLuceneSort(QueryTask.QuerySpecification querySpecification, boolean isGroupSort) { QueryTask.QueryTerm sortTerm = isGroupSort ? querySpecification.groupSortTerm : querySpecification.sortTerm; QueryTask.QuerySpecification.SortOrder sortOrder = isGroupSort ? querySpecification.groupSortOrder : querySpecification.sortOrder; validateSortTerm(sortTerm);//from ww w . ja v a2 s.c o m if (querySpecification.options.contains(QueryOption.TOP_RESULTS)) { if (querySpecification.resultLimit <= 0 || querySpecification.resultLimit == Integer.MAX_VALUE) { throw new IllegalArgumentException("resultLimit should be a positive integer less than MAX_VALUE"); } } if (sortOrder == null) { if (isGroupSort) { querySpecification.groupSortOrder = QueryTask.QuerySpecification.SortOrder.ASC; } else { querySpecification.sortOrder = QueryTask.QuerySpecification.SortOrder.ASC; } } boolean order = sortOrder != QueryTask.QuerySpecification.SortOrder.ASC; SortField sortField = null; SortField.Type type = convertToLuceneType(sortTerm.propertyType); switch (type) { case LONG: case DOUBLE: sortField = new SortedNumericSortField(sortTerm.propertyName, type, order); break; default: sortField = new SortField(sortTerm.propertyName, type, order); break; } return new Sort(sortField); }
From source file:com.xiaomi.linden.core.search.query.sort.SortConstructor.java
License:Apache License
public static Sort constructSort(LindenSearchRequest request, IndexSearcher indexSearcher, LindenConfig config) throws IOException { if (!request.isSetSort()) return null; LindenSort lindenSort = request.getSort(); SortField[] sortFields = new SortField[lindenSort.getFieldsSize()]; for (int i = 0; i < lindenSort.getFieldsSize(); ++i) { LindenSortField field = lindenSort.getFields().get(i); SortField.Type type = SortField.Type.STRING; boolean isReverse = field.isReverse(); switch (field.getType()) { case STRING: type = SortField.Type.STRING; break; case DOUBLE: type = SortField.Type.DOUBLE; break; case FLOAT: type = SortField.Type.FLOAT; break; case INTEGER: type = SortField.Type.INT; break; case LONG: type = SortField.Type.LONG; break; case SCORE: type = SortField.Type.SCORE; isReverse = !isReverse;/* w w w . ja v a 2 s . c om*/ break; case DISTANCE: if (request.isSetSpatialParam()) { Point point = SpatialContext.GEO.makePoint( request.getSpatialParam().getCoordinate().getLongitude(), request.getSpatialParam().getCoordinate().getLatitude()); ValueSource valueSource = config.getSpatialStrategy().makeDistanceValueSource(point, DistanceUtils.DEG_TO_KM); sortFields[i] = valueSource.getSortField(false).rewrite(indexSearcher); } continue; } sortFields[i] = new SortField(field.getName(), type, isReverse); } return new Sort(sortFields); }
From source file:com.xiaomi.linden.lucene.merge.SortingMergePolicyFactory.java
License:Apache License
@Override public MergePolicy getInstance(Map<String, String> params) throws IOException { String field = params.get(SORT_FIELD); SortField.Type sortFieldType = SortField.Type.DOC; if (params.containsKey(SORT_FIELD_TYPE)) { sortFieldType = SortField.Type.valueOf(params.get(SORT_FIELD_TYPE).toUpperCase()); }//from w ww . j a v a2 s. com if (sortFieldType == SortField.Type.DOC) { throw new IOException( "Relying on internal lucene DocIDs is not guaranteed to work, this is only an implementation detail."); } boolean desc = true; if (params.containsKey(SORT_DESC)) { try { desc = Boolean.valueOf(params.get(SORT_DESC)); } catch (Exception e) { desc = true; } } SortField sortField = new SortField(field, sortFieldType, desc); Sort sort = new Sort(sortField); return new SortingMergePolicyDecorator(new TieredMergePolicy(), sort); }
From source file:com.xpn.xwiki.plugin.lucene.LucenePlugin.java
License:Open Source License
/** * Creates and submits a query to the Lucene engine. * /*from w ww.j a v a2s . c o m*/ * @param query The base query, using the query engine supported by Lucene. * @param sortField The name of a field to sort results by. If the name starts with '-', then * the field (excluding the -) is used for reverse sorting. If <tt>null</tt> or * empty, sort by hit score. * @param virtualWikiNames Comma separated list of virtual wiki names to search in, may be * <tt>null</tt> to search all virtual wikis. * @param languages Comma separated list of language codes to search in, may be <tt>null</tt> * or empty to search all languages. * @param indexes List of Lucene indexes (searchers) to search. * @param context The context of the request. * @return The list of search results. * @throws IOException If the Lucene searchers encounter a problem reading the indexes. * @throws ParseException If the query is not valid. */ private SearchResults search(String query, String sortField, String virtualWikiNames, String languages, Searcher[] indexes, XWikiContext context) throws IOException, ParseException { SortField sort = getSortField(sortField); // Perform the actual search return search(query, (sort != null) ? new Sort(sort) : null, virtualWikiNames, languages, indexes, context); }
From source file:com.xpn.xwiki.plugin.lucene.LucenePlugin.java
License:Open Source License
/** * Creates and submits a query to the Lucene engine. * //from w w w.ja v a 2 s . co m * @param query The base query, using the query engine supported by Lucene. * @param sortFields A list of fields to sort results by. For each field, if the name starts * with '-', then that field (excluding the -) is used for reverse sorting. If * <tt>null</tt> or empty, sort by hit score. * @param virtualWikiNames Comma separated list of virtual wiki names to search in, may be * <tt>null</tt> to search all virtual wikis. * @param languages Comma separated list of language codes to search in, may be <tt>null</tt> * or empty to search all languages. * @param indexes List of Lucene indexes (searchers) to search. * @param context The context of the request. * @return The list of search results. * @throws IOException If the Lucene searchers encounter a problem reading the indexes. * @throws ParseException If the query is not valid. */ private SearchResults search(String query, String[] sortFields, String virtualWikiNames, String languages, Searcher[] indexes, XWikiContext context) throws IOException, ParseException { // Turn the sorting field names into SortField objects. SortField[] sorts = null; if (sortFields != null && sortFields.length > 0) { sorts = new SortField[sortFields.length]; for (int i = 0; i < sortFields.length; ++i) { sorts[i] = getSortField(sortFields[i]); } // Remove any null values from the list. int prevLength = -1; while (prevLength != sorts.length) { prevLength = sorts.length; sorts = (SortField[]) ArrayUtils.removeElement(sorts, null); } } // Perform the actual search return search(query, (sorts != null) ? new Sort(sorts) : null, virtualWikiNames, languages, indexes, context); }