List of usage examples for org.apache.lucene.index IndexWriter commit
@Override public final long commit() throws IOException
Commits all pending changes (added and deleted documents, segment merges, added indexes, etc.) to the index, and syncs all referenced index files, such that a reader will see the changes and the index updates will survive an OS or machine crash or power loss.
From source file:com.gitblit.service.LuceneService.java
License:Apache License
/** * Delete a blob from the specified branch of the repository index. * * @param repositoryName//from w w w .java2 s .c o m * @param branch * @param path * @throws Exception * @return true, if deleted, false if no record was deleted */ public boolean deleteBlob(String repositoryName, String branch, String path) throws Exception { String pattern = MessageFormat.format("{0}:'{'0} AND {1}:\"'{'1'}'\" AND {2}:\"'{'2'}'\"", FIELD_OBJECT_TYPE, FIELD_BRANCH, FIELD_PATH); String q = MessageFormat.format(pattern, SearchObjectType.blob.name(), branch, path); StandardAnalyzer analyzer = new StandardAnalyzer(); QueryParser qp = new QueryParser(FIELD_SUMMARY, analyzer); BooleanQuery query = new BooleanQuery.Builder().add(qp.parse(q), Occur.MUST).build(); IndexWriter writer = getIndexWriter(repositoryName); int numDocsBefore = writer.numDocs(); writer.deleteDocuments(query); writer.commit(); int numDocsAfter = writer.numDocs(); if (numDocsBefore == numDocsAfter) { logger.debug(MessageFormat.format("no records found to delete {0}", query.toString())); return false; } else { logger.debug(MessageFormat.format("deleted {0} records with {1}", numDocsBefore - numDocsAfter, query.toString())); return true; } }
From source file:com.gitblit.service.LuceneService.java
License:Apache License
/** * Updates a repository index incrementally from the last indexed commits. * * @param model// w w w .j a v a 2 s . c o m * @param repository * @return IndexResult */ private IndexResult updateIndex(RepositoryModel model, Repository repository) { IndexResult result = new IndexResult(); try { FileBasedConfig config = getConfig(repository); config.load(); // build a quick lookup of annotated tags Map<String, List<String>> tags = new HashMap<String, List<String>>(); for (RefModel tag : JGitUtils.getTags(repository, false, -1)) { if (!tag.isAnnotatedTag()) { // skip non-annotated tags continue; } if (!tags.containsKey(tag.getObjectId().getName())) { tags.put(tag.getReferencedObjectId().getName(), new ArrayList<String>()); } tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName); } // detect branch deletion // first assume all branches are deleted and then remove each // existing branch from deletedBranches during indexing Set<String> deletedBranches = new TreeSet<String>(); for (String alias : config.getNames(CONF_ALIAS)) { String branch = config.getString(CONF_ALIAS, null, alias); deletedBranches.add(branch); } // get the local branches List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1); // sort them by most recently updated Collections.sort(branches, new Comparator<RefModel>() { @Override public int compare(RefModel ref1, RefModel ref2) { return ref2.getDate().compareTo(ref1.getDate()); } }); // reorder default branch to first position RefModel defaultBranch = null; ObjectId defaultBranchId = JGitUtils.getDefaultBranch(repository); for (RefModel branch : branches) { if (branch.getObjectId().equals(defaultBranchId)) { defaultBranch = branch; break; } } branches.remove(defaultBranch); branches.add(0, defaultBranch); // walk through each branches for (RefModel branch : branches) { String branchName = branch.getName(); boolean indexBranch = false; if (model.indexedBranches.contains(com.gitblit.Constants.DEFAULT_BRANCH) && branch.equals(defaultBranch)) { // indexing "default" branch indexBranch = true; } else if (branch.getName().startsWith(com.gitblit.Constants.R_META)) { // ignore internal meta branches indexBranch = false; } else { // normal explicit branch check indexBranch = model.indexedBranches.contains(branch.getName()); } // if this branch is not specifically indexed then skip if (!indexBranch) { continue; } // remove this branch from the deletedBranches set deletedBranches.remove(branchName); // determine last commit String keyName = getBranchKey(branchName); String lastCommit = config.getString(CONF_BRANCH, null, keyName); List<RevCommit> revs; if (StringUtils.isEmpty(lastCommit)) { // new branch/unindexed branch, get all commits on branch revs = JGitUtils.getRevLog(repository, branchName, 0, -1); } else { // pre-existing branch, get changes since last commit revs = JGitUtils.getRevLog(repository, lastCommit, branchName); } if (revs.size() > 0) { result.branchCount += 1; } // reverse the list of commits so we start with the first commit Collections.reverse(revs); for (RevCommit commit : revs) { // index a commit result.add(index(model.name, repository, branchName, commit)); } // update the config config.setString(CONF_ALIAS, null, keyName, branchName); config.setString(CONF_BRANCH, null, keyName, branch.getObjectId().getName()); config.save(); } // the deletedBranches set will normally be empty by this point // unless a branch really was deleted and no longer exists if (deletedBranches.size() > 0) { for (String branch : deletedBranches) { IndexWriter writer = getIndexWriter(model.name); writer.deleteDocuments(new Term(FIELD_BRANCH, branch)); writer.commit(); } } result.success = true; } catch (Throwable t) { logger.error(MessageFormat.format("Exception while updating {0} Lucene index", model.name), t); } return result; }
From source file:com.gitblit.tickets.TicketIndexer.java
License:Apache License
/** * Deletes all tickets for the the repository from the index. *//*from w ww . j a va 2 s . c o m*/ public boolean deleteAll(RepositoryModel repository) { try { IndexWriter writer = getWriter(); StandardAnalyzer analyzer = new StandardAnalyzer(); QueryParser qp = new QueryParser(Lucene.rid.name(), analyzer); BooleanQuery query = new BooleanQuery.Builder().add(qp.parse(repository.getRID()), Occur.MUST).build(); int numDocsBefore = writer.numDocs(); writer.deleteDocuments(query); writer.commit(); closeSearcher(); int numDocsAfter = writer.numDocs(); if (numDocsBefore == numDocsAfter) { log.debug(MessageFormat.format("no records found to delete in {0}", repository)); return false; } else { log.debug(MessageFormat.format("deleted {0} records in {1}", numDocsBefore - numDocsAfter, repository)); return true; } } catch (Exception e) { log.error("error", e); } return false; }
From source file:com.gitblit.tickets.TicketIndexer.java
License:Apache License
/** * Bulk Add/Update tickets in the Lucene index * * @param tickets//w ww .j av a 2 s.c o m */ public void index(List<TicketModel> tickets) { try { IndexWriter writer = getWriter(); for (TicketModel ticket : tickets) { Document doc = ticketToDoc(ticket); writer.addDocument(doc); } writer.commit(); closeSearcher(); } catch (Exception e) { log.error("error", e); } }
From source file:com.gitblit.tickets.TicketIndexer.java
License:Apache License
/** * Add/Update a ticket in the Lucene index * * @param ticket/*from ww w . j a v a2 s . com*/ */ public void index(TicketModel ticket) { try { IndexWriter writer = getWriter(); delete(ticket.repository, ticket.number, writer); Document doc = ticketToDoc(ticket); writer.addDocument(doc); writer.commit(); closeSearcher(); } catch (Exception e) { log.error("error", e); } }
From source file:com.gitblit.tickets.TicketIndexer.java
License:Apache License
/** * Delete a ticket from the Lucene index. * * @param repository/*from w w w. j a v a 2s. co m*/ * @param ticketId * @throws Exception * @return true, if deleted, false if no record was deleted */ private boolean delete(String repository, long ticketId, IndexWriter writer) throws Exception { StandardAnalyzer analyzer = new StandardAnalyzer(); QueryParser qp = new QueryParser(Lucene.did.name(), analyzer); BooleanQuery query = new BooleanQuery.Builder() .add(qp.parse(StringUtils.getSHA1(repository + ticketId)), Occur.MUST).build(); int numDocsBefore = writer.numDocs(); writer.deleteDocuments(query); writer.commit(); closeSearcher(); int numDocsAfter = writer.numDocs(); if (numDocsBefore == numDocsAfter) { log.debug(MessageFormat.format("no records found to delete in {0}", repository)); return false; } else { log.debug(MessageFormat.format("deleted {0} records in {1}", numDocsBefore - numDocsAfter, repository)); return true; } }
From source file:com.github.buzztaiki.lucene.lastuni.CJKSingleCharQueryTest.java
License:Apache License
private void addDoc(Directory dir, Analyzer analyzer, String content) throws IOException { IndexWriter writer = newWriter(dir, analyzer); try {/*ww w .ja v a 2 s. co m*/ addDoc(writer, content); writer.commit(); } finally { writer.close(); } }
From source file:com.github.flaxsearch.testutil.GutenbergIndex.java
License:Apache License
public static void writeDocuments(IndexWriter writer, Path source) throws IOException { int count = 0; try (DirectoryStream<Path> directory = Files.newDirectoryStream(source)) { for (Path file : directory) { byte[] data = Files.readAllBytes(file); writer.addDocument(buildDocument(file, data)); if (count++ % 7 == 0) writer.commit(); }// w w w. j a v a2s.c o m } }
From source file:com.github.mosuka.apache.lucene.example.cmd.AddCommand.java
License:Apache License
@Override public void execute(Map<String, Object> attrs) { Map<String, Object> responseMap = new LinkedHashMap<String, Object>(); String responseJSON = null;//from ww w . j av a 2s.com Directory indexDir = null; IndexWriter writer = null; try { String index = (String) attrs.get("index"); String uniqueId = (String) attrs.get("unique_id"); String text = (String) attrs.get("text"); indexDir = FSDirectory.open(new File(index).toPath()); Document document = LuceneExampleUtil.createDocument(uniqueId, text); IndexWriterConfig config = new IndexWriterConfig(LuceneExampleUtil.createAnalyzerWrapper()); config.setOpenMode(OpenMode.CREATE_OR_APPEND); writer = new IndexWriter(indexDir, config); writer.addDocument(document); writer.commit(); responseMap.put("status", 0); responseMap.put("message", "OK"); } catch (IOException e) { responseMap.put("status", 1); responseMap.put("message", e.getMessage()); } finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { responseMap.put("status", 1); responseMap.put("message", e.getMessage()); } try { if (indexDir != null) { indexDir.close(); } } catch (IOException e) { responseMap.put("status", 1); responseMap.put("message", e.getMessage()); } } try { ObjectMapper mapper = new ObjectMapper(); responseJSON = mapper.writeValueAsString(responseMap); } catch (IOException e) { responseJSON = String.format("{\"status\":1, \"message\":\"%s\"}", e.getMessage()); } System.out.println(responseJSON); }
From source file:com.github.mosuka.apache.lucene.example.cmd.DeleteCommand.java
License:Apache License
@Override public void execute(Map<String, Object> attrs) { Map<String, Object> responseMap = new LinkedHashMap<String, Object>(); String responseJSON = null;/* www . ja v a 2s . c om*/ Directory indexDir = null; IndexWriter writer = null; try { String index = (String) attrs.get("index"); String uniqueId = (String) attrs.get("unique_id"); indexDir = FSDirectory.open(new File(index).toPath()); IndexWriterConfig config = new IndexWriterConfig(LuceneExampleUtil.createAnalyzerWrapper()); config.setOpenMode(OpenMode.CREATE_OR_APPEND); writer = new IndexWriter(indexDir, config); writer.deleteDocuments(new Term("id", uniqueId)); writer.commit(); responseMap.put("status", 0); responseMap.put("message", "OK"); } catch (IOException e) { responseMap.put("status", 1); responseMap.put("message", e.getMessage()); } finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { responseMap.put("status", 1); responseMap.put("message", e.getMessage()); } try { if (indexDir != null) { indexDir.close(); } } catch (IOException e) { responseMap.put("status", 1); responseMap.put("message", e.getMessage()); } } try { ObjectMapper mapper = new ObjectMapper(); responseJSON = mapper.writeValueAsString(responseMap); } catch (IOException e) { responseJSON = String.format("{\"status\":1, \"message\":\"%s\"}", e.getMessage()); } System.out.println(responseJSON); }