List of usage examples for org.apache.lucene.search MatchAllDocsQuery MatchAllDocsQuery
MatchAllDocsQuery
From source file:DVBench.java
License:Apache License
static long search(String description, IndexSearcher searcher, String field, int iters, boolean quiet) throws Exception { int count = 0; Query query = new MatchAllDocsQuery(); Sort sort = new Sort(new SortField(field, SortField.Type.LONG)); long startTime = System.currentTimeMillis(); for (int i = 0; i < iters; i++) { TopDocs td = searcher.search(query, 20, sort); count += td.totalHits;// ww w . ja va2 s .c om } long endTime = System.currentTimeMillis(); if (!quiet) { double delta = endTime - startTime; double avg = delta / iters; double QPS = 1000d / avg; System.out.println(description + ": " + QPS); } return count; }
From source file:aos.lucene.search.advanced.FilterTest.java
License:Apache License
protected void setUp() throws Exception { // allBooks = new MatchAllDocsQuery(); dir = TestUtil.getBookIndexDirectory(); searcher = new IndexSearcher(dir); }
From source file:aos.lucene.search.advanced.SortingExample.java
License:Apache License
public static void main(String[] args) throws Exception { Query allBooks = new MatchAllDocsQuery(); QueryParser parser = new QueryParser(Version.LUCENE_46, // "contents", // new StandardAnalyzer( // Version.LUCENE_46)); // BooleanQuery query = new BooleanQuery(); // query.add(allBooks, BooleanClause.Occur.SHOULD); // query.add(parser.parse("java OR action"), BooleanClause.Occur.SHOULD); // Directory directory = TestUtil.getBookIndexDirectory(); // SortingExample example = new SortingExample(directory); // 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,// ww w.ja va 2s . c o m new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("category", SortField.STRING) })); directory.close(); }
From source file:aos.lucene.search.advanced.SpanQueryTest.java
License:Apache License
public void testSpanQueryFilter() throws Exception { SpanQuery[] quick_brown_dog = new SpanQuery[] { quick, brown, dog }; SpanQuery snq = new SpanNearQuery(quick_brown_dog, 5, true); Filter filter = new SpanQueryFilter(snq); Query query = new MatchAllDocsQuery(); TopDocs hits = searcher.search(query, filter, 10); assertEquals(1, hits.totalHits);//w w w.ja v a2 s. c o m assertEquals("wrong doc", 0, hits.scoreDocs[0].doc); }
From source file:aos.lucene.search.advanced.TimeLimitingCollectorTest.java
License:Apache License
public void testTimeLimitingCollector() throws Exception { Directory dir = TestUtil.getBookIndexDirectory(); IndexSearcher searcher = new IndexSearcher(dir); Query q = new MatchAllDocsQuery(); int numAllBooks = TestUtil.hitCount(searcher, q); TopScoreDocCollector topDocs = TopScoreDocCollector.create(10, false); Collector collector = new TimeLimitingCollector(topDocs, // #A 1000); // #A try {//from w w w . j a v a2s . c o m searcher.search(q, collector); assertEquals(numAllBooks, topDocs.getTotalHits()); // #B } catch (TimeExceededException tee) { // #C LOGGER.info("Too much time taken."); // #C } // #C searcher.close(); dir.close(); }
From source file:aos.lucene.search.ext.filters.SpecialsFilterTest.java
License:Apache License
@Override protected void setUp() throws Exception { allBooks = new MatchAllDocsQuery(); searcher = new IndexSearcher(TestUtil.getBookIndexDirectory(), true); }
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/*w ww. j a v a2 s .c om*/ */ 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:cc.osint.graphd.graph.Graph.java
License:Apache License
public List<JSONObject> query(IndexSearcher indexSearcher, String queryStr) throws Exception { long start_t = System.currentTimeMillis(); final List<JSONObject> results = new ArrayList<JSONObject>(); QueryParser qp = new QueryParser(Version.LUCENE_31, KEY_FIELD, analyzer); qp.setDefaultOperator(org.apache.lucene.queryParser.QueryParser.Operator.AND); qp.setAllowLeadingWildcard(true);/*from w w w .j a v a2 s .com*/ Query query = qp.parse(queryStr); org.apache.lucene.search.Filter filter = new org.apache.lucene.search.CachingWrapperFilter( new QueryWrapperFilter(query)); indexSearcher.search(new MatchAllDocsQuery(), filter, new Collector() { private int docBase; IndexReader reader; // ignore scoring public void setScorer(Scorer scorer) { } // accept docs out of order public boolean acceptsDocsOutOfOrder() { return true; } public void collect(int doc) { try { Document d = reader.document(doc); JSONObject result = new JSONObject(); for (Fieldable f : d.getFields()) { result.put(f.name(), d.get(f.name())); } results.add(result); } catch (Exception ex) { ex.printStackTrace(); } } public void setNextReader(IndexReader reader, int docBase) { this.reader = reader; this.docBase = docBase; } }); long end_t = System.currentTimeMillis(); //log.info("query: hits.scoreDocs.length = " + results.size() + " (" + (end_t-start_t) + "ms)"); return results; }
From source file:cn.hbu.cs.esearch.service.impl.EsearchSearchServiceImpl.java
License:Apache License
@Override public SearchResult search(SearchRequest sResquest) throws EsearchException { try {// www. j a va2 s .co 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.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;//from ww w . java2s. c o m is = searcherManager.acquire(); 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); }