List of usage examples for org.apache.lucene.search IndexSearcher search
protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException
From source file:ci6226.facetsearch.java
public static void main(String[] args) throws Exception { String index = "./myindex"; String field = "text"; String queries = null;//from w w w .j a va2 s. c o m int hitsPerPage = 10; boolean raw = false; //http://lucene.apache.org/core/4_0_0/facet/org/apache/lucene/facet/doc-files/userguide.html#facet_accumulation IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher = new IndexSearcher(reader); // :Post-Release-Update-Version.LUCENE_XY: //TODO: SAME AS HOW U BUILD INDEX Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47); BufferedReader in = null; if (queries != null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); } // :Post-Release-Update-Version.LUCENE_XY: QueryParser parser = new QueryParser(Version.LUCENE_47, field, analyzer); while (true) { System.out.println("Enter query: "); String line = in.readLine(); line = line.trim(); if (line.length() == 0) { break; } Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); Date start = new Date(); searcher.search(query, null, 100); Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms"); TopDocs results = searcher.search(query, 5 * hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = results.totalHits; //N= max docs //df = totoal matched doc //idf=log(N/df) for (int i = 0; i < hits.length; i++) { Document doc = searcher.doc(hits[i].doc); System.out.println(ANSI_BLUE + (i + 1) + ANSI_RESET + "\nScore=\t" + hits[i].score); String rtext = doc.get(field); System.out.println("Text=\t" + rtext); Terms vector = reader.getTermVector(i, "text"); if (vector == null) continue; // System.out.println(vector.getSumDocFreq()); // Terms vector = reader.getTermVector(hits[i].doc, field); //hits[i].doc=docID TermsEnum termsEnum = vector.iterator(null); termsEnum = vector.iterator(termsEnum); Map<String, Integer> frequencies = new HashMap<>(); BytesRef text = null; while ((text = termsEnum.next()) != null) { String term = text.utf8ToString(); int freq = (int) termsEnum.totalTermFreq(); frequencies.put(term, freq); // System.out.println("Time: "+term + " idef "+freq); } } // String[] facetCatlog={""}; System.out.println(numTotalHits + " total matching documents"); } reader.close(); }
From source file:ci6226.loadIndex.java
public static void main(String[] args) throws Exception { String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details."; if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) { System.out.println(usage); System.exit(0);/*from w ww.j av a 2s . com*/ } String index = "./myindex"; String field = "text"; String queries = null; int hitsPerPage = 10; boolean raw = false; IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher = new IndexSearcher(reader); // :Post-Release-Update-Version.LUCENE_XY: //TODO: SAME AS HOW U BUILD INDEX Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47); BufferedReader in = null; if (queries != null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); } // :Post-Release-Update-Version.LUCENE_XY: QueryParser parser = new QueryParser(Version.LUCENE_47, field, analyzer); while (true) { System.out.println("Enter query: "); String line = in.readLine(); line = line.trim(); if (line.length() == 0) { break; } Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); Date start = new Date(); searcher.search(query, null, 100); Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms"); doPagingSearch(in, searcher, query, hitsPerPage, raw, true, analyzer); } reader.close(); }
From source file:cn.hbu.cs.esearch.service.impl.EsearchSearchServiceImpl.java
License:Apache License
@Override public SearchResult search(SearchRequest sResquest) throws EsearchException { try {/* www .ja v a 2 s. c o m*/ esearchSystem.flushEvents(2000); } catch (EsearchException e) { LOGGER.error("Esearch flush events error. \n{}", e); } String queryString = sResquest.getQuery(); String queryField = sResquest.getField(); LOGGER.info("The search request coming: queryField:{},queryString:{}", queryField, queryString); Analyzer analyzer = esearchSystem.getAnalyzer(); QueryParser queryParser = new QueryParser(Version.LUCENE_43, queryField, analyzer); SearchResult result = new SearchResult(); List<EsearchMultiReader<R>> readers = null; MultiReader multiReader = null; IndexSearcher searcher = null; try { Query query = null; if (Strings.isNullOrEmpty(queryString)) { query = new MatchAllDocsQuery(); } else { query = queryParser.parse(queryString); } readers = esearchSystem.getIndexReaders(); multiReader = new MultiReader(readers.toArray(new IndexReader[readers.size()]), false); searcher = new IndexSearcher(multiReader); long start = System.currentTimeMillis(); TopDocs docs = searcher.search(query, null, sResquest.getSize()); long end = System.currentTimeMillis(); result.setTime(end - start); result.setTotalDocs(multiReader.numDocs()); result.setTotalHits(docs.totalHits); LOGGER.info("Got {} hits. Cost:{} ms", docs.totalHits, end - start); if (sResquest.getSearchType() == SearchRequest.SearchType.COUNT) { return result; } ScoreDoc[] scoreDocs = docs.scoreDocs; ArrayList<SearchHit> hitList = new ArrayList<SearchHit>(scoreDocs.length); for (ScoreDoc scoreDoc : scoreDocs) { SearchHit hit = new SearchHit(); hit.setScore(scoreDoc.score); int docID = scoreDoc.doc; Document doc = multiReader.document(docID); String content = doc.get(queryField); Scorer qs = new QueryScorer(query); SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span class=\"hl\">", "</span>"); Highlighter hl = new Highlighter(formatter, qs); String[] fragments = hl.getBestFragments(analyzer, queryField, content, 1); Map<String, String[]> fields = convert(doc, sResquest.getSearchType()); fields.put("fragment", fragments); hit.setFields(fields); hitList.add(hit); } result.setHits(hitList.toArray(new SearchHit[hitList.size()])); return result; } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new EsearchException(e.getMessage(), e); } finally { if (multiReader != null) { try { multiReader.close(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } esearchSystem.returnIndexReaders(readers); } }
From source file:com.admarketplace.isg.lucene.demo.SearchFiles.java
License:Apache License
/** Simple command-line based search demo. */ public static void main(String[] args) throws Exception { String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details."; if (args.length <= 0 || ("-h".equals(args[0]) || "-help".equals(args[0]))) { System.out.println(usage); System.exit(0);//from w w w. j a v a 2s . co m } String index = "index"; String field = "contents"; String queries = null; int repeat = 0; boolean raw = false; String queryString = null; int hitsPerPage = 10; for (int i = 0; i < args.length; i++) { if ("-index".equals(args[i])) { index = args[i + 1]; i++; } else if ("-field".equals(args[i])) { field = args[i + 1]; i++; } else if ("-queries".equals(args[i])) { queries = args[i + 1]; i++; } else if ("-query".equals(args[i])) { queryString = args[i + 1]; i++; } else if ("-repeat".equals(args[i])) { repeat = Integer.parseInt(args[i + 1]); i++; } else if ("-raw".equals(args[i])) { raw = true; } else if ("-paging".equals(args[i])) { hitsPerPage = Integer.parseInt(args[i + 1]); if (hitsPerPage <= 0) { System.err.println("There must be at least 1 hit per page."); System.exit(1); } i++; } } IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index))); IndexSearcher searcher = new IndexSearcher(reader); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40); BufferedReader in = null; if (queries != null) { in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); } QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer); while (true) { if (queries == null && queryString == null) { // prompt the user System.out.println("Enter query: "); } String line = queryString != null ? queryString : in.readLine(); if (line == null || line.length() == -1) { break; } line = line.trim(); if (line.length() == 0) { break; } Query query = parser.parse(line); System.out.println("Searching for: " + query.toString(field)); if (repeat > 0) { // repeat & time as benchmark Date start = new Date(); for (int i = 0; i < repeat; i++) { searcher.search(query, null, 100); } Date end = new Date(); System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms"); } doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null); if (queryString != null) { break; } } reader.close(); }
From source file:com.agiletec.plugins.jacms.aps.system.services.searchengine.SearcherDAO.java
License:Open Source License
/** * Ricerca una lista di identificativi di contenuto in base * al codice della lingua corrente ed alla parola immessa. * @param langCode Il codice della lingua corrente. * @param word La parola in base al quale fare la ricerca. Nel caso * venissero inserite stringhe di ricerca del tipo "Venice Amsterdam" * viene considerato come se fosse "Venice OR Amsterdam". * @param allowedGroups I gruppi autorizzati alla visualizzazione. Nel caso * che la collezione sia nulla o vuota, la ricerca sar effettuata su contenuti * referenziati con il gruppo "Ad accesso libero". Nel caso che nella collezione * sia presente il gruppo degli "Amministratori", la ricerca produrr un'insieme * di identificativi di contenuto non filtrati per gruppo. * @return La lista di identificativi contenuto. * @throws ApsSystemException//w w w.j av a 2s. c o m */ public List<String> searchContentsId(String langCode, String word, Collection<String> allowedGroups) throws ApsSystemException { List<String> contentsId = new ArrayList<String>(); IndexSearcher searcher = null; try { searcher = this.getSearcher(); QueryParser parser = new QueryParser(Version.LUCENE_30, langCode, this.getAnalyzer()); String queryString = this.createQueryString(langCode, word, allowedGroups); Query query = parser.parse(queryString); int maxSearchLength = 1000; TopDocs topDocs = searcher.search(query, null, maxSearchLength); ScoreDoc[] scoreDoc = topDocs.scoreDocs; if (scoreDoc.length > 0) { for (int index = 0; index < scoreDoc.length; index++) { ScoreDoc sDoc = scoreDoc[index]; Document doc = searcher.doc(sDoc.doc); contentsId.add(doc.get(IIndexerDAO.CONTENT_ID_FIELD_NAME)); } } } catch (IOException e) { throw new ApsSystemException("Errore in estrazione " + "documento in base ad indice", e); } catch (ParseException e) { throw new ApsSystemException("Errore parsing nella ricerca", e); } finally { this.releaseSearcher(searcher); } return contentsId; }
From source file:com.baidu.rigel.biplatform.tesseract.isservice.netty.service.SearchServerHandler.java
License:Open Source License
@Override public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception { SearchRequestMessage searchReqeustMessage = (SearchRequestMessage) msg; String idxPath = searchReqeustMessage.getIdxPath(); SearcherManager searcherManager = IndexSearcherFactory.getInstance().getSearcherManager(idxPath); IndexSearcher is = null; is = searcherManager.acquire();// w w w. j a v a 2 s .c om QueryRequest queryRequest = (QueryRequest) searchReqeustMessage.getMessageBody(); Query queryAll = QueryRequestUtil.transQueryRequest2LuceneQuery(queryRequest); List<String> measureFieldList = new ArrayList<String>(); List<String> dimFieldList = new ArrayList<String>(); if (queryRequest.getSelect().getQueryMeasures() != null) { for (QueryMeasure qm : queryRequest.getSelect().getQueryMeasures()) { measureFieldList.add(qm.getProperties()); } } if (queryRequest.getSelect().getQueryProperties() != null) { dimFieldList.addAll(queryRequest.getSelect().getQueryProperties()); } TesseractResultSet searchResult = null; LinkedList<ResultRecord> resultRecordList = new LinkedList<ResultRecord>(); long current = System.currentTimeMillis(); try { QueryWrapperFilter filter = new QueryWrapperFilter(queryAll); Set<String> groupBy = new HashSet<>(); if (queryRequest.getGroupBy() != null) { groupBy = queryRequest.getGroupBy().getGroups(); } TesseractResultRecordCollector collector = new TesseractResultRecordCollector( dimFieldList.toArray(new String[0]), measureFieldList.toArray(new String[0]), groupBy); is.search(new MatchAllDocsQuery(), filter, collector); // for (int docId : collector.getResultDocIdList()) { // Document doc = is.getIndexReader().document(docId); // ResultRecord record = new ResultRecord(doc); // // ??? // // List<ResultRecord> recordList = // // QueryRequestUtil.mapLeafValue2ValueOfRecord(record, // // leafValueMap); // // resultRecordList.add(record); // // } logger.info("cost " + (System.currentTimeMillis() - current) + " in search,result:" + collector.getResult().size()); current = System.currentTimeMillis(); resultRecordList.addAll(collector.getResult()); } finally { searcherManager.release(is); } // process result // group by if (queryRequest.getGroupBy() != null) { int dimSize = queryRequest.getSelect().getQueryProperties().size(); searchResult = new SearchResultSet(AggregateCompute.aggregate(resultRecordList, dimSize, queryRequest.getSelect().getQueryMeasures())); } else { searchResult = new SearchResultSet(resultRecordList); } logger.info("cost " + (System.currentTimeMillis() - current) + " in result group by and prepare netty message."); current = System.currentTimeMillis(); MessageHeader mh = new MessageHeader(NettyAction.NETTY_ACTION_SEARCH_FEEDBACK, Md5Util.encode(searchResult.toString())); SearchResultMessage searchResultMessage = new SearchResultMessage(mh, searchResult); ctx.writeAndFlush(searchResultMessage); }
From source file:com.berico.clavin.resolver.impl.lucene.LuceneCoordinateIndex.java
License:Apache License
/** * Search for locations around the supplied coordinate. * @param coordinate Coordinate to search for nearby locations. * @param distanceInKm Kilometer radius to search around the * target coordinate for named locations. * @param limit Max number of results to return from the index. * @return ResolvedCoordinate instance./* ww w . j a v a 2 s. c o m*/ */ List<ResolvedCoordinate> performSearch(CoordinateOccurrence<?> coordinate, int distanceInKm, int limit) throws Exception { // Acquire a searcher. IndexSearcher searcher = this.lucene.getSearcherManager().acquire(); // Convert the KM distance to degrees. double distanceInDegrees = DistanceUtils.dist2Degrees(distanceInKm, DistanceUtils.EARTH_MEAN_RADIUS_KM); // Convert the coordinate to it's lat/lon representation. LatLon center = coordinate.convertToLatLon(); // Create a circular bounding box using the coordinate as the center // and the distance as the circle's radius. Circle queryBoundary = this.lucene.getSpatialContext().makeCircle(center.getLongitude(), center.getLatitude(), distanceInDegrees); // Create a spatial search configuration SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.Intersects, queryBoundary); // Get a Lucene filter from the spatial config. Filter filter = this.lucene.getSpatialStrategy().makeFilter(spatialArgs); // Search the index using the circle as a bounding box (er...circle). TopDocs results = searcher.search(new MatchAllDocsQuery(), filter, DEFAULT_LIMIT); // Convert the results to a ResolvedCoordinate return LuceneUtils.convertToCoordinate(coordinate, searcher, results, lucene); }
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Searches the index for an attribute with a matching value. * //from www . j a va 2 s. c om * @param feature * - the attribute to match when searching * @param value * - the value to match when searching * @return a list of search results * @throws IllegalArgumentException * if any parameters are null, or if the attribute is not marked * with the search annotation * @throws IOException * if there are issues reading the index */ public List<SearchResult> search(EStructuralFeature feature, String value) throws IllegalArgumentException, IOException { if (feature == null) { throw new IllegalArgumentException("Attribute cannot be null"); } if (value == null) { throw new IllegalArgumentException("Value cannot be null"); } List<SearchResult> rvalue = new ArrayList<SearchResult>(); boolean tokenize = false; EAnnotation annotation = feature.getEAnnotation(EMFIndexUtil.SOURCE); if (annotation != null) { if (annotation.getDetails().containsKey(EMFIndexUtil.TOKENIZE_KEY)) { tokenize = true; } } else { // Bail out early if this feature should not be indexed throw new IllegalArgumentException("Attribute is not annotated to be indexed"); } String key = EMFIndexUtil.getKey(feature); DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { Query query = null; if (tokenize) { QueryParser parser = new QueryParser(version, key, analyzer); query = parser.parse(value); } else { Term term = new Term(key, value); query = new TermQuery(term); } ScoreDoc[] hits = searcher.search(query, null, MAX_SEARCH_RESULT).scoreDocs; // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); SearchResult result = new SearchResult(hitDoc); rvalue.add(result); logger.debug(hitDoc.toString()); } } catch (ParseException e) { logger.error(e.getMessage()); } finally { reader.close(); } return rvalue; }
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Searches the index for an attribute with a matching value and a matching * eclass/*from ww w . j a va 2 s . c o m*/ * * @param eclass * - the EClass to match when searching * @param attr * - the EAttribute to match when searching * @param value * - the value to match when searching * @return a list of search results * @throws IllegalArgumentException * if any parameters are null, or if the attribute is not marked * with the search annotation * @throws IOException * if there are issues reading the index */ public List<SearchResult> search(EClass eclass, EAttribute attr, String value) throws IllegalArgumentException, IOException { if (eclass == null) { throw new IllegalArgumentException("EClass cannot be null"); } if (attr == null) { throw new IllegalArgumentException("Attribute cannot be null"); } if (value == null) { throw new IllegalArgumentException("Value cannot be null"); } EAnnotation annotation = eclass.getEAnnotation(EMFIndexUtil.SOURCE); if (annotation == null) { // Bail out early if this feature should not be indexed throw new IllegalArgumentException("EClass is not annotated to be indexed"); } List<SearchResult> rvalue = new ArrayList<SearchResult>(); boolean tokenize = false; annotation = attr.getEAnnotation(EMFIndexUtil.SOURCE); if (annotation != null) { if (annotation.getDetails().containsKey(EMFIndexUtil.TOKENIZE_KEY)) { tokenize = true; } } else { // Bail out early if this feature should not be indexed throw new IllegalArgumentException("Attribute is not annotated to be indexed"); } String key = EMFIndexUtil.getKey(attr); DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { BooleanQuery bquery = new BooleanQuery(); TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, eclass.getName())); bquery.add(classQuery, Occur.MUST); Query query = null; if (tokenize) { QueryParser parser = new QueryParser(version, key, analyzer); query = parser.parse(value); } else { Term term = new Term(key, value); query = new TermQuery(term); } bquery.add(query, Occur.MUST); ScoreDoc[] hits = searcher.search(bquery, null, MAX_SEARCH_RESULT).scoreDocs; // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); SearchResult result = new SearchResult(hitDoc); rvalue.add(result); logger.debug(hitDoc.toString()); } } catch (ParseException e) { logger.error(e.getMessage()); } finally { reader.close(); } return rvalue; }
From source file:com.cohesionforce.search.EMFIndex.java
License:Open Source License
/** * Searches the index for a matching eclass * // w ww . ja v a 2s . c o m * @param eclass * - the EClass to match when searching * @return a list of search results * @throws IllegalArgumentException * if the eclass is null or is not annotated for indexing * @throws IOException * if there are issues reading the index */ public List<SearchResult> search(EClass eclass) throws IllegalArgumentException, IOException { if (eclass == null) { throw new IllegalArgumentException("EClass cannot be null"); } EAnnotation annotation = eclass.getEAnnotation(EMFIndexUtil.SOURCE); if (annotation == null) { throw new IllegalArgumentException("EClass is not annotated for indexing"); } List<SearchResult> rvalue = new ArrayList<SearchResult>(); DirectoryReader reader = DirectoryReader.open(fsDir); IndexSearcher searcher = new IndexSearcher(reader); try { TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, eclass.getName())); ScoreDoc[] hits = searcher.search(classQuery, null, MAX_SEARCH_RESULT).scoreDocs; // Iterate through the results: for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); SearchResult result = new SearchResult(hitDoc); rvalue.add(result); logger.debug(hitDoc.toString()); } } finally { reader.close(); } return rvalue; }