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:com.fuerve.villageelder.search.SearchTest.java
License:Apache License
/** * Test method for {@link com.fuerve.villageelder.search.Search#Search(java.lang.String, java.util.Map, org.apache.lucene.analysis.Analyzer, org.apache.lucene.search.Sort)}. *//*from ww w .j av a 2 s . co m*/ @Test @SuppressWarnings({ "unchecked", "unused" }) public final void testSearchStringMapOfStringIntegerAnalyzerSort() throws Exception { // Constants Field defaultSortField = Search.class.getDeclaredField("DEFAULT_SORT"); Field defaultFacetsField = Search.class.getDeclaredField("DEFAULT_FACETS"); Field defaultFacetStringsField = Search.class.getDeclaredField("DEFAULT_FACET_STRINGS"); Field defaultAnalyzerField = Search.class.getDeclaredField("DEFAULT_ANALYZER"); Field defaultHitsField = Search.class.getDeclaredField("DEFAULT_HITS"); defaultSortField.setAccessible(true); defaultFacetsField.setAccessible(true); defaultFacetStringsField.setAccessible(true); defaultAnalyzerField.setAccessible(true); defaultHitsField.setAccessible(true); final Sort defaultSort = (Sort) defaultSortField.get(null); final List<FacetRequest> defaultFacets = (List<FacetRequest>) defaultFacetsField.get(null); final Map<String, Integer> defaultFacetStrings = (Map<String, Integer>) defaultFacetStringsField.get(null); final Analyzer defaultAnalyzer = (Analyzer) defaultAnalyzerField.get(null); final int defaultHits = defaultHitsField.getInt(null); // Private members Field queryField = Search.class.getDeclaredField("query"); Field sortField = Search.class.getDeclaredField("sort"); Field facetsField = Search.class.getDeclaredField("facets"); queryField.setAccessible(true); sortField.setAccessible(true); facetsField.setAccessible(true); // Test setup QueryParser parser = getQueryParser(); String queryExpected = "test:foo"; Map<String, Integer> facetsExpected = new HashMap<String, Integer>(); Sort sortExpected = Sort.RELEVANCE; facetsExpected.put("testFacet", 99); Search target = new Search(queryExpected, facetsExpected, Lucene.getPerFieldAnalyzer(), sortExpected); // Gather fields Query queryActual = (Query) queryField.get(target); Sort sortActual = (Sort) sortField.get(target); List<FacetRequest> facetsActual = (List<FacetRequest>) facetsField.get(target); // Test assertEquals(queryExpected, queryActual.toString()); assertEquals(sortExpected, sortActual); assertEquals("testFacet", facetsActual.get(0).categoryPath.toString()); }
From source file:com.fuerve.villageelder.search.SearchTest.java
License:Apache License
/** * Test method for {@link com.fuerve.villageelder.search.Search#getCollector()}. */// w w w . j av a 2s . co m @Test @SuppressWarnings({ "unchecked", "unused", "rawtypes" }) public final void testGetCollector() throws Exception { // Constants Field defaultSortField = Search.class.getDeclaredField("DEFAULT_SORT"); Field defaultFacetsField = Search.class.getDeclaredField("DEFAULT_FACETS"); Field defaultFacetStringsField = Search.class.getDeclaredField("DEFAULT_FACET_STRINGS"); Field defaultAnalyzerField = Search.class.getDeclaredField("DEFAULT_ANALYZER"); Field defaultHitsField = Search.class.getDeclaredField("DEFAULT_HITS"); defaultSortField.setAccessible(true); defaultFacetsField.setAccessible(true); defaultFacetStringsField.setAccessible(true); defaultAnalyzerField.setAccessible(true); defaultHitsField.setAccessible(true); final Sort defaultSort = (Sort) defaultSortField.get(null); final List<FacetRequest> defaultFacets = (List<FacetRequest>) defaultFacetsField.get(null); final Map<String, Integer> defaultFacetStrings = (Map<String, Integer>) defaultFacetStringsField.get(null); final Analyzer defaultAnalyzer = (Analyzer) defaultAnalyzerField.get(null); final int defaultHits = defaultHitsField.getInt(null); // Private members Field queryField = Search.class.getDeclaredField("query"); Field sortField = Search.class.getDeclaredField("sort"); Field facetsField = Search.class.getDeclaredField("facets"); queryField.setAccessible(true); sortField.setAccessible(true); facetsField.setAccessible(true); // Test setup QueryParser parser = getQueryParser(); Query queryExpected = parser.parse("test:foo"); List<FacetRequest> facetsExpected = new ArrayList<FacetRequest>(); Sort sortExpected = Sort.RELEVANCE; Search target = new Search(queryExpected, facetsExpected, sortExpected); // Gather fields Query queryActual = (Query) queryField.get(target); Sort sortActual = (Sort) sortField.get(target); List<FacetRequest> facetsActual = (List<FacetRequest>) facetsField.get(target); // Test TopFieldCollector actual = target.getCollector(); Class clazz = null; Class[] clazzes = TopFieldCollector.class.getDeclaredClasses(); for (int i = 0; i < clazzes.length; i++) { if (clazzes[i].getSimpleName().equals("OutOfOrderOneComparatorNonScoringCollector")) { clazz = clazzes[i]; break; } } assertEquals(clazz, actual.getClass()); }
From source file:com.fuerve.villageelder.search.SearchTest.java
License:Apache License
/** * Test method for {@link com.fuerve.villageelder.search.Search#getCollector(int)}. *//*from w ww.j a v a 2s . c o m*/ @Test @SuppressWarnings({ "unchecked", "unused" }) public final void testGetCollectorInt() throws Exception { // Constants Field defaultSortField = Search.class.getDeclaredField("DEFAULT_SORT"); Field defaultFacetsField = Search.class.getDeclaredField("DEFAULT_FACETS"); Field defaultFacetStringsField = Search.class.getDeclaredField("DEFAULT_FACET_STRINGS"); Field defaultAnalyzerField = Search.class.getDeclaredField("DEFAULT_ANALYZER"); Field defaultHitsField = Search.class.getDeclaredField("DEFAULT_HITS"); defaultSortField.setAccessible(true); defaultFacetsField.setAccessible(true); defaultFacetStringsField.setAccessible(true); defaultAnalyzerField.setAccessible(true); defaultHitsField.setAccessible(true); final Sort defaultSort = (Sort) defaultSortField.get(null); final List<FacetRequest> defaultFacets = (List<FacetRequest>) defaultFacetsField.get(null); final Map<String, Integer> defaultFacetStrings = (Map<String, Integer>) defaultFacetStringsField.get(null); final Analyzer defaultAnalyzer = (Analyzer) defaultAnalyzerField.get(null); final int defaultHits = defaultHitsField.getInt(null); // Private members Field queryField = Search.class.getDeclaredField("query"); Field sortField = Search.class.getDeclaredField("sort"); Field facetsField = Search.class.getDeclaredField("facets"); queryField.setAccessible(true); sortField.setAccessible(true); facetsField.setAccessible(true); // Test setup QueryParser parser = getQueryParser(); Query queryExpected = parser.parse("test:foo"); List<FacetRequest> facetsExpected = new ArrayList<FacetRequest>(); Sort sortExpected = Sort.RELEVANCE; Search target = new Search(queryExpected, facetsExpected, sortExpected); // Gather fields Query queryActual = (Query) queryField.get(target); Sort sortActual = (Sort) sortField.get(target); List<FacetRequest> facetsActual = (List<FacetRequest>) facetsField.get(target); // Test TopFieldCollector actual = target.getCollector(99); assertEquals("OutOfOrderOneComparatorNonScoringCollector", actual.getClass().getSimpleName()); }
From source file:com.fuerve.villageelder.search.SearchTest.java
License:Apache License
/** * Test method for {@link com.fuerve.villageelder.search.Search#getFacetsCollector(org.apache.lucene.index.DirectoryReader, org.apache.lucene.facet.taxonomy.TaxonomyReader)}. *//*from w ww .j a va 2 s . c o m*/ @Test @SuppressWarnings({ "unchecked", "unused" }) public final void testGetFacetsCollector() throws Exception { // Constants Field defaultSortField = Search.class.getDeclaredField("DEFAULT_SORT"); Field defaultFacetsField = Search.class.getDeclaredField("DEFAULT_FACETS"); Field defaultFacetStringsField = Search.class.getDeclaredField("DEFAULT_FACET_STRINGS"); Field defaultAnalyzerField = Search.class.getDeclaredField("DEFAULT_ANALYZER"); Field defaultHitsField = Search.class.getDeclaredField("DEFAULT_HITS"); defaultSortField.setAccessible(true); defaultFacetsField.setAccessible(true); defaultFacetStringsField.setAccessible(true); defaultAnalyzerField.setAccessible(true); defaultHitsField.setAccessible(true); final Sort defaultSort = (Sort) defaultSortField.get(null); final List<FacetRequest> defaultFacets = (List<FacetRequest>) defaultFacetsField.get(null); final Map<String, Integer> defaultFacetStrings = (Map<String, Integer>) defaultFacetStringsField.get(null); final Analyzer defaultAnalyzer = (Analyzer) defaultAnalyzerField.get(null); final int defaultHits = defaultHitsField.getInt(null); // Private members Field queryField = Search.class.getDeclaredField("query"); Field sortField = Search.class.getDeclaredField("sort"); Field facetsField = Search.class.getDeclaredField("facets"); queryField.setAccessible(true); sortField.setAccessible(true); facetsField.setAccessible(true); // Test setup QueryParser parser = getQueryParser(); Query queryExpected = parser.parse("test:foo"); List<FacetRequest> facetsExpected = new ArrayList<FacetRequest>(); Sort sortExpected = Sort.RELEVANCE; Search target = new Search(queryExpected, facetsExpected, sortExpected); target.addFacet("test", 100); // Gather fields Query queryActual = (Query) queryField.get(target); Sort sortActual = (Sort) sortField.get(target); List<FacetRequest> facetsActual = (List<FacetRequest>) facetsField.get(target); // Set up some dummy indices. Directory indexDirectory = new RAMDirectory(); IndexWriterConfig iwc = new IndexWriterConfig(Lucene.LUCENE_VERSION, Lucene.getPerFieldAnalyzer()); IndexWriter iw = new IndexWriter(indexDirectory, iwc); Directory taxonomyDirectory = new RAMDirectory(); TaxonomyWriter tw = new DirectoryTaxonomyWriter(taxonomyDirectory, OpenMode.CREATE); iw.commit(); tw.commit(); // Test FacetsCollector actual = target.getFacetsCollector(DirectoryReader.open(indexDirectory), new DirectoryTaxonomyReader(taxonomyDirectory)); assertEquals("DocsOnlyCollector", actual.getClass().getSimpleName()); iw.close(); tw.close(); taxonomyDirectory.close(); }
From source file:com.ideabase.dictionary.core.impl.LuceneBasedDictionaryServiceImpl.java
License:Open Source License
public DictionaryResult searchWord(final String pWord, final int pMax) { try {/*from w w w . j a v a 2 s .co m*/ final String phoneticSpell = phoneticSpell(pWord); if (mLogger.isDebugEnabled()) { mLogger.debug("Searching word - " + pWord + " : " + phoneticSpell); } final Query luceneQuery = buildLuceneQuery(phoneticSpell); if (mLogger.isDebugEnabled()) { mLogger.debug("Query - " + luceneQuery); } final Hits hits = mIndexManager.search(luceneQuery, Sort.RELEVANCE); if (mLogger.isDebugEnabled()) { mLogger.debug("HITS - " + hits); } int searchHitCount = hits.length(); final List<String> suggestedWords = new ArrayList<String>(); int count = 0; for (final Iterator iterator = hits.iterator(); iterator.hasNext();) { count++; final Hit hit = (Hit) iterator.next(); suggestedWords.add(hit.get(FIELD_BANGLA_WORD)); if (count == pMax) { break; } System.out.println("SCORE - " + hit.getScore()); } return new DictionaryResult(suggestedWords, searchHitCount); } catch (Exception e) { mLogger.warn("Failed to perform search for query - " + pWord, e); } return mEmptyDictionaryResult; }
From source file:com.ideabase.repository.core.search.impl.RepositoryItemSearchImpl.java
License:Open Source License
/** * {@inheritDoc}//from w w w. j av a 2s . com */ public PaginatedList<Hit> search(final Query pQuery) { LOG.debug("Performing search action by query - " + pQuery); try { final QueryCreator queryCreator = new RepositoryQueryCreatorImpl(pQuery); // Prepare paginated hit collector. final RepositoryHitCollectorImpl hitCollector = new RepositoryHitCollectorImpl(pQuery.getSkipRows(), pQuery.getMaxRows(), getSearcherFactory().getSearcher(), mHitTransformer); final Map<String, Boolean> sortableFields = pQuery.getSortableFields(); final LuceneSearcher searcher = getSearcherFactory().getSearcher(); if (sortableFields.isEmpty()) { populateHitCollector(hitCollector, searcher.search(pQuery.buildQuery(), Sort.RELEVANCE)); } else { // determine sortable field and type // by default sort is based on relevance applySortingFilter(pQuery, hitCollector, searcher); } return hitCollector.getPaginatedList(); } catch (Throwable e) { throw new ServiceException(pQuery, "Failed to perform search.", e); } }
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; break; } else if (SORT_INDEXORDERED.equalsIgnoreCase(key)) { LOG.debug("Applying Index ordered sorting."); sort = Sort.INDEXORDER;/*from w ww . j av a2 s . c om*/ 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.leavesfly.lia.advsearching.SortingExample.java
License:Apache License
public static void main(String[] args) throws Exception { Query allBooks = new MatchAllDocsQuery(); QueryParser parser = new QueryParser(Version.LUCENE_30, // #1 "contents", // #1 new StandardAnalyzer( // #1 Version.LUCENE_30)); // #1 BooleanQuery query = new BooleanQuery(); // #1 query.add(allBooks, BooleanClause.Occur.SHOULD); // #1 query.add(parser.parse("java OR action"), BooleanClause.Occur.SHOULD); // #1 Directory directory = TestUtil.getBookIndexDirectory(); // #2 SortingExample example = new SortingExample(directory); // #2 example.displayResults(query, Sort.RELEVANCE); example.displayResults(query, Sort.INDEXORDER); example.displayResults(query, new Sort(new SortField("category", SortField.STRING))); example.displayResults(query, new Sort(new SortField("pubmonth", SortField.INT, true))); example.displayResults(query, new Sort(new SortField("category", SortField.STRING), SortField.FIELD_SCORE, new SortField("pubmonth", SortField.INT, true))); example.displayResults(query,/*www .j a va 2s .co m*/ new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("category", SortField.STRING) })); directory.close(); }
From source file:com.mathworks.xzheng.advsearching.SortingExample.java
License:Apache License
public static void main(String[] args) throws Exception { Query allBooks = new MatchAllDocsQuery(); QueryParser parser = new QueryParser(Version.LUCENE_46, // #1 "contents", // #1 new StandardAnalyzer( // #1 Version.LUCENE_46)); // #1 BooleanQuery query = new BooleanQuery(); // #1 query.add(allBooks, BooleanClause.Occur.SHOULD); // #1 query.add(parser.parse("java OR action"), BooleanClause.Occur.SHOULD); // #1 Directory directory = TestUtil.getBookIndexDirectory(); // #2 SortingExample example = new SortingExample(directory); // #2 example.displayResults(query, Sort.RELEVANCE); example.displayResults(query, Sort.INDEXORDER); example.displayResults(query, new Sort(new SortField("category", SortField.STRING))); example.displayResults(query, new Sort(new SortField("pubmonth", SortField.INT, true))); example.displayResults(query, new Sort(new SortField("category", SortField.STRING), SortField.FIELD_SCORE, new SortField("pubmonth", SortField.INT, true))); example.displayResults(query,/* w ww . ja v a 2 s. c o m*/ new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("category", SortField.STRING) })); directory.close(); }
From source file:com.searchbox.solr.CategoryLikeThis.java
License:Apache License
@Override public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { numRequests++;/* ww w. j ava 2 s. co m*/ long startTime = System.currentTimeMillis(); if (!keystate) { LOGGER.error( "License key failure, not performing clt query. Please email contact@searchbox.com for more information."); return; } try { SolrParams params = req.getParams(); String senseField = params.get(SenseParams.SENSE_FIELD, SenseParams.DEFAULT_SENSE_FIELD); BooleanQuery catfilter = new BooleanQuery(); // Set field flags ReturnFields returnFields = new SolrReturnFields(req); rsp.setReturnFields(returnFields); int flags = 0; if (returnFields.wantsScore()) { flags |= SolrIndexSearcher.GET_SCORES; } String defType = params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE); String q = params.get(CommonParams.Q); Query query = null; SortSpec sortSpec = null; List<Query> filters = new LinkedList<Query>(); List<RealTermFreqVector> prototypetfs = new LinkedList<RealTermFreqVector>(); try { if (q != null) { QParser parser = QParser.getParser(q, defType, req); query = parser.getQuery(); sortSpec = parser.getSort(true); } String[] fqs = req.getParams().getParams(CommonParams.FQ); if (fqs != null && fqs.length != 0) { for (String fq : fqs) { if (fq != null && fq.trim().length() != 0) { QParser fqp = QParser.getParser(fq, null, req); filters.add(fqp.getQuery()); } } } } catch (Exception e) { numErrors++; throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e); } SolrIndexSearcher searcher = req.getSearcher(); DocListAndSet cltDocs = null; // Parse Required Params // This will either have a single Reader or valid query Reader reader = null; try { if (q == null || q.trim().length() < 1) { Iterable<ContentStream> streams = req.getContentStreams(); if (streams != null) { Iterator<ContentStream> iter = streams.iterator(); if (iter.hasNext()) { reader = iter.next().getReader(); } if (iter.hasNext()) { numErrors++; throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "SenseLikeThis does not support multiple ContentStreams"); } } } int start = params.getInt(CommonParams.START, 0); int rows = params.getInt(CommonParams.ROWS, 10); // Find documents SenseLikeThis - either with a reader or a query // -------------------------------------------------------------------------------- if (reader != null) { numErrors++; throw new RuntimeException("SLT based on a reader is not yet implemented"); } else if (q != null) { LOGGER.debug("Query for category:\t" + query); DocList match = searcher.getDocList(query, null, null, 0, 10, flags); // get first 10 if (match.size() == 0) { // no docs to make prototype! LOGGER.info("No documents found for prototype!"); rsp.add("response", new DocListAndSet()); return; } HashMap<String, Float> overallFreqMap = new HashMap<String, Float>(); // Create the TF of blah blah blah DocIterator iterator = match.iterator(); while (iterator.hasNext()) { // do a MoreLikeThis query for each document in results int id = iterator.nextDoc(); LOGGER.trace("Working on doc:\t" + id); RealTermFreqVector rtv = new RealTermFreqVector(id, searcher.getIndexReader(), senseField); for (int zz = 0; zz < rtv.getSize(); zz++) { Float prev = overallFreqMap.get(rtv.getTerms()[zz]); if (prev == null) { prev = 0f; } overallFreqMap.put(rtv.getTerms()[zz], rtv.getFreqs()[zz] + prev); } prototypetfs.add(rtv); } List<String> sortedKeys = Ordering.natural().onResultOf(Functions.forMap(overallFreqMap)) .immutableSortedCopy(overallFreqMap.keySet()); int keyiter = Math.min(sortedKeys.size() - 1, BooleanQuery.getMaxClauseCount() - 1); LOGGER.debug("I have this many terms:\t" + sortedKeys.size()); LOGGER.debug("And i'm going to use this many:\t" + keyiter); for (; keyiter >= 0; keyiter--) { TermQuery tq = new TermQuery(new Term(senseField, sortedKeys.get(keyiter))); catfilter.add(tq, BooleanClause.Occur.SHOULD); } } else { numErrors++; throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "CategoryLikeThis requires either a query (?q=) or text to find similar documents."); } LOGGER.debug("document filter is: \t" + catfilter); CategorizationBase model = new CategorizationBase(prototypetfs); CategoryQuery clt = CategoryQuery.CategoryQueryForDocument(catfilter, model, searcher.getIndexReader(), senseField); DocSet filtered = searcher.getDocSet(filters); cltDocs = searcher.getDocListAndSet(clt, filtered, Sort.RELEVANCE, start, rows, flags); } finally { if (reader != null) { reader.close(); } } if (cltDocs == null) { numEmpty++; cltDocs = new DocListAndSet(); // avoid NPE } rsp.add("response", cltDocs.docList); // maybe facet the results if (params.getBool(FacetParams.FACET, false)) { if (cltDocs.docSet == null) { rsp.add("facet_counts", null); } else { SimpleFacets f = new SimpleFacets(req, cltDocs.docSet, params); rsp.add("facet_counts", f.getFacetCounts()); } } } catch (Exception e) { numErrors++; } finally { totalTime += System.currentTimeMillis() - startTime; } }