List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:axiom.scripting.rhino.LuceneQueryDispatcher.java
License:Open Source License
private Sort getLuceneSort(SortObject sort) { if (sort == null) { return null; }// w w w . ja va 2s . com QuerySortField[] fields = sort.getSortFields(); if (fields == null) { return null; } final int length = fields.length; SortField[] sortFields = new SortField[length]; for (int i = 0; i < length; i++) { String s = fields[i].getField(); if (s == null) { return null; } sortFields[i] = new SortField(s, SortField.STRING, !fields[i].isAscending()); } return new Sort(sortFields); }
From source file:br.ufba.dcc.mestrado.computacao.repository.impl.ProjectRepositoryImpl.java
private void configureRelevanceSort(FullTextQuery fullTextQuery) { boolean reverse = true; SortField userContSortField = new SortField(SearchFieldsEnum.projectUserCount.fieldName(), SortField.Type.LONG, reverse); SortField ratingContSortField = new SortField(SearchFieldsEnum.projectRatingCount.fieldName(), SortField.Type.LONG, reverse); SortField reviewContSortField = new SortField(SearchFieldsEnum.projectReviewCount.fieldName(), SortField.Type.LONG, reverse); List<SortField> sortFieldList = new LinkedList<>(); sortFieldList.addAll(Arrays.asList(Sort.RELEVANCE.getSort())); sortFieldList.add(userContSortField); sortFieldList.add(reviewContSortField); sortFieldList.add(ratingContSortField); Sort sort = new Sort(userContSortField); fullTextQuery.setSort(sort);/*from w w w .j a v a 2 s . c om*/ }
From source file:bzh.terrevirtuelle.navisu.gazetteer.impl.lucene.GeoNameResolver.java
License:Apache License
/** * Returns a list of location near a certain coordinate. * * @param latitude, @param longitude - Center of search area * @param distanceInMiles - Search Radius in miles * @param indexerPath - Path to Lucene index * @param count - Upper bound to number of results * @return - List of locations sorted by population * @throws IOException/*from w w w . j av a2 s.co m*/ */ public List<Location> searchNearby(Double latitude, Double longitude, Double distanceInMiles, String indexerPath, int count) throws IOException { double distanceInDeg = DistanceUtils.dist2Degrees(distanceInMiles, DistanceUtils.EARTH_EQUATORIAL_RADIUS_MI); SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.IsWithin, ctx.makeCircle(longitude, latitude, distanceInDeg)); String key = latitude + "-" + longitude; Filter filter = strategy.makeFilter(spatialArgs); IndexSearcher searcher = new IndexSearcher(createIndexReader(indexerPath)); Sort sort = new Sort(populationSort); TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), filter, count, sort); ScoreDoc[] scoreDocs = topDocs.scoreDocs; HashMap<String, List<Location>> allCandidates = new HashMap<String, List<Location>>(); getMatchingCandidates(searcher, allCandidates, key, scoreDocs); List<Location> results = allCandidates.get(key); return results; }
From source file:bzh.terrevirtuelle.navisu.gazetteer.impl.lucene.GeoNameResolver.java
License:Apache License
private HashMap<String, List<Location>> resolveEntities(List<String> locationNames, int count, IndexReader reader) throws IOException { if (locationNames.size() >= 200) { hitsPerPage = 5; // avoid heavy computation }/*from w ww . j a v a2 s . c o m*/ IndexSearcher searcher = new IndexSearcher(reader); Query q = null; HashMap<String, List<Location>> allCandidates = new HashMap<String, List<Location>>(); for (String name : locationNames) { if (!allCandidates.containsKey(name)) { try { //query is wrapped in additional quotes (") to avoid query tokenization on space q = new MultiFieldQueryParser(new String[] { FIELD_NAME_NAME, FIELD_NAME_ALTERNATE_NAMES }, analyzer).parse(String.format("\"%s\"", name)); Sort sort = new Sort(populationSort); //Fetch 3 times desired values, these will be sorted on code and only desired number will be kept ScoreDoc[] hits = searcher.search(q, hitsPerPage * 3, sort).scoreDocs; getMatchingCandidates(searcher, allCandidates, name, hits); } catch (org.apache.lucene.queryparser.classic.ParseException e) { e.printStackTrace(); } } } HashMap<String, List<Location>> resolvedEntities = new HashMap<String, List<Location>>(); pickBestCandidates(resolvedEntities, allCandidates, count); return resolvedEntities; }
From source file:ccc.plugins.search.lucene.SimpleLuceneFS.java
License:Open Source License
/** {@inheritDoc} */ @Override/*from ww w . java 2 s . c om*/ public SearchResult find(final String searchTerms, final String sort, final SortOrder order, final int nofOfResultsPerPage, final int pageNo) { final int page = pageNo - 1; final Sort sorter = (null == sort) ? null : new Sort(new SortField(sort, SortField.STRING_VAL, (SortOrder.DESC == order))); if (searchTerms == null || searchTerms.trim().equals("")) { return new SearchResult(new HashSet<UUID>(), 0, nofOfResultsPerPage, searchTerms, page); } final int maxHits = (page + 1) * nofOfResultsPerPage; final CapturingHandler capturingHandler = new CapturingHandler(nofOfResultsPerPage, page); find(searchTerms, maxHits, sorter, null, capturingHandler); return new SearchResult(capturingHandler.getHits(), capturingHandler.getTotalResultsCount(), nofOfResultsPerPage, searchTerms, page); }
From source file:ccc.plugins.search.lucene.SimpleLuceneFS.java
License:Open Source License
/** {@inheritDoc} */ @Override//from www .ja va2s . co m public SearchResult find(final String searchTerms, final String sort, final SortOrder order, final ACL userPerms, final int nofOfResultsPerPage, final int pageNo) { final int page = pageNo - 1; final Sort sorter = (null == sort) ? null : new Sort(new SortField(sort, SortField.STRING_VAL, (SortOrder.DESC == order))); if (searchTerms == null || searchTerms.trim().equals("")) { return new SearchResult(new HashSet<UUID>(), 0, nofOfResultsPerPage, searchTerms, page); } final int maxHits = (page + 1) * nofOfResultsPerPage; final CapturingHandler capturingHandler = new CapturingHandler(nofOfResultsPerPage, page); find(searchTerms, maxHits, sorter, userPerms, capturingHandler); return new SearchResult(capturingHandler.getHits(), capturingHandler.getTotalResultsCount(), nofOfResultsPerPage, searchTerms, page); }
From source file:com.aistor.modules.cms.service.ArticleService.java
License:Open Source License
/** * // w ww . j a v a2 s . co m */ public Page<Article> search(Page<Article> page, String q) { // ? BooleanQuery query = articleDao.getFullTextQuery(q, "title", "keywords", "desciption", "articleData.content"); // ? BooleanQuery queryFilter = articleDao.getFullTextQuery( new BooleanClause(new TermQuery(new Term("status", Article.STATUS_RELEASE)), Occur.MUST)); // ? Sort sort = new Sort(new SortField("updateDate", SortField.DOC, true)); // articleDao.search(page, query, queryFilter, sort); // articleDao.keywordsHighlight(query, page.getList(), "desciption", "articleData.content"); return page; }
From source file:com.aliasi.lingmed.medline.IndexMedline.java
License:Lingpipe license
private String getLastUpdate(File index) throws IOException { System.out.println("Got index" + index); IndexReader reader = null;//from ww w . ja va2s.com IndexSearcher searcher = null; try { if (isNewDirectory(mIndex)) return LOW_SORT_STRING; Directory fsDir = FSDirectory.open(mIndex); reader = IndexReader.open(fsDir); searcher = new IndexSearcher(reader); Term term = new Term(Fields.MEDLINE_DIST_FIELD, Fields.MEDLINE_DIST_VALUE); SortField sortField = new SortField(Fields.MEDLINE_FILE_FIELD, SortField.STRING); Sort sort = new Sort(sortField); Query query = new TermQuery(term); TopFieldDocs results = searcher.search(query, null, 1, sort); if (results.totalHits == 0) { searcher.close(); reader.close(); return LOW_SORT_STRING; } // if (mLogger.isDebugEnabled()) // mLogger.debug("num MEDLINE_FILE docs: " + results.totalHits); Document d = searcher.doc(results.scoreDocs[0].doc); return d.get(Fields.MEDLINE_FILE_FIELD); } finally { if (searcher != null) searcher.close(); if (reader != null) reader.close(); } }
From source file:com.amalto.core.storage.hibernate.FullTextQueryHandler.java
License:Open Source License
@Override public StorageResults visit(OrderBy orderBy) { TypedExpression field = orderBy.getExpression(); if (field instanceof Field) { FieldMetadata fieldMetadata = ((Field) field).getFieldMetadata(); SortField sortField = new SortField(fieldMetadata.getName(), getSortType(fieldMetadata), orderBy.getDirection() == OrderBy.Direction.DESC); query.setSort(new Sort(sortField)); return null; } else {/*from ww w . java2 s .c o m*/ throw new NotImplementedException("No support for order by for full text search on non-field."); } }
From source file:com.b2international.index.lucene.IndexFieldBase.java
License:Apache License
@Override public final Sort createSort() { return new Sort(new SortField(fieldName(), getSortFieldType())); }