List of usage examples for org.apache.lucene.search IndexSearcher search
public <C extends Collector, T> T search(Query query, CollectorManager<C, T> collectorManager) throws IOException
From source file:com.vmware.demo.sgf.lucene.impl.LuceneGemFireRepositoryImpl.java
License:Apache License
private Iterable searchStore(Serializable id, boolean singleRecordRequired) { ArrayList results = new ArrayList(); IndexSearcher searcher = searchManager.acquire(); try {//from w w w .j av a 2s.c o m // Extract search criteria String field = defaultFeild; String searchText = null; if (id instanceof SearcheCriteria) { SearcheCriteria key = (SearcheCriteria) id; field = key.getSearchField(); searchText = key.getSearchText(); } else if (id instanceof String) { searchText = (String) id; } QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer); Query query = parser.parse(searchText); TopDocs docs = searcher.search(query, Integer.MAX_VALUE); if (docs.totalHits > 0) { for (int i = 0; i < docs.totalHits; i++) { ScoreDoc hit = docs.scoreDocs[i]; Document doc = searcher.doc(hit.doc); Object gfKey = ObjectSerializer.deserialize(doc.getBinaryValue(GEMFIRE_KEY).bytes); results.add(gfKey); if (singleRecordRequired) break; } } } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { searchManager.release(searcher); } catch (IOException e) { e.printStackTrace(); } } searcher = null; return results; }
From source file:com.vmware.demo.sgf.lucene.impl.LuceneGemFireRepositoryImpl.java
License:Apache License
@Override public Iterable findAll() { ArrayList results = new ArrayList(); IndexSearcher searcher = searchManager.acquire(); try {/*from w w w. j a va 2s . co m*/ QueryParser parser = new QueryParser(Version.LUCENE_40, savedField, analyzer); Query query = parser.parse(savedFieldValue); TopDocs docs = searcher.search(query, Integer.MAX_VALUE); if (docs.totalHits > 0) { for (int i = 0; i < docs.totalHits; i++) { ScoreDoc hit = docs.scoreDocs[i]; Document doc = searcher.doc(hit.doc); Object gfKey = ObjectSerializer.deserialize(doc.getBinaryValue(GEMFIRE_KEY).bytes); results.add(gfKey); } } } catch (Exception e) { e.printStackTrace(); } finally { try { searchManager.release(searcher); } catch (IOException e) { e.printStackTrace(); } } searcher = null; return results; }
From source file:com.vmware.xenon.services.common.LuceneDocumentIndexService.java
License:Open Source License
private void applyDocumentExpirationPolicy(IndexWriter w) throws Throwable { // if we miss a document update, we will catch it, and refresh the searcher on the // next update or maintenance IndexSearcher s = this.searcher != null ? this.searcher : updateSearcher(null, Integer.MAX_VALUE, w); if (s == null) { return;/*from ww w. jav a 2 s . co m*/ } long expirationUpperBound = Utils.getNowMicrosUtc(); Query versionQuery = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_EXPIRATION_TIME_MICROS, 1L, expirationUpperBound); TopDocs results = s.search(versionQuery, EXPIRED_DOCUMENT_SEARCH_THRESHOLD); if (results.totalHits == 0) { return; } // The expiration query will return all versions for a link. Use a set so we only delete once per link Set<String> links = new HashSet<>(); long now = Utils.getNowMicrosUtc(); for (ScoreDoc sd : results.scoreDocs) { Document d = s.getIndexReader().document(sd.doc, this.fieldsToLoadNoExpand); String link = d.get(ServiceDocument.FIELD_NAME_SELF_LINK); IndexableField versionField = d.getField(ServiceDocument.FIELD_NAME_VERSION); long versionExpired = versionField.numericValue().longValue(); long latestVersion = this.getLatestVersion(s, link); if (versionExpired < latestVersion) { continue; } if (!links.add(link)) { continue; } checkAndDeleteExpiratedDocuments(link, s, sd.doc, d, now); } // More documents to be expired trigger maintenance right away. if (results.totalHits > EXPIRED_DOCUMENT_SEARCH_THRESHOLD) { adjustStat(STAT_NAME_DOCUMENT_EXPIRATION_FORCED_MAINTENANCE_COUNT, 1); ServiceMaintenanceRequest body = ServiceMaintenanceRequest.create(); Operation servicePost = Operation.createPost(UriUtils.buildUri(getHost(), getSelfLink())) .setReferer(getHost().getUri()).setBody(body); // servicePost can be cached handleMaintenance(servicePost); } }
From source file:com.vnet.demo.service.lucene.LuceneService.java
License:Apache License
public SearchResult<DocumentData> query(String keyword, int start, int number) { SearchResult<DocumentData> searchResult = null; try {// w w w .j a v a2 s . c o m List<DocumentData> documentDatas = new ArrayList<DocumentData>(); DirectoryReader ireader = DirectoryReader.open(index); IndexSearcher isearcher = new IndexSearcher(ireader); Query query = new QueryParser(version, "title", analyzer).parse(keyword + "*"); TopDocs hits = null; if (start > 0) { TopDocs result = isearcher.search(query, start); ScoreDoc scoreDoc = result.scoreDocs[result.scoreDocs.length - 1]; hits = isearcher.searchAfter(scoreDoc, query, number); } else { hits = isearcher.search(query, number); } for (int i = 0; i < hits.scoreDocs.length; i++) { DocumentData data = new DocumentData(); Document hitDoc = isearcher.doc(hits.scoreDocs[i].doc); data.setId(Long.parseLong(hitDoc.get("id"))); data.setTitle(hitDoc.get("title")); data.setSummary(hitDoc.get("summary")); data.setCreateDate(Long.parseLong(hitDoc.get("createDate"))); documentDatas.add(data); } searchResult = new SearchResult<DocumentData>(new Long(hits.totalHits), documentDatas); } catch (ParseException | IOException e) { e.printStackTrace(); } return searchResult; }
From source file:com.weasel.lucene.ik.sample.IKAnalyzerDemo.java
License:Apache License
public static void main(String[] args) { //Lucene Document?? String fieldName = "text"; ///* w w w. ja v a2 s. c om*/ String text = "IK Analyzer???????"; //IKAnalyzer? Analyzer analyzer = new IKAnalyzer(); Directory directory = null; IndexWriter iwriter = null; IndexReader ireader = null; IndexSearcher isearcher = null; try { // directory = new RAMDirectory(); //?IndexWriterConfig IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_34, analyzer); iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); iwriter = new IndexWriter(directory, iwConfig); // Document doc = new Document(); doc.add(new Field("ID", "10000", Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field(fieldName, text, Field.Store.YES, Field.Index.ANALYZED)); iwriter.addDocument(doc); iwriter.close(); //?********************************** //? ireader = IndexReader.open(directory); isearcher = new IndexSearcher(ireader); String keyword = "?"; //QueryParser?Query QueryParser qp = new QueryParser(Version.LUCENE_34, fieldName, analyzer); qp.setDefaultOperator(QueryParser.AND_OPERATOR); Query query = qp.parse(keyword); //?5? TopDocs topDocs = isearcher.search(query, 5); System.out.println("" + topDocs.totalHits); // ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (int i = 0; i < topDocs.totalHits; i++) { Document targetDoc = isearcher.doc(scoreDocs[i].doc); System.out.println("" + targetDoc.toString()); } } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { if (ireader != null) { try { ireader.close(); } catch (IOException e) { e.printStackTrace(); } } if (directory != null) { try { directory.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.work.SearchFiles.java
License:Apache License
/** Simple command-line based search demo. */ public static void main(String[] args) throws Exception { // String indexPath = "C:/Users/Harish/Desktop/IR/Data/Data English/masc_500k_texts/written/letters/index/one/"; // String queries = "C:/Users/Harish/Desktop/IR/Data/Data English/masc_500k_texts/written/letters/query/query1.txt"; // Analyzer analyzer = new StandardAnalyzer(); //Hindi/*from ww w .java 2 s .c o m*/ String indexPath = "C:/Users/Harish/Desktop/IR/Data/Hindi Data/hin_corp_unicode/index/one/"; // String queries = "C:/Users/Harish/Desktop/IR/Data/Hindi Data/hin_corp_unicode/query/one.txt"; String queries = null; Analyzer analyzer = new HindiAnalyzer(); //Chinese // Analyzer analyzer = new CJKAnalyzer(); String index = indexPath; String field = "contents"; String a = "???"; int repeat = 0; boolean raw = false; String queryString = null; int hitsPerPage = 10; IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index))); IndexSearcher searcher = new IndexSearcher(reader); BufferedReader in = null; if (queries != null) { in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8); } else { in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); } QueryParser parser = new QueryParser(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, 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.work.SearchFiles.java
License:Apache License
/** * This demonstrates a typical paging search scenario, where the search engine presents * pages of size n to the user. The user can then go to the next page if interested in * the next hits.//from w w w . j av a 2 s .c om * * When the query is executed for the first time, then only enough results are collected * to fill 5 result pages. If the user wants to page beyond this limit, then the query * is executed another time and all hits are collected. * */ public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query, int hitsPerPage, boolean raw, boolean interactive) throws IOException { // Collect enough docs to show 5 pages TopDocs results = searcher.search(query, 5 * hitsPerPage); ScoreDoc[] hits = results.scoreDocs; int numTotalHits = results.totalHits; System.out.println(numTotalHits + " total matching documents"); int start = 0; int end = Math.min(numTotalHits, hitsPerPage); while (true) { if (end > hits.length) { System.out.println("Only results 1 - " + hits.length + " of " + numTotalHits + " total matching documents collected."); System.out.println("Collect more (y/n) ?"); String line = in.readLine(); if (line.length() == 0 || line.charAt(0) == 'n') { break; } hits = searcher.search(query, numTotalHits).scoreDocs; } end = Math.min(hits.length, start + hitsPerPage); for (int i = start; i < end; i++) { if (raw) { // output raw format System.out.println("doc=" + hits[i].doc + " score=" + hits[i].score); continue; } Document doc = searcher.doc(hits[i].doc); String path = doc.get("path"); if (path != null) { System.out.println((i + 1) + ". " + path); String title = doc.get("title"); if (title != null) { System.out.println(" Title: " + doc.get("title")); } } else { System.out.println((i + 1) + ". " + "No path for this document"); } } if (!interactive || end == 0) { break; } if (numTotalHits >= end) { boolean quit = false; while (true) { System.out.print("Press "); if (start - hitsPerPage >= 0) { System.out.print("(p)revious page, "); } if (start + hitsPerPage < numTotalHits) { System.out.print("(n)ext page, "); } System.out.println("(q)uit or enter number to jump to a page."); String line = in.readLine(); if (line.length() == 0 || line.charAt(0) == 'q') { quit = true; break; } if (line.charAt(0) == 'p') { start = Math.max(0, start - hitsPerPage); break; } else if (line.charAt(0) == 'n') { if (start + hitsPerPage < numTotalHits) { start += hitsPerPage; } break; } else { int page = Integer.parseInt(line); if ((page - 1) * hitsPerPage < numTotalHits) { start = (page - 1) * hitsPerPage; break; } else { System.out.println("No such page"); } } } if (quit) break; end = Math.min(numTotalHits, start + hitsPerPage); } } }
From source file:com.xiaomi.linden.core.search.LindenCoreImpl.java
License:Apache License
public JSONObject getInputDocument(Term term) throws IOException { SearcherTaxonomyManager.SearcherAndTaxonomy searcherAndTaxonomy = lindenNRTSearcherManager.acquire(); try {/*from w ww. j a v a 2 s . c o m*/ IndexSearcher indexSearcher = searcherAndTaxonomy.searcher; TopDocs results = indexSearcher.search(new TermQuery(term), 1); if (results.scoreDocs.length == 0) { return null; } int docId = results.scoreDocs[0].doc; String source = LindenUtil.getSource(indexSearcher, docId, null, null, config); return JSONObject.parseObject(source); } finally { lindenNRTSearcherManager.release(searcherAndTaxonomy); } }
From source file:com.xpn.xwiki.plugin.lucene.internal.IndexRebuilder.java
License:Open Source License
public boolean isIndexed(DocumentReference documentReference, String version, String language, IndexSearcher searcher) { boolean exists = false; BooleanQuery query = new BooleanQuery(); query.add(new TermQuery(new Term(IndexFields.DOCUMENT_NAME, documentReference.getName().toLowerCase())), BooleanClause.Occur.MUST);/*from w ww. java 2s. c o m*/ query.add( new TermQuery(new Term(IndexFields.DOCUMENT_SPACE, documentReference.getLastSpaceReference().getName().toLowerCase())), BooleanClause.Occur.MUST); query.add(new TermQuery( new Term(IndexFields.DOCUMENT_WIKI, documentReference.getWikiReference().getName().toLowerCase())), BooleanClause.Occur.MUST); if (version != null) { query.add(new TermQuery(new Term(IndexFields.DOCUMENT_VERSION, version)), BooleanClause.Occur.MUST); } if (language != null) { query.add(new TermQuery( new Term(IndexFields.DOCUMENT_LANGUAGE, StringUtils.isEmpty(language) ? "default" : language)), BooleanClause.Occur.MUST); } try { TopDocs topDocs = searcher.search(query, 1); exists = topDocs.totalHits == 1; } catch (IOException e) { LOGGER.error("Faild to search for page [{}] in Lucene index", documentReference, e); } return exists; }
From source file:com.yahoo.bard.webservice.data.dimension.impl.LuceneSearchProvider.java
License:Apache License
/** * Returns the requested page of dimension metadata from Lucene. * <p>/* w w w .ja v a 2s . c o m*/ * Note that this method acquires and releases a read lock when querying Lucene for data. * * @param indexSearcher The service to find the desired dimension metadata in the Lucene index * @param lastEntry The last entry from the previous page of dimension metadata, the indexSearcher will begin its * search after this entry (if lastEntry is null, the indexSearcher will begin its search from the beginning) * @param query The Lucene query used to locate the desired dimension metadata * @param perPage The number of entries per page * * @return The desired page of dimension metadata */ private TopDocs getPageOfData(IndexSearcher indexSearcher, ScoreDoc lastEntry, Query query, int perPage) { TimeLimitingCollectorManager manager = new TimeLimitingCollectorManager(searchTimeout, lastEntry, perPage); lock.readLock().lock(); try { return indexSearcher.search(query, manager); } catch (IOException e) { String errorMessage = "Unable to find dimension rows for specified page."; LOG.error(errorMessage); throw new RuntimeException(errorMessage); } catch (TimeLimitingCollector.TimeExceededException e) { LOG.warn("Lucene query timeout: {}. {}", query, e.getMessage()); throw new TimeoutException(e.getMessage(), e); } finally { lock.readLock().unlock(); } }