List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:de.uni_koeln.spinfo.maalr.lucene.core.Dictionary.java
License:Apache License
public QueryResult getAllStartingWith(String language, String prefix, int page) throws NoIndexAvailableException, BrokenIndexException, InvalidQueryException { String field = null;//from w w w . j a va 2 s . c om String sortField = null; List<Query> queries = null; boolean firstLanguage = language.equals(description.getLanguageName(true)); field = description.getDictField(firstLanguage); if (firstLanguage) { queries = langAIndexBuilder.transform(prefix); sortField = langAIndexBuilder.getIndexSortField(); } else { queries = langBIndexBuilder.transform(prefix); sortField = langBIndexBuilder.getIndexSortField(); } int pageSize = 120; try { BooleanQuery query = new BooleanQuery(true); for (Query q : queries) { query.add(q, Occur.SHOULD); } BooleanQuery bc = new BooleanQuery(); bc.add(query, Occur.MUST); bc.add(new TermQuery(new Term(LemmaVersion.VERIFICATION, Verification.ACCEPTED.toString())), Occur.MUST); query = bc; TopDocs docs = indexProvider.getSearcher().search(query, new DuplicateFilter(field), Integer.MAX_VALUE, new Sort(new SortField(sortField, SortField.Type.STRING))); return toQueryResult(docs, page * pageSize, pageSize); } catch (IOException e) { throw new BrokenIndexException("Broken index!", e); } catch (InvalidTokenOffsetsException e) { throw new InvalidQueryException("Highlighting failed", e); } }
From source file:dk.dbc.opensearch.fedora.search.LuceneFieldIndex.java
License:Open Source License
public int findHighestId(String namespace) throws IOException { TermQuery luceneQuery = new TermQuery(new Term(PID_NAMESPACE, namespace)); searchManager.maybeRefreshBlocking(); IndexSearcher localSearcher = searchManager.acquire(); try {// ww w. j a va 2 s.c o m log.debug("Query: {}", luceneQuery.toString()); TopFieldDocs search = localSearcher.search(luceneQuery, 1, new Sort(new SortField(PID_INT, SortField.Type.INT, true))); if (search.scoreDocs.length > 0) { IndexReader localReader = localSearcher.getIndexReader(); Document document = localReader.document(search.scoreDocs[0].doc); IndexableField identifer = document.getField(PID_INT); if (identifer != null) { return identifer.numericValue().intValue(); } } return 0; } finally { searchManager.release(localSearcher); } }
From source file:dk.defxws.fgslucene.Statement.java
License:Open Source License
private TopDocs getHits(Query query, int numHits, String sortFields) throws GenericSearchException { if (logger.isDebugEnabled()) logger.debug("getHits" + " query=" + query + " numHits=" + numHits + " sortFields=" + sortFields); TopDocs hits = null;//w w w . ja v a 2 s.c o m IndexReader ireader = searcher.getIndexReader(); Collection<String> fieldNames = ReaderUtil.getIndexedFields(ireader); String sortFieldsString = sortFields; if (sortFields == null) sortFieldsString = ""; StringTokenizer st = new StringTokenizer(sortFieldsString, ";"); SortField[] sortFieldArray = new SortField[st.countTokens()]; int i = 0; while (st.hasMoreTokens()) { SortField sortField = null; int sortType = -1; String sortFieldString = st.nextToken().trim(); if (sortFieldString.length() == 0) errorExit("getHits sortFields='" + sortFields + "' : empty sortField string"); StringTokenizer stf = new StringTokenizer(sortFieldString, ","); if (!stf.hasMoreTokens()) errorExit("getHits sortFields='" + sortFields + "' : empty sortFieldName string in '" + sortFieldString + "'"); String sortFieldName = stf.nextToken().trim(); if (sortFieldName.length() == 0) errorExit("getHits sortFields='" + sortFields + "' : empty sortFieldName string in '" + sortFieldString + "'"); if (!fieldNames.contains(sortFieldName)) errorExit("getHits sortFields='" + sortFields + "' : sortFieldName '" + sortFieldName + "' not found as index field name"); if (!stf.hasMoreTokens()) { sortType = SortField.SCORE; sortField = new SortField(sortFieldName, sortType); } else { String sortTypeOrLocaleOrCompString = stf.nextToken().trim(); if (sortTypeOrLocaleOrCompString.length() == 0) errorExit("getHits sortFields='" + sortFields + "' : empty sortType or locale or comparatorClass string in '" + sortFieldString + "'"); if (sortTypeOrLocaleOrCompString.indexOf(".") >= 0) { String compString = sortTypeOrLocaleOrCompString; String paramString = ""; Object[] params = new Object[] {}; if (sortTypeOrLocaleOrCompString.indexOf("(") >= 0) { int p = compString.indexOf("("); int q = compString.indexOf(")"); if (p < 3 || q < p + 1) errorExit("getHits sortFields='" + sortFields + "' : comparatorClass parameters malformed in '" + compString + "'."); paramString = compString.substring(p + 1, q); compString = compString.substring(0, p); StringTokenizer stp = new StringTokenizer(paramString, "-"); params = new Object[stp.countTokens()]; int ip = 0; while (stp.hasMoreTokens()) { params[ip++] = stp.nextToken().trim(); } } FieldComparatorSource scs = null; Class comparatorClass = null; try { comparatorClass = Class.forName(compString); } catch (ClassNotFoundException e) { errorExit("getHits sortFields='" + sortFields + "' : comparatorClass '" + compString + "'" + ": class not found:\n" + e.toString()); } Constructor[] constructors = comparatorClass.getConstructors(); StringBuffer errorMessage = new StringBuffer(); for (int j = 0; j < constructors.length; j++) { Constructor cj = constructors[j]; try { scs = (FieldComparatorSource) cj.newInstance(params); if (logger.isDebugEnabled()) logger.debug( "getHits sortFields='" + sortFields + "' : comparatorClass '" + compString + "'" + ": constructor[" + j + "]='" + cj.toGenericString() + "'"); break; } catch (IllegalArgumentException e) { errorMessage.append("\nconstructor[" + j + "]='" + cj.toGenericString() + "'" + "\n" + e.toString() + " "); } catch (InstantiationException e) { errorMessage.append("\nconstructor[" + j + "]='" + cj.toGenericString() + "'" + "\n" + e.toString() + " "); } catch (IllegalAccessException e) { errorMessage.append("\nconstructor[" + j + "]='" + cj.toGenericString() + "'" + "\n" + e.toString() + " "); } catch (InvocationTargetException e) { errorMessage.append("\nconstructor[" + j + "]='" + cj.toGenericString() + "'" + "\n" + e.toString() + " "); } } if (scs == null) { errorExit("getHits sortFields='" + sortFields + "' : comparatorClass '" + compString + "'" + ": no constructor applied:\n" + errorMessage.toString()); } if (!stf.hasMoreTokens()) { sortField = new SortField(sortFieldName, scs); } else { String reverseString = stf.nextToken().trim(); if (reverseString.length() == 0) errorExit("getHits sortFields='" + sortFields + "' : empty reverse string in '" + sortFieldString + "'"); boolean reverse = false; if ("true".equalsIgnoreCase(reverseString)) reverse = true; else if ("reverse".equalsIgnoreCase(reverseString)) reverse = true; else if ("false".equalsIgnoreCase(reverseString)) reverse = false; else errorExit("getHits sortFields='" + sortFields + "' : unknown reverse string '" + reverseString + "' in '" + sortFieldString + "'"); sortField = new SortField(sortFieldName, scs, reverse); } } else { String sortTypeOrLocaleString = sortTypeOrLocaleOrCompString; Locale locale = null; if ("BYTE".equals(sortTypeOrLocaleString)) sortType = SortField.BYTE; else if ("DOC".equals(sortTypeOrLocaleString)) sortType = SortField.DOC; else if ("DOUBLE".equals(sortTypeOrLocaleString)) sortType = SortField.DOUBLE; else if ("FLOAT".equals(sortTypeOrLocaleString)) sortType = SortField.FLOAT; else if ("INT".equals(sortTypeOrLocaleString)) sortType = SortField.INT; else if ("LONG".equals(sortTypeOrLocaleString)) sortType = SortField.LONG; else if ("SCORE".equals(sortTypeOrLocaleString)) { sortType = SortField.SCORE; searcher.setDefaultFieldSortScoring(true, true); } else if ("SHORT".equals(sortTypeOrLocaleString)) sortType = SortField.SHORT; else if ("STRING".equals(sortTypeOrLocaleString)) sortType = SortField.STRING; else if ("STRING_VAL".equals(sortTypeOrLocaleString)) sortType = SortField.STRING_VAL; else if (((sortTypeOrLocaleString.substring(0, 1)).compareTo("A") >= 0) && ((sortTypeOrLocaleString.substring(0, 1)).compareTo("Z") <= 0)) { errorExit("getHits sortFields='" + sortFields + "' : unknown sortType string '" + sortTypeOrLocaleString + "' in '" + sortFieldString + "'"); } else { StringTokenizer stfl = new StringTokenizer(sortTypeOrLocaleString, "-"); if (stfl.countTokens() > 3) errorExit("getHits sortFields='" + sortFields + "' : unknown locale string '" + sortTypeOrLocaleString + "' in '" + sortFieldString + "'"); String language = stfl.nextToken().trim(); if (language.length() == 0) errorExit("getHits sortFields='" + sortFields + "' : empty language string in '" + sortFieldString + "'"); if (language.length() > 2) errorExit("getHits sortFields='" + sortFields + "' : unknown language string '" + language + "' in '" + sortFieldString + "'"); if (!stfl.hasMoreTokens()) { locale = new Locale(language); } else { String country = stfl.nextToken().trim(); if (country.length() == 0) errorExit("getHits sortFields='" + sortFields + "' : empty country string in '" + sortFieldString + "'"); if (country.length() > 3) errorExit("getHits sortFields='" + sortFields + "' : unknown country string '" + country + "' in '" + sortFieldString + "'"); if (!stfl.hasMoreTokens()) { locale = new Locale(language, country); } else { String variant = stfl.nextToken().trim(); if (variant.length() == 0) errorExit("getHits sortFields='" + sortFields + "' : empty variant string in '" + sortFieldString + "'"); locale = new Locale(language, country, variant); } } } if (!stf.hasMoreTokens()) { if (sortType >= 0) sortField = new SortField(sortFieldName, sortType); else sortField = new SortField(sortFieldName, locale); } else { String reverseString = stf.nextToken().trim(); if (reverseString.length() == 0) errorExit("getHits sortFields='" + sortFields + "' : empty reverse string in '" + sortFieldString + "'"); boolean reverse = false; if ("true".equalsIgnoreCase(reverseString)) reverse = true; else if ("reverse".equalsIgnoreCase(reverseString)) reverse = true; else if ("false".equalsIgnoreCase(reverseString)) reverse = false; else throw new GenericSearchException( "getHits sortFields='" + sortFields + "' : unknown reverse string '" + reverseString + "' in '" + sortFieldString + "'"); if (sortType == SortField.SCORE) reverse = !reverse; if (sortType >= 0) sortField = new SortField(sortFieldName, sortType, reverse); else sortField = new SortField(sortFieldName, locale, reverse); } } } sortFieldArray[i++] = sortField; } if (sortFieldArray.length == 0) { try { hits = searcher.search(query, numHits); } catch (Exception e) { errorExit("getHits search : " + e.toString()); } } else { try { Sort sort = new Sort(sortFieldArray); hits = searcher.search(query, numHits, sort); } catch (Exception e) { errorExit("getHits search sortFields='" + sortFields + "' : " + e.toString()); } } return hits; }
From source file:dk.dma.msinm.lucene.SpatialLuceneTest.java
License:Open Source License
@Test public void testSpatialSearch() throws IOException, ParseException { int maxLevels = 11;//results in sub-meter precision for geohash SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels); strategy = new RecursivePrefixTreeStrategy(grid, "myGeoField"); Directory directory = new RAMDirectory(); IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_47, null); IndexWriter indexWriter = new IndexWriter(directory, iwConfig); indexWriter.addDocument(newSampleDocument(2, ctx.makePoint(-80.93, 33.77))); indexWriter.addDocument(newSampleDocument(4, ctx.readShapeFromWkt("POINT(60.9289094 -50.7693246)"))); indexWriter.addDocument(newSampleDocument(20, ctx.makePoint(0.1, 0.1), ctx.makePoint(0, 0))); indexWriter.addDocument(newSampleDocument(30, JtsSpatialContext.GEO.readShapeFromWkt("POLYGON((0 0, -90 0, -90 40, 0 40, 0 0))"))); indexWriter.close();/*from ww w. j a v a 2 s . com*/ IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); Sort idSort = new Sort(new SortField("id", SortField.Type.INT)); // Search 1 SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(200, DistanceUtils.EARTH_MEAN_RADIUS_KM))); TopDocs docs = indexSearcher.search(new MatchAllDocsQuery(), strategy.makeFilter(args), 10, idSort); assertDocMatchedIds(indexSearcher, docs, 2, 30); // Search 2 args = new SpatialArgs(SpatialOperation.Intersects, JtsSpatialContext.GEO.readShapeFromWkt("POLYGON((-10 10, -20 0, -20 20, -10 20, -10 10))")); docs = indexSearcher.search(new MatchAllDocsQuery(), strategy.makeFilter(args), 10, idSort); assertDocMatchedIds(indexSearcher, docs, 30); }
From source file:edu.usc.ir.geo.gazetteer.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 IndexSearcher searcher = new IndexSearcher(reader); Query q = null;// w w w . j a va 2s . c o m 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 descending on population SortField populationSort = new SortedNumericSortField(FIELD_NAME_POPULATION, SortField.Type.LONG, true); 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; List<Location> topHits = new ArrayList<Location>(); for (int i = 0; i < hits.length; ++i) { Location tmpLocObj = new Location(); int docId = hits[i].doc; Document d; try { d = searcher.doc(docId); tmpLocObj.setName(d.get(FIELD_NAME_NAME)); tmpLocObj.setLongitude(d.get(FIELD_NAME_LONGITUDE)); tmpLocObj.setLatitude(d.get(FIELD_NAME_LATITUDE)); //If alternate names are empty put name as actual name //This covers missing data and equals weight for later computation if (d.get(FIELD_NAME_ALTERNATE_NAMES).isEmpty()) { tmpLocObj.setAlternateNames(d.get(FIELD_NAME_NAME)); } else { tmpLocObj.setAlternateNames(d.get(FIELD_NAME_ALTERNATE_NAMES)); } tmpLocObj.setCountryCode(d.get(FIELD_NAME_COUNTRY_CODE)); tmpLocObj.setAdmin1Code(d.get(FIELD_NAME_ADMIN1_CODE)); tmpLocObj.setAdmin2Code(d.get(FIELD_NAME_ADMIN2_CODE)); tmpLocObj.setFeatureCode(d.get(FIELD_NAME_FEATURE_CODE)); } catch (IOException e) { e.printStackTrace(); } topHits.add(tmpLocObj); } //Picking hitsPerPage number of locations from feature code sorted list allCandidates.put(name, pickTopSortedByCode(topHits, hitsPerPage)); } 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:es.pode.indexador.negocio.servicios.busqueda.SrvBuscadorServiceImpl.java
License:Open Source License
private Sort chooseSorter(es.pode.indexador.negocio.servicios.busqueda.ParamAvanzadoVO paramBusq) throws Exception { Sort result = null;/*from w w w .j a v a 2 s . c o m*/ if (paramBusq.getSortingMethod() == null || paramBusq.getSortingMethod().trim().toUpperCase().equals("RELEVANCE")) { result = Sort.RELEVANCE; } else if (paramBusq.getSortingMethod() == null || paramBusq.getSortingMethod().trim().toUpperCase().equals("FORMAT")) { result = new Sort(new SortField("formato", new MimeTypeSortComparatorSource())); } else { StringTokenizer strTnk = new StringTokenizer(paramBusq.getSortingMethod(), ","); int numTokens = strTnk.countTokens(); if (numTokens == 1) { result = new Sort(new SortField(props.getProperty(strTnk.nextToken()), new AlphabeticalSortComparatorSource())); } else if (numTokens == 2) { result = result = new Sort( new SortField(props.getProperty(strTnk.nextToken()), new AlphabeticalSortComparatorSource(), strTnk.nextToken().equalsIgnoreCase("n") ? false : true)); } else throw new Exception("Sorting paramerters bad formed. Please review spring_buscador.properties"); } return result; }
From source file:fr.openwide.core.jpa.more.business.generic.dao.GenericListItemDaoImpl.java
License:Apache License
@Deprecated protected final <E extends GenericListItem<?>> List<E> searchAutocomplete(String searchPattern, Class<E> clazz, String[] fields, Integer limit, Integer offset) throws ServiceException { GenericListItemBinding<E> binding = new GenericListItemBinding<E>(); String labelBindingPath = binding.label().getPath(); QueryBuilder queryBuilder = Search.getFullTextEntityManager(getEntityManager()).getSearchFactory() .buildQueryBuilder().forEntity(clazz).get(); Query luceneQuery = queryBuilder.keyword().onField(binding.enabled().getPath()).matching(true) .createQuery();/*from www .j a va2s.c o m*/ String[] actualFields = fields; if (actualFields == null || actualFields.length == 0) { actualFields = new String[] { labelBindingPath, binding.code().getPath() }; } return hibernateSearchService.search(clazz, actualFields, LuceneUtils.getAutocompleteQuery(searchPattern), luceneQuery, limit, offset, new Sort(new SortField(GenericListItem.LABEL_SORT_FIELD_NAME, SortField.Type.STRING))); }
From source file:framework.retrieval.engine.query.item.QuerySort.java
License:Apache License
/** * ?Lucene?//from w w w.j av a 2s . c o m * @return */ public Sort getSort() { if (sort != null) { return sort; } else { return new Sort(new SortField(fieldName, sortFieldType, ascFlag)); } }
From source file:framework.retrieval.engine.query.item.QueryUtil.java
License:Apache License
/** * ??/*from w ww.j a va 2 s. co m*/ * * @param orderByFlag * @return */ public static Sort createCreateTimeSort(boolean orderByFlag) { Sort sort = new Sort(new SortField[] { new SortField(null, SortField.DOC, orderByFlag) }); return sort; }
From source file:framework.retrieval.engine.query.item.QueryUtil.java
License:Apache License
/** * ??/* w w w.ja va 2 s .c om*/ * * @return */ public static Sort createScoreSort() { Sort sort = new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField(null, SortField.DOC, true) }); return sort; }