Project

General

Profile

« Previous | Next » 

Revision 7634

Added by Jing Tao over 11 years ago

Add code to handle the solr index information. we still need to figure out how to get the information.

View differences:

src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
143 143
    implements MNAuthorization, MNCore, MNRead, MNReplication, MNStorage, MNQuery {
144 144

  
145 145
    private static final String PATHQUERY = "pathquery";
146
    public static final String SOLRQUERY = "solr";
147 146
	private static final String UUID_SCHEME = "UUID";
148 147
	private static final String DOI_SCHEME = "DOI";
149 148
	private static final String UUID_PREFIX = "urn:uuid:";
......
1407 1406
	public QueryEngineDescription getQueryEngineDescription(String engine)
1408 1407
			throws InvalidToken, ServiceFailure, NotAuthorized, NotImplemented,
1409 1408
			NotFound {
1410
		QueryEngineDescription qed = new QueryEngineDescription();
1411
		qed.setName(PATHQUERY);
1412
		qed.setQueryEngineVersion("1.0");
1413
		qed.addAdditionalInfo("This is the traditional structured query for Metacat");
1414
		Vector<String> pathsForIndexing = null;
1415
		try {
1416
			pathsForIndexing = SystemUtil.getPathsForIndexing();
1417
		} catch (MetacatUtilException e) {
1418
			logMetacat.warn("Could not get index paths", e);
1419
		}
1420
		for (String fieldName: pathsForIndexing) {
1421
			QueryField field = new QueryField();
1422
			field.addDescription("Indexed field for path '" + fieldName + "'");
1423
			field.setName(fieldName);
1424
			field.setReturnable(true);
1425
			field.setSearchable(true);
1426
			field.setSortable(false);
1427
			// TODO: determine type and multivaluedness
1428
			field.setType(String.class.getName());
1429
			//field.setMultivalued(true);
1430
			qed.addQueryField(field);
1431
		}
1432
		return qed;
1409
	    if(engine != null && engine.equals(PATHQUERY)) {
1410
	        QueryEngineDescription qed = new QueryEngineDescription();
1411
	        qed.setName(PATHQUERY);
1412
	        qed.setQueryEngineVersion("1.0");
1413
	        qed.addAdditionalInfo("This is the traditional structured query for Metacat");
1414
	        Vector<String> pathsForIndexing = null;
1415
	        try {
1416
	            pathsForIndexing = SystemUtil.getPathsForIndexing();
1417
	        } catch (MetacatUtilException e) {
1418
	            logMetacat.warn("Could not get index paths", e);
1419
	        }
1420
	        for (String fieldName: pathsForIndexing) {
1421
	            QueryField field = new QueryField();
1422
	            field.addDescription("Indexed field for path '" + fieldName + "'");
1423
	            field.setName(fieldName);
1424
	            field.setReturnable(true);
1425
	            field.setSearchable(true);
1426
	            field.setSortable(false);
1427
	            // TODO: determine type and multivaluedness
1428
	            field.setType(String.class.getName());
1429
	            //field.setMultivalued(true);
1430
	            qed.addQueryField(field);
1431
	        }
1432
	        return qed;
1433
	    } else if (engine != null && engine.equals(MetacatSolrIndex.SOLRQUERY)) {
1434
	        try {
1435
	            QueryEngineDescription qed = MetacatSolrIndex.getInstance().getQueryEngineDescription();
1436
	            return qed;
1437
	        } catch (Exception e) {
1438
	            e.printStackTrace();
1439
	            throw new ServiceFailure("Solr server error", e.getMessage());
1440
	        }
1441
	    } else {
1442
	        throw new NotFound("404", "The Metacat member node can't find the query engine - "+engine);
1443
	    }
1444
		
1433 1445
	}
1434 1446

  
1435 1447
	@Override
......
1438 1450
		QueryEngineList qel = new QueryEngineList();
1439 1451
		// support pathquery initially
1440 1452
		qel.addQueryEngine(PATHQUERY);
1441
		qel.addQueryEngine(SOLRQUERY);
1453
		qel.addQueryEngine(MetacatSolrIndex.SOLRQUERY);
1442 1454
		return qel;
1443 1455
	}
1444 1456

  
......
1469 1481
				
1470 1482
			}
1471 1483
			
