List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:io.crate.operation.collect.collectors.LuceneOrderedDocCollectorTest.java
License:Apache License
private Long[] nextPageQuery(IndexReader reader, FieldDoc lastCollected, boolean reverseFlag, @Nullable Boolean nullFirst) throws IOException { OrderBy orderBy = new OrderBy(ImmutableList.<Symbol>of(REFERENCE), new boolean[] { reverseFlag }, new Boolean[] { nullFirst }); SortField sortField = new SortedNumericSortField("value", SortField.Type.LONG, reverseFlag); Long missingValue = (Long) LuceneMissingValue.missingValue(orderBy, 0); sortField.setMissingValue(missingValue); Sort sort = new Sort(sortField); Query nextPageQuery = LuceneOrderedDocCollector.nextPageQuery(lastCollected, orderBy, new Object[] { missingValue }, name -> valueFieldType); TopFieldDocs result = search(reader, nextPageQuery, sort); Long results[] = new Long[result.scoreDocs.length]; for (int i = 0; i < result.scoreDocs.length; i++) { Long value = (Long) ((FieldDoc) result.scoreDocs[i]).fields[0]; results[i] = value.equals(missingValue) ? null : value; }/*from w w w . j av a 2s. co m*/ return results; }
From source file:io.crate.operation.collect.collectors.OrderedLuceneBatchIteratorBenchmark.java
License:Apache License
private LuceneOrderedDocCollector createOrderedCollector(IndexSearcher searcher, String sortByColumnName) { List<LuceneCollectorExpression<?>> expressions = Collections .singletonList(new OrderByCollectorExpression(reference, orderBy, o -> o)); return new LuceneOrderedDocCollector(dummyShardId, searcher, new MatchAllDocsQuery(), null, false, 10_000_000, fieldTypeLookup, collectorContext, orderBy, new Sort(new SortedNumericSortField(sortByColumnName, SortField.Type.INT, reverseFlags[0])), expressions, expressions);//w w w.j a va 2 s . co m }
From source file:io.crate.operation.collect.collectors.OrderedLuceneBatchIteratorFactoryTest.java
License:Apache License
private LuceneOrderedDocCollector createOrderedCollector(IndexSearcher searcher, int shardId) { CollectorContext collectorContext = new CollectorContext(mock(IndexFieldDataService.class), new CollectorFieldsVisitor(0)); List<LuceneCollectorExpression<?>> expressions = Collections .singletonList(new OrderByCollectorExpression(reference, orderBy, o -> o)); return new LuceneOrderedDocCollector(new ShardId("dummy", UUIDs.randomBase64UUID(), shardId), searcher, new MatchAllDocsQuery(), null, false, 5, // batchSize < 10 to have at least one searchMore call. fieldTypeLookup, collectorContext, orderBy, new Sort(new SortedNumericSortField(columnName, SortField.Type.LONG, reverseFlags[0])), expressions, expressions);/* w ww .j ava2 s. c o m*/ }
From source file:io.crate.operation.collect.LuceneDocCollectorTest.java
License:Apache License
private Long[] nextPageQuery(IndexReader reader, FieldDoc lastCollected, boolean reverseFlag, @Nullable Boolean nullFirst) throws IOException { OrderBy orderBy = new OrderBy(ImmutableList.<Symbol>of(new Reference(info)), new boolean[] { reverseFlag }, new Boolean[] { nullFirst }); SortField sortField = new SortField("value", SortField.Type.LONG, reverseFlag); Long missingValue = (Long) LuceneMissingValue.missingValue(orderBy, 0); sortField.setMissingValue(missingValue); Sort sort = new Sort(sortField); Query nextPageQuery = OrderedDocCollector.nextPageQuery(lastCollected, orderBy, new Object[] { missingValue }); TopFieldDocs result = search(reader, nextPageQuery, sort); Long results[] = new Long[result.scoreDocs.length]; for (int i = 0; i < result.scoreDocs.length; i++) { Long value = (Long) ((FieldDoc) result.scoreDocs[i]).fields[0]; results[i] = value.equals(missingValue) ? null : value; }/*from www. j a v a2 s . c o m*/ return results; }
From source file:io.vertigo.dynamo.plugins.collections.lucene.RamLuceneIndex.java
License:Apache License
private static Optional<Sort> createSort(final DtListState dtListState) { if (dtListState.getSortFieldName().isPresent()) { final String sortFieldName = dtListState.getSortFieldName().get(); final boolean sortDesc = dtListState.isSortDesc().get(); final SortField.Type luceneType = SortField.Type.STRING; //TODO : check if other type are necessary final SortField sortField = new SortField(sortFieldName, luceneType, sortDesc); sortField.setMissingValue(SortField.STRING_LAST); return Optional.of(new Sort(sortField)); }// w ww. ja va 2s. co m return Optional.empty(); }
From source file:it.doqui.index.ecmengine.business.personalization.multirepository.index.lucene.RepositoryAwareADMLuceneSearcherImpl.java
License:Open Source License
public ResultSet query(SearchParameters searchParameters) { // final Query filter; if (searchParameters.getStores().size() != 1) { throw new IllegalStateException("Only one store can be searched at present"); }/*w ww . jav a 2s .c o m*/ ArrayList<StoreRef> stores = searchParameters.getStores(); stores.set(0, tenantService.getName(searchParameters.getStores().get(0))); final StoreRef queryStore = stores.get(0); if (logger.isDebugEnabled()) { logger.debug("[RepositoryAwareADMLuceneSearcherImpl::query] Repository '" + getRepository() + "' -- Executing query on store: " + queryStore); } String parameterisedQueryString; if (!searchParameters.getQueryParameterDefinitions().isEmpty()) { Map<QName, QueryParameterDefinition> map = new HashMap<QName, QueryParameterDefinition>(); for (QueryParameterDefinition qpd : searchParameters.getQueryParameterDefinitions()) { map.put(qpd.getQName(), qpd); } parameterisedQueryString = parameterise(searchParameters.getQuery(), map, null, namespacePrefixResolver); } else { parameterisedQueryString = searchParameters.getQuery(); } if (searchParameters.getLanguage().equalsIgnoreCase(SearchService.LANGUAGE_LUCENE)) { try { final Operator defaultOperator = (searchParameters.getDefaultOperator() == SearchParameters.AND) ? LuceneQueryParser.AND_OPERATOR : LuceneQueryParser.OR_OPERATOR; // Query userQuery = LuceneQueryParser.parse(parameterisedQueryString, DEFAULT_FIELD, // new LuceneAnalyser(getDictionaryService(), // (searchParameters.getMlAnalaysisMode() == null) // ? getLuceneConfig().getDefaultMLSearchAnalysisMode() // : searchParameters.getMlAnalaysisMode()), // namespacePrefixResolver, // getDictionaryService(), // defaultOperator, // searchParameters, // getLuceneConfig()); // // Aggiunta filtraggio per permessi di lettura // BooleanQuery query = new BooleanQuery(); // query.add(userQuery, BooleanClause.Occur.MUST); // if (filter != null) { // query.add(filter, BooleanClause.Occur.MUST); // } if (logger.isDebugEnabled()) { logger.debug("[RepositoryAwareADMLuceneSearcherImpl::query] Repository '" + getRepository() + "' -- Retrieving searcher for Indexer: " + indexer); } RepositoryAwareClosingIndexSearcher searcher = getSearcher(indexer); Query query = LuceneQueryParser.parse(parameterisedQueryString, DEFAULT_FIELD, new LuceneAnalyser(getDictionaryService(), searchParameters.getMlAnalaysisMode() == null ? getLuceneConfig().getDefaultMLSearchAnalysisMode() : searchParameters.getMlAnalaysisMode()), namespacePrefixResolver, getDictionaryService(), tenantService, defaultOperator, searchParameters, getLuceneConfig(), searcher.getIndexReader()); if (logger.isDebugEnabled()) { logger.debug( "[RepositoryAwareADMLuceneSearcherImpl::query] Query is '" + query.toString() + "'"); } if (searcher == null) { // no index return an empty result set return new EmptyResultSet(); } Hits hits; if (!searchParameters.getSortDefinitions().isEmpty()) { int index = 0; SortField[] fields = new SortField[searchParameters.getSortDefinitions().size()]; for (SearchParameters.SortDefinition sd : searchParameters.getSortDefinitions()) { switch (sd.getSortType()) { case FIELD: String field = sd.getField(); if (field.startsWith("@")) { field = expandAttributeFieldName(field); PropertyDefinition propertyDef = getDictionaryService() .getProperty(QName.createQName(field.substring(1))); if (propertyDef.getDataType().getName().equals(DataTypeDefinition.DATETIME)) { DataTypeDefinition dataType = propertyDef.getDataType(); String analyserClassName = dataType.getAnalyserClassName(); if (analyserClassName.equals(DateTimeAnalyser.class.getCanonicalName())) { field = field + ".sort"; } } } if (fieldHasTerm(searcher.getReader(), field)) { fields[index++] = new SortField(field, !sd.isAscending()); } else { fields[index++] = new SortField(null, SortField.DOC, !sd.isAscending()); } break; case DOCUMENT: fields[index++] = new SortField(null, SortField.DOC, !sd.isAscending()); break; case SCORE: fields[index++] = new SortField(null, SortField.SCORE, !sd.isAscending()); break; } } hits = searcher.search(query, new Sort(fields)); } else { hits = searcher.search(query); } if (logger.isDebugEnabled()) { logger.debug("[RepositoryAwareADMLuceneSearcherImpl::query] Repository '" + getRepository() + "' -- Lucene query executed: " + query.toString()); logger.debug("[RepositoryAwareADMLuceneSearcherImpl::query] Repository '" + getRepository() + "' -- Lucene Results count: " + hits.length()); } Path[] paths = searchParameters.getAttributePaths().toArray(new Path[0]); LuceneResultSet rs = new LuceneResultSet(hits, searcher, nodeService, tenantService, paths, searchParameters); if (logger.isDebugEnabled()) { logger.debug("[RepositoryAwareADMLuceneSearcherImpl::query] Repository '" + getRepository() + "' -- Lucene Results created"); } return rs; } catch (ParseException e) { throw new SearcherException("Failed to parse query: " + parameterisedQueryString, e); } catch (IOException e) { throw new SearcherException("IO exception during search", e); } } else if (searchParameters.getLanguage().equalsIgnoreCase(SearchService.LANGUAGE_XPATH)) { try { XPathReader reader = new XPathReader(); LuceneXPathHandler handler = new LuceneXPathHandler(); handler.setNamespacePrefixResolver(namespacePrefixResolver); handler.setDictionaryService(getDictionaryService()); // TODO: Handler should have the query parameters to use in // building its lucene query // At the moment xpath style parameters in the PATH // expression are not supported. reader.setXPathHandler(handler); reader.parse(parameterisedQueryString); Query userQuery = handler.getQuery(); // Aggiunta filtraggio per permessi di lettura BooleanQuery query = new BooleanQuery(); query.add(userQuery, BooleanClause.Occur.MUST); // if (filter != null) { // query.add(filter, BooleanClause.Occur.MUST); // } if (logger.isDebugEnabled()) { logger.debug("[RepositoryAwareADMLuceneSearcherImpl::query] Repository '" + getRepository() + "' -- Retrieving searcher for Indexer: " + indexer); } // Searcher searcher = getSearcher(null); Searcher searcher = getSearcher(indexer); if (searcher == null) { // no index return an empty result set return new EmptyResultSet(); } Hits hits = searcher.search(query); if (logger.isDebugEnabled()) { logger.debug("[RepositoryAwareADMLuceneSearcherImpl::query] Repository '" + getRepository() + "' -- XPath query executed: " + query.toString()); logger.debug("[RepositoryAwareADMLuceneSearcherImpl::query] Repository '" + getRepository() + "' -- XPath Results count: " + hits.length()); } Path[] paths = searchParameters.getAttributePaths().toArray(new Path[0]); LuceneResultSet rs = new LuceneResultSet(hits, searcher, nodeService, tenantService, paths, searchParameters); if (logger.isDebugEnabled()) { logger.debug("[RepositoryAwareADMLuceneSearcherImpl::query] Repository '" + getRepository() + "' -- XPath Results created"); } return rs; //return new LuceneResultSet(hits, searcher, nodeService, tenantService, searchParameters.getAttributePaths().toArray(new Path[0]), searchParameters); } catch (SAXPathException e) { throw new SearcherException("Failed to parse query: " + searchParameters.getQuery(), e); } catch (IOException e) { throw new SearcherException("IO exception during search", e); } } else { throw new SearcherException("Unknown query language: " + searchParameters.getLanguage()); } }
From source file:it.pronetics.madstore.repository.index.impl.LuceneSearcher.java
License:Apache License
private PagingList<SearchResult> doSearch(Query query, int offset, int max) throws Exception { if (max == 0) { max = LuceneIndexManager.DEFAULT_MAX_SEARCH_RESULTS; }/*from w w w . j av a 2 s .c o m*/ int limit = offset + max; IndexSearcher searcher = threadLocalSeacher.get(); TopFieldDocs topFieldDocs = searcher.search(query, null, limit, new Sort(SortField.FIELD_SCORE)); PagingList<SearchResult> entryItems = new PagingList<SearchResult>(new ArrayList<SearchResult>(), offset, max, topFieldDocs.totalHits); for (int i = offset; i < (offset + max) && i < topFieldDocs.totalHits; i++) { Document doc = searcher.doc(topFieldDocs.scoreDocs[i].doc, new MapFieldSelector( new String[] { LuceneIndexManager.INDEX_COLLECTION_KEY, LuceneIndexManager.INDEX_ENTRY_KEY })); String collectionKey = doc.get(LuceneIndexManager.INDEX_COLLECTION_KEY); String entryKey = doc.get(LuceneIndexManager.INDEX_ENTRY_KEY); if (collectionKey != null && entryKey != null) { entryItems.add(new SearchResult(collectionKey, entryKey)); } else { LOG.warn("Found an entry with missing collection ({}) or entry ({}) key.", collectionKey, entryKey); } } return entryItems; }
From source file:jboss.jbw2011.ogm.ServiceImpl.java
License:Open Source License
/** * To search for all tweets, sorted in creation order (assuming the timestamp is correct). * @return/*w ww. j av a 2s.c om*/ */ public FullTextQuery allTweetsSortedByTime() { Query query = getQueryBuilder().all().createQuery(); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query); fullTextQuery.initializeObjectsWith(ObjectLookupMethod.PERSISTENCE_CONTEXT, DatabaseRetrievalMethod.FIND_BY_ID); fullTextQuery.setSort(new Sort(new SortField("timestamp", SortField.LONG))); return fullTextQuery; }
From source file:lia.chapter5.SortingExample.java
License:Apache License
public static void main(String[] args) throws Exception { Query allBooks = new MatchAllDocsQuery(); QueryParser parser = new QueryParser("contents", // #1 new StandardAnalyzer( // #1 )); // #1 BooleanQuery.Builder query = new BooleanQuery.Builder(); query.add(allBooks, BooleanClause.Occur.SHOULD); // #1 query.add(parser.parse("java OR action"), BooleanClause.Occur.SHOULD); // #1 SortingExample example = new SortingExample(); // #2 example.displayResults(query.build(), Sort.RELEVANCE); example.displayResults(query.build(), Sort.INDEXORDER); example.displayResults(query.build(), new Sort(new SortField("category", SortField.Type.STRING))); example.displayResults(query.build(), new Sort(new SortField("pubmonth", SortField.Type.INT, true))); example.displayResults(query.build(), new Sort(new SortField("category", SortField.Type.STRING), SortField.FIELD_SCORE, new SortField("pubmonth", SortField.Type.INT, true))); example.displayResults(query.build(), new Sort( new SortField[] { SortField.FIELD_SCORE, new SortField("category", SortField.Type.STRING) })); }
From source file:lucandra.LucandraTests.java
License:Apache License
public void testSortQuery() throws Exception { IndexReader indexReader = new IndexReader(indexName, client); IndexSearcher searcher = new IndexSearcher(indexReader); QueryParser qp = new QueryParser(Version.LUCENE_CURRENT, "key", analyzer); // check sort Sort sort = new Sort(new SortField("date", SortField.STRING)); Query q = qp.parse("+key:sort"); TopDocs docs = searcher.search(q, null, 10, sort); for (int i = 0; i < 10; i++) { Document d = indexReader.document(docs.scoreDocs[i].doc); String dval = d.get("date"); assertEquals("test" + (i + 200), dval); }/*from w ww . j a v a 2 s .c o m*/ }