package com.senseidb.clue.commands;

import java.io.PrintStream;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;

import com.senseidb.clue.ClueContext;

public class DeleteCommand extends ClueCommand {

  public DeleteCommand(ClueContext ctx) {
    super(ctx);
  }

  @Override
  public String getName() {
    return "delete";
  }

  @Override
  public String help() {
    return "deletes a list of documents from searching via a query, input: query";
  }
  
  @Override
  public void execute(String[] args, PrintStream out) throws Exception {
    QueryParser qparser = new QueryParser("contents", new StandardAnalyzer());
    Query q = null;
    if (args.length == 0){
      q = new MatchAllDocsQuery();
    }
    else{
      StringBuilder buf = new StringBuilder();
      for (String s : args){
        buf.append(s).append(" ");
      }
      String qstring = buf.toString();
      try{
        q = qparser.parse(qstring);
      }
      catch(Exception e){
        out.println("cannot parse query: "+e.getMessage());
        return;
      }
    }
    
    if (q != null){
      IndexWriter writer = ctx.getIndexWriter();
      if (writer != null) {
        writer.deleteDocuments(q);
        writer.commit();
        ctx.refreshReader();
      }
      else {
        out.println("unable to open writer, index is in readonly mode");
      }
    }
  }

}

@@ -88,195 +88,25 @@
 ene.
-analysis.standard.StandardAnalyzer;%0Aimport org.apache.lucene.index.IndexWriter;%0Aimport org.apache.lucene.queryparser.classic.QueryParser;%0Aimport org.apache.lucene.search.MatchAllDocsQuery
+index.IndexWriter
 ;%0Aim
@@ -586,178 +586,19 @@
 uery
-Parser qparser = new QueryParser(%22contents%22, new StandardAnalyzer());%0A    Query q = null;%0A    if (args.length == 0)%7B%0A      q = new MatchAllDocsQuery();%0A    %7D%0A    else%7B%0A  
+ q = null;%0A
     
@@ -642,18 +642,16 @@
 ();%0A    
- 
- 
 for (Str
@@ -665,18 +665,16 @@
  args)%7B%0A
-  
       bu
@@ -698,30 +698,26 @@
 d(%22 %22);%0A    
- 
- 
 %7D%0A
-  
     String q
@@ -745,18 +745,16 @@
 ();%0A    
-  
 try%7B%0A   
@@ -760,27 +760,39 @@
     
-  
 q = 
-qparser.parse
+ctx.getQueryBuilder().build
 (qst
@@ -798,30 +798,26 @@
 tring);%0A    
-  %7D%0A  
+%7D%0A
     catch(Ex
@@ -828,18 +828,16 @@
 ion e)%7B%0A
-  
       ou
@@ -892,18 +892,16 @@
 ;%0A      
-  
 return;%0A
@@ -904,25 +904,59 @@
 rn;%0A    
-  
 %7D%0A    
-%7D
+%0A    out.println(%22parsed query: %22+q);
 %0A    %0A  

delete command: (#56)