How to delete multiple documents

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to delete multiple documents

srinivas.t@aksha.in

In the Following Code if i insert one document i was
able to delete but when i insert multiple documents i
was unable to delete.

please follow the code and advice me

thanks
srinvias.



            // Code to index Documents
            for (int count = 1; count < 10; count++)
            {
                String CatID = "13";
                String FieldName = "Field-" + count;

                document.add(Field.Text("FieldName",
FieldName));
                document.add(Field.Keyword("CatID",
CatID));
                Analyzer analyzer = new
StandardAnalyzer();
                IndexWriter writer = new
IndexWriter(indexDirectory, analyzer, false);
                writer.addDocument(document);
                writer.optimize();
                writer.close();
            }

            //Code to delete Documents
            Hits objhits = null;
            IndexSearcher is = new
IndexSearcher(LuceneHandle.indexFields);
            Analyzer analyzer = new StandardAnalyzer();
            QueryParser parser = new
QueryParser("CatID", analyzer);
            Query query = parser.parse("13");
            objhits = is.search(query);

            IndexReader ir =
IndexReader.open(LuceneHandle.indexFields);
            for(int i=0;i<objhits.length();i++)
            {
                 Document doc = objhits.doc(i);

                 String [] strtemp =
doc.getValues("CatID");

                 if(strtemp!=null)
                 for(int tempcount
=0;tempcount<strtemp.length;tempcount++)
                  {  
                    Term uidTerm = new Term("CatID",
"13");
                    int DelCount = ir.delete(uidTerm);
                 }
            }


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: How to delete multiple documents

Peter Keegan
srinivas,

Create your IndexWriter and Analyzer once before your loop.
I suggest you follow up any questions to the 'java-user' list.

Peter

On Thu, 21 Feb 2008 13:14:21 -00, [hidden email] <[hidden email]>
wrote:

>
> In the Following Code if i insert one document i was
> able to delete but when i insert multiple documents i
> was unable to delete.
>
> please follow the code and advice me
>
> thanks
> srinvias.
>
>
>
>            // Code to index Documents
>            for (int count = 1; count < 10; count++)
>            {
>                String CatID = "13";
>                String FieldName = "Field-" + count;
>
>                document.add(Field.Text("FieldName",
> FieldName));
>                document.add(Field.Keyword("CatID",
> CatID));
>                Analyzer analyzer = new
> StandardAnalyzer();
>                IndexWriter writer = new
> IndexWriter(indexDirectory, analyzer, false);
>                writer.addDocument(document);
>                writer.optimize();
>                writer.close();
>            }
>
>            //Code to delete Documents
>            Hits objhits = null;
>            IndexSearcher is = new
> IndexSearcher(LuceneHandle.indexFields);
>            Analyzer analyzer = new StandardAnalyzer();
>            QueryParser parser = new
> QueryParser("CatID", analyzer);
>            Query query = parser.parse("13");
>            objhits = is.search(query);
>
>            IndexReader ir =
> IndexReader.open(LuceneHandle.indexFields);
>            for(int i=0;i<objhits.length();i++)
>            {
>                 Document doc = objhits.doc(i);
>
>                 String [] strtemp =
> doc.getValues("CatID");
>
>                 if(strtemp!=null)
>                 for(int tempcount
> =0;tempcount<strtemp.length;tempcount++)
>                  {
>                    Term uidTerm = new Term("CatID",
> "13");
>                    int DelCount = ir.delete(uidTerm);
>                 }
>            }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>