Project

General

Profile

« Previous | Next » 

Revision 7664

Added by Jing Tao over 11 years ago

Add code to handle query for the http solr server.

View differences:

src/edu/ucsb/nceas/metacat/index/MetacatSolrEngineDescriptionHandler.java
60 60
public class MetacatSolrEngineDescriptionHandler {
61 61
    private static final String UNKNOWN = "Unknown";
62 62
    private static final String DESCRIPTIONFILENAME= "solrQueryFieldDescriptions.properties";
63
    private static final String HTTPSOLRSERVERSCHEMAURLPATH="/admin/file/?contentType=text/xml;charset=utf-8&file=schema.xml";
63 64
    
64 65
    private static MetacatSolrEngineDescriptionHandler handler = null;
65 66
    private static Log logger = LogFactory.getLog(MetacatSolrEngineDescriptionHandler.class);
......
90 91
    private MetacatSolrEngineDescriptionHandler() throws Exception {
91 92
       CoreContainer container = SolrServerFactory.getCoreContainer();
92 93
       if(container == null) {
93
           throw new Exception("MetacatSolrEngineDescriptionHandler - The Solr Server is not configured as an EmbeddedSolrServer since Metacat can't find the CoreContainer");
94
           throw new Exception("MetacatSolrEngineDescriptionHandler - The Solr Server is not configured as an EmbeddedSolrServer and the EmbeddedSolrServer is the only SolrServer that the Metacat can provide the Query Engine Description.");
94 95
       }
95 96
       String coreName = SolrServerFactory.getCollectionName();
96 97
       if(container == null) {
src/edu/ucsb/nceas/metacat/index/MetacatSolrIndex.java
31 31
import java.io.OutputStreamWriter;
32 32
import java.io.StringWriter;
33 33
import java.io.Writer;
34
import java.net.MalformedURLException;
35
import java.net.URL;
34 36
import java.sql.SQLException;
35 37
import java.util.ArrayList;
36 38
import java.util.Collection;
......
46 48
import org.apache.solr.client.solrj.SolrServer;
47 49
import org.apache.solr.client.solrj.SolrServerException;
48 50
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
51
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
49 52
import org.apache.solr.client.solrj.response.QueryResponse;
50 53
import org.apache.solr.common.params.AppendedSolrParams;
51 54
import org.apache.solr.common.params.ModifiableSolrParams;
......
76 79

  
77 80
import edu.ucsb.nceas.metacat.DBTransform;
78 81
import edu.ucsb.nceas.metacat.MetaCatServlet;
82
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
79 83
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
80 84

  
81 85

  
......
118 122
    private SolrServer solrServer = null;
119 123
    //private String wt = null;//specify the return format.
120 124
    private String collectionName = null;
121
    private String solrHomeDir = null;
125
    private String solrBaseURL = null;
126
    private boolean isEmbeddedSolrServer = true;
122 127
    private static MetacatSolrIndex  solrIndex = null;
123 128
    
124
    public static MetacatSolrIndex getInstance() throws ParserConfigurationException, IOException, SAXException {
129
    public static MetacatSolrIndex getInstance() throws Exception {
125 130
        if (solrIndex == null) {
126 131
            solrIndex = new MetacatSolrIndex();
127 132
        }
......
134 139
     * @throws IOException 
135 140
     * @throws ParserConfigurationException 
136 141
     */
137
    private MetacatSolrIndex() throws ParserConfigurationException, IOException, SAXException {
142
    private MetacatSolrIndex() throws Exception {
138 143
    	// these are handled directly by solr
139 144
    	supportedWriterTypes = new ArrayList<String>();
140 145
    	supportedWriterTypes.add(CSV);
......
146 151
    	supportedWriterTypes.add(PYTHON);
147 152
    	supportedWriterTypes.add(XML);
148 153

  
149
        generateEmbeddedServer();
154
        generateSolrServer();
150 155
    }
151 156
    
152 157
    
153 158
    /*
154 159
     * Generate the embedded solr server
155 160
     */
156
    private void generateEmbeddedServer() throws ParserConfigurationException, IOException, SAXException {
157
        solrHomeDir = Settings.getConfiguration().getString(SOLR_HOME_PROPERTY_NAME);
158
        System.setProperty("solr.solr.home", solrHomeDir);
159
        log.info("The configured solr home from properties is " + solrHomeDir);
160
        String configFileName = Settings.getConfiguration().getString(SOLR_CONFIG_FILE_NAME_PROPERTY_NAME);
161
        File configFile = new File(solrHomeDir, configFileName);
162
        coreContainer = new CoreContainer(solrHomeDir, configFile);
163
        coreContainer.load(solrHomeDir, configFile);
164
        collectionName = Settings.getConfiguration().getString(SOLR_COLLECTION_NAME_PROPERTY_NAME);
165
        solrServer = new EmbeddedSolrServer(coreContainer, collectionName);
161
    private void generateSolrServer() throws Exception {
162
        solrServer = SolrServerFactory.createSolrServer();
163
        if(solrServer instanceof EmbeddedSolrServer) {
164
            isEmbeddedSolrServer = true;
165
            coreContainer = SolrServerFactory.getCoreContainer();
166
            collectionName = SolrServerFactory.getCollectionName();
167
        } else {
168
            isEmbeddedSolrServer = false;
169
            CommonsHttpSolrServer httpServer = (CommonsHttpSolrServer)solrServer;
170
            solrBaseURL = httpServer.getBaseURL();
171
        }
172
        
173
       
166 174
    }
167 175
    
168 176
    /**
......
175 183
     * @throws PropertyNotFoundException 
176 184
     */
177 185
    public InputStream query(String query, String user, String[] groups) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException {
186
        if(isEmbeddedSolrServer) {
187
            return queryEmbedded(query, user, groups);
188
        } else {
189
            return queryHttp(query, user, groups);
190
        }
191
    }
192
    /**
193
     * Query a solr embedded server
194
     * @param query  the solr query
195
     * @return the result as the InputStream
196
     * @throws SolrServerException 
197
     * @throws ClassNotFoundException 
198
     * @throws SQLException 
199
     * @throws PropertyNotFoundException 
200
     */
201
    private InputStream queryEmbedded(String query, String user, String[] groups) throws SolrServerException, IOException, PropertyNotFoundException, SQLException, ClassNotFoundException {
178 202
        InputStream inputStream = null;
179 203
        SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
180 204
        solrParams = appendAccessFilterParams(solrParams, user, groups);
......
224 248
        return inputStream;
225 249
    }
226 250
    
251
    /*
252
     * Query a http server. We directly build the url to send the http server.
253
     * The reason we don't use the method is:
254
     * QueryResponse response = solrServer.query(solrParams);
255
     * When we transform the QueryReponse object to the InputStream object, we need to have SolrCore object 
256
     * which is not available for the SolrHttpServer.
257
     * 
258
     */
259
    private InputStream queryHttp(String query, String user, String[] groups) throws IOException {
260
        StringBuffer accessFilter = generateAccessFilterParamsString(user, groups);
261
        if(accessFilter != null && accessFilter.length() != 0) {
262
            query = solrBaseURL+"/select?"+query+"&"+FILTERQUERY+"="+accessFilter.toString();
263
        }
264
        URL url = new URL(query);
265
        return url.openStream();
266
    }
227 267
    
268
    
228 269
    /*
229 270
     * Append the access filter query to the params
230 271
     */
231 272
    private SolrParams appendAccessFilterParams(SolrParams solrParams, String user, String[] groups) {
232 273
        SolrParams append = null;
233 274
        if(solrParams != null) {
234
            StringBuffer query = new StringBuffer();
235
            if (user != null && groups != null) {
275
            StringBuffer query = generateAccessFilterParamsString(user, groups);      
276
            if(query != null && query.length() != 0) {
277
                log.info("=================== fq query is "+query.toString());
278
                NamedList fq = new NamedList();
279
                fq.add(FILTERQUERY, query.toString());
280
                SolrParams fqParam = SolrParams.toSolrParams(fq);
281
                append = new AppendedSolrParams(solrParams, fqParam);
282
            } else {
283
                append = solrParams;
284
            }
285
        }
286
        return append;
287
    }
288
    
289
    private StringBuffer generateAccessFilterParamsString(String user, String[] groups) {
290
        StringBuffer query = new StringBuffer();
291
        if (user != null && groups != null) {
236 292
                query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+user+"\""+CLOSEPARENTHESE);
237 293
                for(int i=0; i<groups.length; i++) {
238 294
                    query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
239 295
                }
240
            } else if (user != null && groups == null) {
296
        } else if (user != null && groups == null) {
241 297
                query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+user+"\""+CLOSEPARENTHESE);
242
            } else if ( user == null && groups != null) {
298
        } else if ( user == null && groups != null) {
243 299
                for (int i=0; i<groups.length; i++) {
244 300
                    if(i==0) {
245 301
                        query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
......
247 303
                        query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+groups[i]+"\""+CLOSEPARENTHESE);
248 304
                    }
249 305
                }
250
            }
251
            if(query.length() != 0) {
252
                log.info("=================== fq query is "+query.toString());
253
                NamedList fq = new NamedList();
254
                fq.add(FILTERQUERY, query.toString());
255
                SolrParams fqParam = SolrParams.toSolrParams(fq);
256
                append = new AppendedSolrParams(solrParams, fqParam);
257
            } else {
258
                append = solrParams;
259
            }
260 306
        }
261
        return append;
307
        return query;
262 308
    }
263 309
    
264 310
   
......
310 356
        }
311 357
        return writer;
312 358
    }
313
    /**
314
     * Get the description of solr query engine. It returns the list of index.
315
     * @return
316
     * @throws IOException 
317
     * @throws SAXException 
318
     * @throws ParserConfigurationException 
319
     */
320
    public  QueryEngineDescription getQueryEngineDescription() throws IOException, ParserConfigurationException, SAXException {
321
        //LukeRequestHandler handler = new LukeRequestHandler();
322
        //System.out.println("the class path is =============\n"+System.getProperty("java.class.path"));
323
        QueryEngineDescription qed = new QueryEngineDescription();
324
        qed.setName(SOLRQUERY);
325
        qed.setQueryEngineVersion(VERSION);
326
        qed.addAdditionalInfo("This is the SOLR query for Metacat");        
327
        SolrConfig config = new SolrConfig();
328
        InputSource schemaSource = new InputSource(new FileInputStream(new File(solrHomeDir+SOLRSCHEMAFILEPATH)));
329
        IndexSchema indexSchema = new IndexSchema(config, "dataone", schemaSource);
330
        Map<String, SchemaField> fields = indexSchema.getFields();
331
        if(fields != null) {
332
            Collection<SchemaField> schemaFields = fields.values();
333
            if(schemaFields != null) {
334
                /*qed.addAdditionalInfo(handler.getDescription());
335
                StandardIndexReaderFactory indexFactory = new StandardIndexReaderFactory();
336
                StandardDirectoryFactory directoryFactory = new StandardDirectoryFactory();
337
                IndexReader indexReader = indexFactory.newReader(directoryFactory.open(solrHomeDir+"/conf/schema.xml"), true);
338
                SimpleOrderedMap<Object> indexMap = LukeRequestHandler.getIndexInfo(indexReader, false);
339
                Iterator<Map.Entry<String,Object>> iterator = indexMap.iterator();*/
340
                for(SchemaField schemaField : schemaFields) {
341
                    if(schemaField != null) {
342
                        QueryField field = new QueryField();
343
                        //field.addDescription("Indexed field for path '" + name + "'");
344
                        field.setName(schemaField.getName());
345
                        field.setReturnable(true);
346
                        field.setSearchable(true);
347
                        field.setSortable(true);
348
                        field.setMultivalued(schemaField.multiValued());
349
                        field.setType(schemaField.getType().getTypeName());
350
                        qed.addQueryField(field);
351

  
352
                    }
353
                }
354

  
355
            }
356
        }
357
        
358
        return qed;
359
    }
359
  
360 360
}

Also available in: Unified diff