1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.directory.service.directorysearch;
35
36 import java.io.IOException;
37 import java.nio.file.Paths;
38
39 import org.apache.commons.lang3.mutable.MutableBoolean;
40 import org.apache.lucene.analysis.Analyzer;
41 import org.apache.lucene.index.DirectoryReader;
42 import org.apache.lucene.index.IndexWriter;
43 import org.apache.lucene.index.IndexWriterConfig;
44 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
45 import org.apache.lucene.search.IndexSearcher;
46 import org.apache.lucene.store.Directory;
47 import org.apache.lucene.store.NIOFSDirectory;
48
49 import fr.paris.lutece.plugins.lucene.service.analyzer.LuteceFrenchAnalyzer;
50 import fr.paris.lutece.portal.service.util.AppLogService;
51 import fr.paris.lutece.portal.service.util.AppPathService;
52 import fr.paris.lutece.portal.service.util.AppPropertiesService;
53
54
55
56
57 public class DirectorySearchFactory
58 {
59
60 private static final String PATH_INDEX = "directory.internalIndexer.lucene.indexPath";
61 private static final String PROPERTY_ANALYSER_CLASS_NAME = "directory.internalIndexer.lucene.analyser.className";
62
63
64 private Analyzer _analyzer;
65
66
67
68
69 private DirectorySearchFactory( )
70 {
71 try
72 {
73 String strAnalyserClassName = AppPropertiesService.getProperty( PROPERTY_ANALYSER_CLASS_NAME );
74 _analyzer = (Analyzer) Class.forName( strAnalyserClassName ).newInstance( );
75 }
76 catch( InstantiationException | IllegalAccessException | ClassNotFoundException exception )
77 {
78 AppLogService.error( "Failed to instanciate the analyzer of the type: " + PROPERTY_ANALYSER_CLASS_NAME );
79
80 _analyzer = new LuteceFrenchAnalyzer( );
81 }
82 }
83
84
85
86
87
88
89 public static DirectorySearchFactory getInstance( )
90 {
91 return DirectorySearchFactoryHolder._instance;
92 }
93
94
95
96
97
98
99 public Analyzer getAnalyzer( )
100 {
101 return _analyzer;
102 }
103
104
105
106
107
108
109
110
111 public IndexSearcher getIndexSearcher( ) throws IOException
112 {
113 Directory luceneDirectory = getDirectory( );
114
115 return new IndexSearcher( DirectoryReader.open( luceneDirectory ) );
116 }
117
118
119
120
121
122
123
124
125
126
127 public IndexWriter getIndexWriter( MutableBoolean bCreateIndex ) throws IOException
128 {
129 Directory luceneDirectory = getDirectory( );
130
131 if ( !DirectoryReader.indexExists( luceneDirectory ) )
132 {
133 bCreateIndex.setValue( Boolean.TRUE );
134 }
135
136 IndexWriterConfig conf = new IndexWriterConfig( getAnalyzer( ) );
137
138 if ( bCreateIndex.isTrue( ) )
139 {
140 conf.setOpenMode( OpenMode.CREATE );
141 }
142 else
143 {
144 conf.setOpenMode( OpenMode.APPEND );
145 }
146
147 return new IndexWriter( luceneDirectory, conf );
148 }
149
150
151
152
153
154
155
156
157 private Directory getDirectory( ) throws IOException
158 {
159 String strIndex = AppPathService.getPath( PATH_INDEX );
160
161 return NIOFSDirectory.open( Paths.get( strIndex ) );
162 }
163
164
165
166
167 private static class DirectorySearchFactoryHolder
168 {
169 public static final DirectorySearchFactory _instance = new DirectorySearchFactory( );
170 }
171 }