List of usage examples for org.apache.lucene.search Sort RELEVANCE
Sort RELEVANCE
To view the source code for org.apache.lucene.search Sort RELEVANCE.
Click Source Link
From source file:org.neo4j.index.lucene.QueryContext.java
License:Open Source License
/** * @return a QueryContext with sorting by relevance, i.e. sorted after which * score each hit has. */ public QueryContext sortByScore() { return sort(Sort.RELEVANCE); }
From source file:org.neo4j.kernel.api.impl.index.collector.DocValuesCollector.java
License:Open Source License
private TopDocs getTopDocs(Sort sort, int size) throws IOException { TopDocs topDocs;// ww w .jav a 2s. c o m if (sort == Sort.RELEVANCE) { TopScoreDocCollector collector = TopScoreDocCollector.create(size); replayTo(collector); topDocs = collector.topDocs(); } else { TopFieldCollector collector = TopFieldCollector.create(sort, size, false, true, false); replayTo(collector); topDocs = collector.topDocs(); } return topDocs; }
From source file:org.neo4j.kernel.api.impl.index.collector.DocValuesCollectorTest.java
License:Open Source License
@Test public void shouldReturnIndexHitsOrderedByRelevance() throws Exception { // given//w w w. j a va 2 s . c o m DocValuesCollector collector = new DocValuesCollector(true); IndexReaderStub readerStub = indexReaderWithMaxDocs(42); // when collector.doSetNextReader(readerStub.getContext()); collector.setScorer(constantScorer(1.0f)); collector.collect(1); collector.setScorer(constantScorer(2.0f)); collector.collect(2); // then IndexHits<Document> indexHits = collector.getIndexHits(Sort.RELEVANCE); assertEquals(2, indexHits.size()); assertEquals("2", indexHits.next().get("id")); assertEquals(2.0f, indexHits.currentScore(), 0.0f); assertEquals("1", indexHits.next().get("id")); assertEquals(1.0f, indexHits.currentScore(), 0.0f); assertFalse(indexHits.hasNext()); }
From source file:org.neo4j.kernel.api.impl.index.collector.DocValuesCollectorTest.java
License:Open Source License
@Test public void shouldReturnDocValuesInRelevanceOrder() throws Exception { // given/*from www. ja va 2s . com*/ DocValuesCollector collector = new DocValuesCollector(true); IndexReaderStub readerStub = indexReaderWithMaxDocs(42); // when collector.doSetNextReader(readerStub.getContext()); collector.setScorer(constantScorer(1.0f)); collector.collect(1); collector.setScorer(constantScorer(2.0f)); collector.collect(2); // then PrimitiveLongIterator valuesIterator = collector.getSortedValuesIterator("id", Sort.RELEVANCE); assertEquals(2, valuesIterator.next()); assertEquals(1, valuesIterator.next()); assertFalse(valuesIterator.hasNext()); }
From source file:org.neo4j.kernel.api.impl.index.collector.DocValuesCollectorTest.java
License:Open Source License
@Test public void shouldSilentlyMergeSegmentsWhenReturnDocValuesInOrder() throws Exception { // given/*from w w w . j ava 2s . co m*/ DocValuesCollector collector = new DocValuesCollector(true); IndexReaderStub readerStub = indexReaderWithMaxDocs(42); // when collector.doSetNextReader(readerStub.getContext()); collector.setScorer(constantScorer(1.0f)); collector.collect(1); collector.doSetNextReader(readerStub.getContext()); collector.setScorer(constantScorer(2.0f)); collector.collect(2); // then PrimitiveLongIterator valuesIterator = collector.getSortedValuesIterator("id", Sort.RELEVANCE); assertEquals(2, valuesIterator.next()); assertEquals(1, valuesIterator.next()); assertFalse(valuesIterator.hasNext()); }
From source file:org.neo4j.kernel.api.impl.index.collector.DocValuesCollectorTest.java
License:Open Source License
@Test public void shouldReturnEmptyIteratorWhenNoDocValuesInOrder() throws Exception { // given//from www. ja v a2 s . co m DocValuesCollector collector = new DocValuesCollector(false); IndexReaderStub readerStub = indexReaderWithMaxDocs(42); // when collector.doSetNextReader(readerStub.getContext()); // then PrimitiveLongIterator valuesIterator = collector.getSortedValuesIterator("id", Sort.RELEVANCE); assertFalse(valuesIterator.hasNext()); }
From source file:org.opengrok.indexer.web.SearchHelper.java
License:Open Source License
/** * Create the searcher to use w.r.t. currently set parameters and the given * projects. Does not produce any {@link #redirect} link. It also does * nothing if {@link #redirect} or {@link #errorMsg} have a * none-{@code null} value./*from w w w .j ava 2 s . c o m*/ * <p> * Parameters which should be populated/set at this time: * <ul> * <li>{@link #builder}</li> <li>{@link #dataRoot}</li> * <li>{@link #order} (falls back to relevance if unset)</li> * <li>{@link #parallel} (default: false)</li> </ul> Populates/sets: <ul> * <li>{@link #query}</li> <li>{@link #searcher}</li> <li>{@link #sort}</li> * <li>{@link #projects}</li> <li>{@link #errorMsg} if an error occurs</li> * </ul> * * @param projects project names. If empty, a no-project setup * is assumed (i.e. DATA_ROOT/index will be used instead of possible * multiple DATA_ROOT/$project/index). If the set contains projects * not known in the configuration or projects not yet indexed, * an error will be returned in {@link #errorMsg}. * @return this instance */ public SearchHelper prepareExec(SortedSet<String> projects) { if (redirect != null || errorMsg != null) { return this; } mappedAnalysisSettings = null; // the Query created by the QueryBuilder try { indexDir = new File(dataRoot, IndexDatabase.INDEX_DIR); query = builder.build(); if (projects == null) { errorMsg = "No project selected!"; return this; } this.projects = projects; if (projects.isEmpty()) { // no project setup FSDirectory dir = FSDirectory.open(indexDir.toPath()); reader = DirectoryReader.open(dir); searcher = new IndexSearcher(reader); closeOnDestroy = true; } else { // Check list of project names first to make sure all of them // are valid and indexed. closeOnDestroy = false; Set<String> invalidProjects = projects.stream().filter(proj -> (Project.getByName(proj) == null)) .collect(Collectors.toSet()); if (invalidProjects.size() > 0) { errorMsg = "Project list contains invalid projects: " + String.join(", ", invalidProjects); return this; } Set<Project> notIndexedProjects = projects.stream().map(x -> Project.getByName(x)) .filter(proj -> !proj.isIndexed()).collect(Collectors.toSet()); if (notIndexedProjects.size() > 0) { errorMsg = "Some of the projects to be searched are not indexed yet: " + String.join(", ", notIndexedProjects.stream().map(proj -> proj.getName()).collect(Collectors.toSet())); return this; } // We use MultiReader even for single project. This should // not matter given that MultiReader is just a cheap wrapper // around set of IndexReader objects. reader = RuntimeEnvironment.getInstance().getMultiReader(projects, searcherList); if (reader != null) { searcher = new IndexSearcher(reader); } else { errorMsg = "Failed to initialize search. Check the index."; return this; } } // TODO check if below is somehow reusing sessions so we don't // requery again and again, I guess 2min timeout sessions could be // useful, since you click on the next page within 2mins, if not, // then wait ;) // Most probably they are not reused. SearcherLifetimeManager might help here. switch (order) { case LASTMODIFIED: sort = new Sort(new SortField(QueryBuilder.DATE, SortField.Type.STRING, true)); break; case BY_PATH: sort = new Sort(new SortField(QueryBuilder.FULLPATH, SortField.Type.STRING)); break; default: sort = Sort.RELEVANCE; break; } checker = new DirectSpellChecker(); } catch (ParseException e) { errorMsg = PARSE_ERROR_MSG + e.getMessage(); } catch (FileNotFoundException e) { // errorMsg = "Index database(s) not found: " + e.getMessage(); errorMsg = "Index database(s) not found."; } catch (IOException e) { errorMsg = e.getMessage(); } return this; }
From source file:org.opensolaris.opengrok.web.SearchHelper.java
License:Open Source License
/** * Create the searcher to use wrt. to currently set parameters and the given * projects. Does not produce any {@link #redirect} link. It also does * nothing if {@link #redirect} or {@link #errorMsg} have a * none-{@code null} value. <p> Parameters which should be populated/set at * this time: <ul> <li>{@link #builder}</li> <li>{@link #dataRoot}</li> * <li>{@link #order} (falls back to relevance if unset)</li> * <li>{@link #parallel} (default: false)</li> </ul> Populates/sets: <ul> * <li>{@link #query}</li> <li>{@link #searcher}</li> <li>{@link #sort}</li> * <li>{@link #projects}</li> <li>{@link #errorMsg} if an error occurs</li> * </ul>//from w ww . j a va2s. c o m * * @param projects project to use query. If empty, a none-project opengrok * setup is assumed (i.e. DATA_ROOT/index will be used instead of possible * multiple DATA_ROOT/$project/index). * @return this instance */ public SearchHelper prepareExec(SortedSet<String> projects) { if (redirect != null || errorMsg != null) { return this; } // the Query created by the QueryBuilder try { indexDir = new File(dataRoot, IndexDatabase.INDEX_DIR); query = builder.build(); if (projects == null) { errorMsg = "No project selected!"; return this; } this.projects = projects; if (projects.isEmpty()) { //no project setup FSDirectory dir = FSDirectory.open(indexDir); searcher = new IndexSearcher(DirectoryReader.open(dir)); } else if (projects.size() == 1) { // just 1 project selected FSDirectory dir = FSDirectory.open(new File(indexDir, projects.first())); searcher = new IndexSearcher(DirectoryReader.open(dir)); } else { //more projects IndexReader[] subreaders = new IndexReader[projects.size()]; int ii = 0; //TODO might need to rewrite to Project instead of // String , need changes in projects.jspf too for (String proj : projects) { FSDirectory dir = FSDirectory.open(new File(indexDir, proj)); subreaders[ii++] = DirectoryReader.open(dir); } MultiReader searchables = new MultiReader(subreaders, true); if (parallel) { int noThreads = 2 + (2 * Runtime.getRuntime().availableProcessors()); //TODO there might be a better way for counting this executor = Executors.newFixedThreadPool(noThreads); } searcher = parallel ? new IndexSearcher(searchables, executor) : new IndexSearcher(searchables); } // TODO check if below is somehow reusing sessions so we don't // requery again and again, I guess 2min timeout sessions could be // usefull, since you click on the next page within 2mins, if not, // then wait ;) switch (order) { case LASTMODIFIED: sort = new Sort(new SortField(QueryBuilder.DATE, SortField.Type.STRING, true)); break; case BY_PATH: sort = new Sort(new SortField(QueryBuilder.FULLPATH, SortField.Type.STRING)); break; default: sort = Sort.RELEVANCE; break; } checker = new DirectSpellChecker(); } catch (ParseException e) { errorMsg = PARSE_ERROR_MSG + e.getMessage(); } catch (FileNotFoundException e) { // errorMsg = "Index database(s) not found: " + e.getMessage(); errorMsg = "Index database(s) not found."; } catch (Exception e) { errorMsg = e.getMessage(); } return this; }
From source file:org.pageseeder.flint.lucene.query.BasicQuery.java
License:Apache License
/** * Returns the sort order for the results. * * @return the sort order for the results (defaults to relevance). *//*from w w w . j ava 2 s. c o m*/ @Override public final Sort getSort() { return this._sort != null ? this._sort : Sort.RELEVANCE; }
From source file:org.pageseeder.flint.lucene.query.BasicQuery.java
License:Apache License
/** * Generates the XML for this query.//w ww . ja va 2s . c om * * <p>As: * <pre>{@code * <basic-query empty="[true|false]" query="[lucene query]"> * <base> * <!-- The base query using the toXML() method --> * </base> * <parameters> * <!-- Each parameters in order using the toXML() method --> * </parameters> * </basic-query> * }</pre> * * {@inheritDoc} */ @Override public void toXML(XMLWriter xml) throws IOException { xml.openElement("basic-query", true); xml.attribute("empty", Boolean.toString(isEmpty())); if (!isEmpty()) { xml.attribute("query", this._query.toString()); // Base query xml.openElement("base", true); this._base.toXML(xml); xml.closeElement(); // Parameters xml.openElement("parameters", !this.parameters().isEmpty()); for (SearchParameter p : parameters()) { p.toXML(xml); } xml.closeElement(); xml.openElement("sort", this._sort != Sort.RELEVANCE); if (this._sort == Sort.RELEVANCE) { xml.attribute("by", "relevance"); } else { xml.attribute("by", "fields"); for (SortField sf : this._sort.getSort()) { xml.openElement("sortfield"); xml.attribute("field", sf.getField()); xml.attribute("type", sf.getType().toString().toLowerCase()); xml.attribute("reverse", Boolean.toString(sf.getReverse())); xml.closeElement(); } } xml.closeElement(); } xml.closeElement(); }