List of usage examples for org.apache.lucene.search Sort INDEXORDER
Sort INDEXORDER
To view the source code for org.apache.lucene.search Sort INDEXORDER.
Click Source Link
From source file:org.sindice.siren.search.TestSirenNumericRangeQuery32.java
License:Open Source License
private void testLeftOpenRange(final int precisionStep) throws Exception { final String field = "field" + precisionStep; final int count = 3000; final int upper = (count - 1) * distance + (distance / 3) + startOffset; SirenNumericRangeQuery<Integer> q = SirenNumericRangeQuery.newIntRange(field, precisionStep, null, upper, true, true);//from w w w.j ava 2 s .co m SirenCellQuery cq = new SirenCellQuery(q); cq.setConstraint(2); TopDocs topDocs = searcher.search(cq, null, noDocs, Sort.INDEXORDER); if (VERBOSE) System.out.println("Found " + q.getTotalNumberOfTerms() + " distinct terms in left open range for field '" + field + "'."); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", count, sd.length); Document doc = searcher.doc(sd[0].doc); assertEquals("First doc", startOffset, Integer.parseInt(getLiteralValue(doc.get(field)))); doc = searcher.doc(sd[sd.length - 1].doc); assertEquals("Last doc", (count - 1) * distance + startOffset, Integer.parseInt(getLiteralValue(doc.get(field)))); q = SirenNumericRangeQuery.newIntRange(field, precisionStep, null, upper, false, true); cq = new SirenCellQuery(q); cq.setConstraint(2); topDocs = searcher.search(cq, null, noDocs, Sort.INDEXORDER); sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", count, sd.length); doc = searcher.doc(sd[0].doc); assertEquals("First doc", startOffset, Integer.parseInt(getLiteralValue(doc.get(field)))); doc = searcher.doc(sd[sd.length - 1].doc); assertEquals("Last doc", (count - 1) * distance + startOffset, Integer.parseInt(getLiteralValue(doc.get(field)))); }
From source file:org.sindice.siren.search.TestSirenNumericRangeQuery32.java
License:Open Source License
private void testRightOpenRange(final int precisionStep) throws Exception { final String field = "field" + precisionStep; final int count = 3000; final int lower = (count - 1) * distance + (distance / 3) + startOffset; SirenNumericRangeQuery<Integer> q = SirenNumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, true);// www.j ava 2 s .co m SirenCellQuery cq = new SirenCellQuery(q); cq.setConstraint(2); TopDocs topDocs = searcher.search(cq, null, noDocs, Sort.INDEXORDER); if (VERBOSE) System.out.println("Found " + q.getTotalNumberOfTerms() + " distinct terms in right open range for field '" + field + "'."); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", noDocs - count, sd.length); Document doc = searcher.doc(sd[0].doc); assertEquals("First doc", count * distance + startOffset, Integer.parseInt(getLiteralValue(doc.get(field)))); doc = searcher.doc(sd[sd.length - 1].doc); assertEquals("Last doc", (noDocs - 1) * distance + startOffset, Integer.parseInt(getLiteralValue(doc.get(field)))); q = SirenNumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, false); cq = new SirenCellQuery(q); cq.setConstraint(2); topDocs = searcher.search(cq, null, noDocs, Sort.INDEXORDER); sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", noDocs - count, sd.length); doc = searcher.doc(sd[0].doc); assertEquals("First doc", count * distance + startOffset, Integer.parseInt(getLiteralValue(doc.get(field)))); doc = searcher.doc(sd[sd.length - 1].doc); assertEquals("Last doc", (noDocs - 1) * distance + startOffset, Integer.parseInt(getLiteralValue(doc.get(field)))); }
From source file:org.sindice.siren.search.TestSirenNumericRangeQuery64.java
License:Open Source License
/** test for constant score + boolean query + filter, the other tests only use the constant score mode */ private void testRange(final int precisionStep) throws Exception { final String field = "field" + precisionStep; final int count = 3000; final long lower = (distance * 3 / 2) + startOffset, upper = lower + count * distance + (distance / 3); final SirenNumericRangeQuery<Long> q = SirenNumericRangeQuery.newLongRange(field, precisionStep, lower, upper, true, true);/*from w ww. j a v a2 s . c om*/ final SirenCellQuery cq = new SirenCellQuery(q); cq.setConstraint(2); TopDocs topDocs; int terms; String type; q.clearTotalNumberOfTerms(); type = " (constant score boolean rewrite)"; q.setRewriteMethod(SirenMultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE); topDocs = searcher.search(cq, null, noDocs, Sort.INDEXORDER); terms = q.getTotalNumberOfTerms(); if (VERBOSE) System.out .println("Found " + terms + " distinct terms in range for field '" + field + "'" + type + "."); final ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count" + type, count, sd.length); Document doc = searcher.doc(sd[0].doc); assertEquals("First doc" + type, 2 * distance + startOffset, Long.parseLong(getLiteralValue(doc.get(field)))); doc = searcher.doc(sd[sd.length - 1].doc); assertEquals("Last doc" + type, (1 + count) * distance + startOffset, Long.parseLong(getLiteralValue(doc.get(field)))); }
From source file:org.sindice.siren.search.TestSirenNumericRangeQuery64.java
License:Open Source License
private void testLeftOpenRange(final int precisionStep) throws Exception { final String field = "field" + precisionStep; final int count = 3000; final long upper = (count - 1) * distance + (distance / 3) + startOffset; SirenNumericRangeQuery<Long> q = SirenNumericRangeQuery.newLongRange(field, precisionStep, null, upper, true, true);//from w w w.ja va2 s . co m SirenCellQuery cq = new SirenCellQuery(q); cq.setConstraint(2); TopDocs topDocs = searcher.search(cq, null, noDocs, Sort.INDEXORDER); if (VERBOSE) System.out.println("Found " + q.getTotalNumberOfTerms() + " distinct terms in left open range for field '" + field + "'."); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", count, sd.length); Document doc = searcher.doc(sd[0].doc); assertEquals("First doc", startOffset, Long.parseLong(getLiteralValue(doc.get(field)))); doc = searcher.doc(sd[sd.length - 1].doc); assertEquals("Last doc", (count - 1) * distance + startOffset, Long.parseLong(getLiteralValue(doc.get(field)))); q = SirenNumericRangeQuery.newLongRange(field, precisionStep, null, upper, false, true); cq = new SirenCellQuery(q); cq.setConstraint(2); topDocs = searcher.search(cq, null, noDocs, Sort.INDEXORDER); sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", count, sd.length); doc = searcher.doc(sd[0].doc); assertEquals("First doc", startOffset, Long.parseLong(getLiteralValue(doc.get(field)))); doc = searcher.doc(sd[sd.length - 1].doc); assertEquals("Last doc", (count - 1) * distance + startOffset, Long.parseLong(getLiteralValue(doc.get(field)))); }
From source file:org.sindice.siren.search.TestSirenNumericRangeQuery64.java
License:Open Source License
private void testRightOpenRange(final int precisionStep) throws Exception { final String field = "field" + precisionStep; final int count = 3000; final long lower = (count - 1) * distance + (distance / 3) + startOffset; SirenNumericRangeQuery<Long> q = SirenNumericRangeQuery.newLongRange(field, precisionStep, lower, null, true, true);// ww w . ja va2s. co m SirenCellQuery cq = new SirenCellQuery(q); cq.setConstraint(2); TopDocs topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); if (VERBOSE) System.out.println("Found " + q.getTotalNumberOfTerms() + " distinct terms in right open range for field '" + field + "'."); ScoreDoc[] sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", noDocs - count, sd.length); Document doc = searcher.doc(sd[0].doc); assertEquals("First doc", count * distance + startOffset, Long.parseLong(getLiteralValue(doc.get(field)))); doc = searcher.doc(sd[sd.length - 1].doc); assertEquals("Last doc", (noDocs - 1) * distance + startOffset, Long.parseLong(getLiteralValue(doc.get(field)))); q = SirenNumericRangeQuery.newLongRange(field, precisionStep, lower, null, true, false); cq = new SirenCellQuery(q); cq.setConstraint(2); topDocs = searcher.search(q, null, noDocs, Sort.INDEXORDER); sd = topDocs.scoreDocs; assertNotNull(sd); assertEquals("Score doc count", noDocs - count, sd.length); doc = searcher.doc(sd[0].doc); assertEquals("First doc", count * distance + startOffset, Long.parseLong(getLiteralValue(doc.get(field)))); doc = searcher.doc(sd[sd.length - 1].doc); assertEquals("Last doc", (noDocs - 1) * distance + startOffset, Long.parseLong(getLiteralValue(doc.get(field)))); }
From source file:org.weborganic.flint.IndexManager.java
License:artistic-license-2.0
/** * Run a search on the given Index.//from w w w. j av a2 s .c o m * * @param index the Index to run the search on * @param query the query to run * @param paging paging details (can be <code>null</code>) * * @return the search results * * @throws IndexException if any error occurred while performing the search */ public SearchResults query(Index index, SearchQuery query, SearchPaging paging) throws IndexException { IndexIO io = getIndexIO(index); IndexSearcher searcher = null; try { searcher = io.bookSearcher(); } catch (CorruptIndexException ex) { throw new IndexException("Failed getting a Searcher to perform a query because the Index is corrupted", ex); } catch (LockObtainFailedException ex) { throw new IndexException("Failed getting a lock on the Index to perform a query", ex); } catch (IOException ex) { throw new IndexException( "Failed getting a searcher to perform a query on the Index because of an I/O problem", ex); } if (searcher != null) { try { Query lquery = query.toQuery(); if (lquery == null) { try { io.releaseSearcher(searcher); } catch (IOException ex) { LOGGER.error( "Failed releasing a Searcher after performing a query because of an I/O problem", ex); } throw new IndexException("Failed performing a query on the Index because the query is null", new NullPointerException("Null query")); } LOGGER.debug("Performing search [{}] on index {}", query, index); Sort sort = query.getSort(); if (sort == null) sort = Sort.INDEXORDER; // load the scores TopFieldCollector tfc = TopFieldCollector.create(sort, paging.getHitsPerPage() * paging.getPage(), true, true, false, true); searcher.search(lquery, tfc); return new SearchResults(query, tfc.topDocs().scoreDocs, tfc.getTotalHits(), paging, io, searcher); } catch (IOException e) { try { io.releaseSearcher(searcher); } catch (IOException ioe) { LOGGER.error( "Failed releasing a Searcher after performing a query on the Index because of an I/O problem", ioe); } throw new IndexException("Failed performing a query on the Index because of an I/O problem", e); } } return null; }
From source file:org.weborganic.flint.query.PredicateSearchQuery.java
License:artistic-license-2.0
/** * Creates new predicate search query.//from w ww . ja v a2 s . c o m * * @param predicate The predicate for this query. * @param sortField The field name to use to order the results. * * @throws IllegalArgumentException If the predicate is <code>null</code>. */ public PredicateSearchQuery(String predicate, String sortField) throws IllegalArgumentException { this(predicate, sortField == null ? Sort.INDEXORDER : new Sort(new SortField(sortField, SortField.STRING))); }
From source file:org.weborganic.flint.query.PredicateSearchQuery.java
License:artistic-license-2.0
/** * Creates new predicate search query./*from w w w.ja v a2 s . c o m*/ * * @param predicate The predicate for this query. * @param analyzer The analyzer to use for the query, should be the same as the one used to write the Index. * @param sortField The field name to use to order the results. * * @throws IllegalArgumentException If the predicate is <code>null</code>. */ public PredicateSearchQuery(String predicate, Analyzer analyzer, String sortField) throws IllegalArgumentException { this(predicate, analyzer, sortField == null ? Sort.INDEXORDER : new Sort(new SortField(sortField, SortField.STRING))); }
From source file:tml.corpus.Corpus.java
License:Apache License
/** * <p>/*from w ww.j a va 2 s. c o m*/ * This method searches for whatever you want, full documents, sentences or * paragraphs. All mixed up, so this should only be used by experts that * know how tml uses the Lucene index to store its data. * </p> * <p> * For example, to find all the sentences from a document with external id * "foo" * </p> * * <pre> * String query = "type:sentence AND reference:foo"; * searchFullOpenQuery(query); * </pre> * <p> * It returns a Lucene Hits results because the documents inside can't be * used directly to create a Corpus * </p> * * @param query * the Lucene query * @return the search results */ private TopFieldDocs searchFullOpenQuery(Repository storage, String query) { assert (query != null); // The query is parsed QueryParser parser = new QueryParser(Version.LUCENE_29, storage.getLuceneContentField(), new KeywordAnalyzer()); parser.setLowercaseExpandedTerms(false); Query documentsQuery = null; try { documentsQuery = parser.parse(query); } catch (ParseException e) { e.printStackTrace(); logger.error(e.toString()); return null; } // The index is searched using the query TopFieldDocs docs = null; try { docs = new IndexSearcher(storage.getIndexReader()).search(documentsQuery, null, 9999, Sort.INDEXORDER); } catch (Exception e) { logger.error(e.toString()); return null; } return docs; }