How can I flush/delete/erase all the index files/data in the disk using Apache Lucene.This is my code so far and still I can't remove index files.Please help me out...
Test:
public class Test {
private static final String INDEX_DIR = "/home/amila/Lucene/REST/indexing";
public static void main(String[] args) {
try {
ContentIndexer contentIndexer = new ContentIndexer(INDEX_DIR);
contentIndexer.flushDisk();
System.out.println("Flushed");
} catch (IOException e) {
e.printStackTrace();
}
}
}
ContentIndexer:
public class ContentIndexer {
private IndexWriter writer;
public ContentIndexer(String indexDir) throws IOException {
// create the index
if (writer == null) {
writer = new IndexWriter(FSDirectory.open(new File(indexDir)),
new IndexWriterConfig(Version.LUCENE_36,
new StandardAnalyzer(Version.LUCENE_36)));
}
}
public void flushDisk() {
try {
writer.deleteAll();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Edited -- Updated Answer
public class Test {
private static final String INDEX_DIR = "/home/amila/Lucene/REST/indexing";
public static void main(String[] args) {
try {
IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36,
new StandardAnalyzer(Version.LUCENE_36));
conf.setOpenMode(OpenMode.CREATE);
Directory directory = FSDirectory.open(new File(INDEX_DIR));
IndexWriter indexWriter = new IndexWriter(directory, conf);
indexWriter.deleteAll();
indexWriter.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The simplest way is to open IndexWriter with a CREATE mode (via indexWriterConfig.setOpenMode(...)). This removes all existing index files in the given directory.
For older versions IndexWriter constructor also has a special boolean create flag which does the same thing.
You can use two options :
You can call the delete all method of the writer
indexWriter.DeleteAll();
You can create a new indexWriter with the create flag set to true ( open mode= created)
new IndexWriter(_luceneDirectory, _analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
Related
Problem: if the path to the file was not specified in the arguments, then it still displays the phrase "The file was closed". This works 2 times. In uploadToFile and read method. I pass one path in the arguments, and the second is written in the DownloadFile
public class Task implements AutoCloseable {
public static void main(String[] args) throws IOException {
String DownloadFile = "C:\\Users\\VGilenko\\IdeaProjects\\Task\\src\\main\\resources\\Out.txt";
Map<String, Departament> departments = new HashMap<>();
String path = args.length > 0 ? args[0] : null;
read(path, departments);
transferToDepartment(departments, DownloadFile);
}
private static void uploadToFile(List download, String path) {
int i = 0;
try (FileWriter writer = new FileWriter(path, false)) {
...
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
System.out.println("The file was closed");
}
}
public static void transferToDepartment(Map<String, Departament> departments, String downloadFile) {
List<String> download = new ArrayList<>();
...
}
uploadToFile(download, downloadFile);
}
public static void read(String path, Map<String, Departament> departments) throws IOException {
assert path != null;
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "CP1251")); br) {
.....
}
}
} catch (FileNotFoundException e) {
System.out.println("The file was not found, check the path");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Correct the file path, step out of the array");
} catch (NullPointerException e) {
System.out.println("You forgot to register the path to the file");
} finally {
System.out.println("The file was closed");
}
}
#Override
public void close() {
System.out.println("The file was closed");
}
}
You have your printout "The file was closed" in your finally statement. If you don't specify a file, you will catch an Exception, and your finally block will be executed.
An easy fix would be to check for the existence of the path (not being empty, not being null).
I`m new developper of spark, And now I was block by a issue.
I was implements freemarker as web template.
Not like other framework when you modify a .ftl file, You no need to restart the server.
But now in my local it must restart the server if I wanto see the change.
Below is code.
public class SparkServer {
public static void main(String[] args){
get("/hello",(request,response) ->{
Map root = new HashMap();
root.put("user", "xiekakaban");
Map product = new HashMap();
product.put("name","Pringles");
product.put("price",13.2);
root.put("product",product);
return new ModelAndView(root,"test.ftl");
},FreeMarkerEngine.getInstance());
}
}
public class FreeMarkerEngine extends TemplateEngine{
private static FreeMarkerEngine freeMarkerEngine;
private Configuration freeConfig;
private FreeMarkerEngine() throws IOException{
freeConfig = new Configuration();
freeConfig.setDirectoryForTemplateLoading(StringUtil.getResourceFile("templates"));
freeConfig.setTemplateUpdateDelay(1);
}
public static FreeMarkerEngine getInstance(){
if(freeMarkerEngine == null){
try {
freeMarkerEngine = new FreeMarkerEngine();
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
}
return freeMarkerEngine;
}
#Override
public String render(ModelAndView modelAndView) {
StringWriter stringWriter = new StringWriter();
try {
freeConfig.clearTemplateCache();
freeConfig.clearSharedVariables();
freeConfig.clearEncodingMap();
Template template = freeConfig.getTemplate(modelAndView.getViewName());
template.process(modelAndView.getModel(), stringWriter);
System.out.println(stringWriter.toString());
return stringWriter.toString();
} catch (IOException | TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Can not find the template:"+modelAndView.getViewName();
}
}
I don`t sure whether it is cache by spark or freemarker.But I have clear freemarker cache.
Anyone can help me.....
ok, I have figure out it.
Reloading the static files in Spark/Jetty-server
first I think you should make sure which page the freemarker load.
if you not setup, it will load ftl under "target" fold.
I think I put out a stupid question....
I want to delete embedded files in ms word document using java API and i tried with POI API but it is not working , please suggest best java API.
public static void modifyDocxFile(String fileName) {
try {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
System.out.println(document.getAllEmbedds().size());
/*List<PackagePart> embeddedDocs = document.getAllEmbedds();
if (embeddedDocs != null && !embeddedDocs.isEmpty()) {
embeddedDocs.remove(0);
}*/
document.getAllEmbedds().remove(0);
document.write(new FileOutputStream(new File("D:\\study\\word\\SampleModified.docx")));
//System.out.println(document.getAllEmbedds());
fis.close();
//fis2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
modifyDocxFile("D:\\study\\word\\Sample.docx");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//readDocFile("C:\\Test.doc");
}
I want to lsit all the files form a directory i get as a paramter that end in .java and have a certain text in them,I have issues when i want to do this for all of it's ssubfolders.
Using filenamefilter on a folder only returns the files in the folder not in it's subfolders.
Any ideea how I can make it work on the subfolders too?
I solved this problem and I am attaching my solution jsut in case somebody else bumps into this.Also,I used Threads because I found it easier to do it with Threads.
The ideea behind my solution was
one thread was in charge of getting all the files that end in ".java" [producer]
another parsing through those files to get the ones that contain a specific text
The Producer:
public class ThreadProducator extends Thread {
public Bufferino buffer ;
File directory ;
static ArrayList<File>files = new ArrayList<File>();
public ThreadProducator(Bufferino a){
this.buffer = a;
directory = new File("src");
}
public static ArrayList<File> getFiles(File f){
ArrayList<File> files1= new ArrayList<File>();
for ( File fis : f.listFiles()){
if ( fis.isDirectory()){
getFiles(fis);
}
else{
if(fis.getName().endsWith(".java")){
files.add(fis);
//System.out.println(files.toString());
}
}
}
return files;
}
public void run() {
// TODO Auto-generated method stub
if(directory.isDirectory()){
ArrayList<File>files =new ThreadProducator(buffer).getFiles(directory);
// System.out.println(files.toString());
buffer.put(files);
try {
sleep((int)Math.random()*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
The Consumer:
public class ThreadConsumator implements Runnable {
private Bufferino buf;
String lookedFor = "asdas";
public ThreadConsumator(Bufferino bufi){
this.buf = bufi;
}
#Override
public void run() {
// TODO Auto-generated method stub
ArrayList<File> res = new ArrayList<File>();
String content="";
System.out.println("Files containing the word "+lookedFor+" are:");
for ( int i = 0 ; i <25;i++){
res = buf.get();
for (File f : res)
try {
content = new Scanner(f).useDelimiter("\\Z").next();
if(content.contains(lookedFor)){
System.out.println(f.getName());
}
/*System.out.println("-----------------------");
System.out.println(content);
System.out.println("-----------------------");*/
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
I have created an Apache Lucene search project using java.If there is an error/corrupt/missed files in index folder,how can I find it?
I have tried CheckIndex but still no good.
How can I check whether the index is damaged or corrupt?
public class Test {
private static final String INDEX_DIR = "/home/amila/Lucene/REST/indexing";
public static void main(String[] args) {
try {
Directory directory = FSDirectory.open(new File(INDEX_DIR));
CheckIndex checkIndex = new CheckIndex(directory);
Status status= checkIndex.checkIndex();
System.out.println(status.missingSegments);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}