1472
		} else if (engine != null && engine.equals(SOLRQUERY)) {
1473
		    System.out.println("The query is ==================================== \n"+query);
1484
		} else if (engine != null && engine.equals(MetacatSolrIndex.SOLRQUERY)) {
1485
		    logMetacat.info("The query is ==================================== \n"+query);
1474 1486
		    try {
1475
		        MetacatSolrIndex solrIndex = new MetacatSolrIndex();
1476
                return solrIndex.query(query);
1487
		        
1488
                return MetacatSolrIndex.getInstance().query(query);
1477 1489
            } catch (Exception e) {
1478 1490
                // TODO Auto-generated catch block
1479 1491
                throw new ServiceFailure("Solr server error", e.getMessage());
src/edu/ucsb/nceas/metacat/index/MetacatSolrIndex.java
28 28
import java.io.InputStream;
29 29
import java.io.StringWriter;
30 30
import java.io.Writer;
31
import java.util.Iterator;
32
import java.util.Map;
33
import java.util.Vector;
31 34

  
32 35
import javax.xml.parsers.ParserConfigurationException;
33 36

  
34 37
import org.apache.commons.logging.Log;
35 38
import org.apache.commons.logging.LogFactory;
39
import org.apache.lucene.index.IndexReader;
36 40
import org.apache.solr.client.solrj.SolrServer;
37 41
import org.apache.solr.client.solrj.SolrServerException;
38 42
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
39 43
import org.apache.solr.client.solrj.response.QueryResponse;
40 44
import org.apache.solr.common.params.SolrParams;
45
import org.apache.solr.common.util.SimpleOrderedMap;
41 46
import org.apache.solr.core.CoreContainer;
42 47
import org.apache.solr.core.SolrCore;
48
import org.apache.solr.core.StandardDirectoryFactory;
49
import org.apache.solr.core.StandardIndexReaderFactory;
50
import org.apache.solr.handler.admin.LukeRequestHandler;
43 51
import org.apache.solr.request.LocalSolrQueryRequest;
44 52
import org.apache.solr.response.CSVResponseWriter;
45 53
import org.apache.solr.response.JSONResponseWriter;
......
52 60
import org.apache.solr.response.XMLResponseWriter;
53 61
import org.apache.solr.servlet.SolrRequestParsers;
54 62
import org.dataone.configuration.Settings;
63
import org.dataone.service.types.v1_1.QueryEngineDescription;
64
import org.dataone.service.types.v1_1.QueryField;
55 65
import org.xml.sax.SAXException;
56 66

  
57 67
import edu.ucsb.nceas.metacat.MetaCatServlet;
......
64 74
 */
65 75
public class MetacatSolrIndex {
66 76
    
77
    
78
    public static final String SOLRQUERY = "solr";
67 79
    public static final String SOLR_HOME_PROPERTY_NAME = "solr.homeDir";
68 80
    public static final String SOLR_CONFIG_FILE_NAME_PROPERTY_NAME = "solr.configFileName";
69 81
    public static final String SOLR_COLLECTION_NAME_PROPERTY_NAME = "solr.collectionName";
......
77 89
    private static final String PHPS = "phps";
78 90
    private static final String VELOCITY = "velocity";
79 91
    private static final String CSV ="csv";
92
    private static final String VERSION = "3.4";
80 93
    
81 94
    private static Log log = LogFactory.getLog(MetacatSolrIndex.class);
82 95
    private CoreContainer coreContainer = null;
83 96
    private SolrServer solrServer = null;
84 97
    private String wt = null;//specify the return format.
85 98
    private String collectionName = null;
99
    private String solrHomeDir = null;
100
    private static MetacatSolrIndex  solrIndex = null;
86 101
    
102
    public static MetacatSolrIndex getInstance() throws ParserConfigurationException, IOException, SAXException {
103
        if (solrIndex == null) {
104
            solrIndex = new MetacatSolrIndex();
105
        }
106
        return solrIndex;
107
    }
108
    
87 109
    /**
88 110
     * Constructor
89 111
     * @throws SAXException 
90 112
     * @throws IOException 
91 113
     * @throws ParserConfigurationException 
92 114
     */
93
    public MetacatSolrIndex() throws ParserConfigurationException, IOException, SAXException {
115
    private MetacatSolrIndex() throws ParserConfigurationException, IOException, SAXException {
94 116
        generateEmbeddedServer();
95 117
    }
96 118
    
......
99 121
     * Generate the embedded solr server
100 122
     */
101 123
    private void generateEmbeddedServer() throws ParserConfigurationException, IOException, SAXException {
102
        String solrHomeDir = Settings.getConfiguration().getString(SOLR_HOME_PROPERTY_NAME);
124
        solrHomeDir = Settings.getConfiguration().getString(SOLR_HOME_PROPERTY_NAME);
103 125
        log.info("The configured solr home from properties is " + solrHomeDir);
104 126
        String configFileName = Settings.getConfiguration().getString(SOLR_CONFIG_FILE_NAME_PROPERTY_NAME);
105 127
        File configFile = new File(solrHomeDir, configFileName);
......
173 195
        }
174 196
        return writer;
175 197
    }
198
    /**
199
     * Get the description of solr query engine. It returns the list of index.
200
     * @return
201
     * @throws IOException 
202
     */
203
    public  QueryEngineDescription getQueryEngineDescription() throws IOException {
204
        LukeRequestHandler handler = new LukeRequestHandler();
205
        QueryEngineDescription qed = new QueryEngineDescription();
206
        qed.setName(SOLRQUERY);
207
        qed.setQueryEngineVersion(handler.getVersion());
208
        //qed.addAdditionalInfo("This is the SOLR query for Metacat");
209
        qed.addAdditionalInfo(handler.getDescription());
210
        StandardIndexReaderFactory indexFactory = new StandardIndexReaderFactory();
211
        StandardDirectoryFactory directoryFactory = new StandardDirectoryFactory();
212
        IndexReader indexReader = indexFactory.newReader(directoryFactory.open(solrHomeDir+"/data/index"), true);
213
        SimpleOrderedMap<Object> indexMap = LukeRequestHandler.getIndexInfo(indexReader, false);
214
        Iterator<Map.Entry<String,Object>> iterator = indexMap.iterator();
215
        while(iterator.hasNext()) {
216
            Map.Entry<String, Object> index = iterator.next();
217
            String name = index.getKey();
218
            String value = index.getValue().toString();
219
            //System.out.println("the value is ================"+name);
220
            //System.out.println("the value is ================"+value);
221
            QueryField field = new QueryField();
222
            field.addDescription("Indexed field for path '" + name + "'");
223
            field.setName(name);
224
            field.setReturnable(true);
225
            field.setSearchable(true);
226
            field.setSortable(false);
227
            // TODO: determine type and multivaluedness
228
            field.setType(String.class.getName());
229
            //field.setMultivalued(true);
230
            qed.addQueryField(field);
231
        }
232
        return qed;
233
    }
176 234
}

Also available in: Unified diff