View Javadoc

1   /*
2    * Copyright (c) 2003 by Atlassian Software Systems Pty. Ltd.
3    * All rights reserved.
4    */
5   package com.atlassian.bonnie.search;
6   
7   import com.atlassian.bonnie.Indexer;
8   import com.atlassian.bonnie.Searchable;
9   import com.atlassian.bonnie.LuceneConnection;
10  import com.atlassian.bonnie.search.DocumentBuilder;
11  import org.apache.log4j.Category;
12  import org.apache.log4j.MDC;
13  import org.apache.lucene.index.IndexReader;
14  import org.apache.lucene.index.IndexWriter;
15  import org.apache.lucene.index.Term;
16  
17  import java.io.IOException;
18  
19  /**
20   * This Indexer is responsible for indexing objects to Lucene.
21   */
22  public class LuceneIndexer implements Indexer
23  {
24      public static final Category LOG = Category.getInstance(LuceneIndexer.class);
25  
26      private LuceneConnection luceneConnection;
27      private DocumentBuilder documentBuilder;
28  
29      public void index(final Searchable obj)
30      {
31          if (!obj.isIndexable())
32          {
33              return;
34          }
35  
36          unIndex(obj);
37  
38          luceneConnection.withWriter(new LuceneConnection.WriterAction()
39          {
40              public void perform(IndexWriter indexWriter) throws IOException
41              {
42                  try
43                  {
44                      MDC.put("Indexing", obj.toString());
45                      indexWriter.addDocument(documentBuilder.getDocument(obj));
46                  }
47                  finally
48                  {
49                      MDC.remove("Indexing");
50                  }
51              }
52          });
53      }
54  
55      public void unIndex(final Searchable obj)
56      {
57          luceneConnection.withReaderAndDeletes(new LuceneConnection.ReaderAction()
58          {
59              public Object perform(IndexReader indexReader) throws IOException
60              {
61                  try
62                  {
63                      MDC.put("Unindexing", obj.toString());
64                      Term t = new Term(DocumentBuilder.HANDLE_FIELD_NAME, documentBuilder.getHandleAttributeStringValue(obj));
65                      indexReader.deleteDocuments(t);
66                      return null;
67                  }
68                  finally
69                  {
70                      MDC.remove("Unindexing");
71                  }
72              }
73          });
74      }
75  
76      public void reIndex(Searchable obj)
77      {
78          unIndex(obj);
79          index(obj);
80      }
81  
82      public void unIndexAll()
83      {
84          try
85          {
86              luceneConnection.truncateIndex();
87          }
88          catch (IOException e)
89          {
90              LOG.error("", e);
91          }
92      }
93  
94      public void setLuceneConnection(LuceneConnection luceneConnection)
95      {
96          this.luceneConnection = luceneConnection;
97      }
98  
99      public void setDocumentBuilder(DocumentBuilder documentBuilder)
100     {
101         this.documentBuilder = documentBuilder;
102     }
103 }