1 |
7662
|
tao
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Copyright: 2000-2011 Regents of the University of California and the
|
4 |
|
|
* National Center for Ecological Analysis and Synthesis
|
5 |
|
|
*
|
6 |
|
|
* '$Author: leinfelder $'
|
7 |
|
|
* '$Date: 2012-11-29 16:52:29 -0800 (Thu, 29 Nov 2012) $'
|
8 |
|
|
*
|
9 |
|
|
* This program is free software; you can redistribute it and/or modify
|
10 |
|
|
* it under the terms of the GNU General Public License as published by
|
11 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
12 |
|
|
* (at your option) any later version.
|
13 |
|
|
*
|
14 |
|
|
* This program is distributed in the hope that it will be useful,
|
15 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17 |
|
|
* GNU General Public License for more details.
|
18 |
|
|
*
|
19 |
|
|
* You should have received a copy of the GNU General Public License
|
20 |
|
|
* along with this program; if not, write to the Free Software
|
21 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
22 |
|
|
*/
|
23 |
|
|
package edu.ucsb.nceas.metacat.index;
|
24 |
|
|
|
25 |
|
|
import java.io.File;
|
26 |
|
|
import java.io.FileInputStream;
|
27 |
|
|
import java.io.IOException;
|
28 |
|
|
import java.io.StringWriter;
|
29 |
|
|
import java.util.Collections;
|
30 |
|
|
import java.util.Comparator;
|
31 |
|
|
import java.util.HashMap;
|
32 |
|
|
import java.util.List;
|
33 |
|
|
import java.util.Map;
|
34 |
|
|
import java.util.Properties;
|
35 |
|
|
import java.util.Set;
|
36 |
|
|
|
37 |
7730
|
tao
|
import javax.xml.parsers.ParserConfigurationException;
|
38 |
7662
|
tao
|
|
39 |
|
|
|
40 |
7730
|
tao
|
|
41 |
7662
|
tao
|
import org.apache.commons.io.FileUtils;
|
42 |
|
|
import org.apache.commons.logging.Log;
|
43 |
|
|
import org.apache.commons.logging.LogFactory;
|
44 |
|
|
import org.apache.solr.common.util.XML;
|
45 |
|
|
import org.apache.solr.core.CoreContainer;
|
46 |
|
|
import org.apache.solr.core.SolrCore;
|
47 |
|
|
import org.apache.solr.schema.IndexSchema;
|
48 |
|
|
import org.apache.solr.schema.SchemaField;
|
49 |
7730
|
tao
|
import org.dataone.service.exceptions.NotFound;
|
50 |
|
|
import org.dataone.service.exceptions.UnsupportedType;
|
51 |
7662
|
tao
|
import org.dataone.service.types.v1_1.QueryEngineDescription;
|
52 |
|
|
import org.dataone.service.types.v1_1.QueryField;
|
53 |
7730
|
tao
|
import org.xml.sax.SAXException;
|
54 |
7662
|
tao
|
|
55 |
|
|
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
|
56 |
7772
|
tao
|
import edu.ucsb.nceas.metacat.common.query.EnabledQueryEngines;
|
57 |
7730
|
tao
|
import edu.ucsb.nceas.metacat.common.query.SolrQueryServiceController;
|
58 |
7662
|
tao
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
59 |
|
|
import edu.ucsb.nceas.utilities.FileUtil;
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
/**
|
63 |
|
|
* This class handles the request for getting the solr engine description.
|
64 |
|
|
* @author tao
|
65 |
|
|
*
|
66 |
|
|
*/
|
67 |
|
|
public class MetacatSolrEngineDescriptionHandler {
|
68 |
|
|
private static final String UNKNOWN = "Unknown";
|
69 |
|
|
private static final String DESCRIPTIONFILENAME= "solrQueryFieldDescriptions.properties";
|
70 |
7664
|
tao
|
private static final String HTTPSOLRSERVERSCHEMAURLPATH="/admin/file/?contentType=text/xml;charset=utf-8&file=schema.xml";
|
71 |
7662
|
tao
|
|
72 |
|
|
private static MetacatSolrEngineDescriptionHandler handler = null;
|
73 |
|
|
private static Log logger = LogFactory.getLog(MetacatSolrEngineDescriptionHandler.class);
|
74 |
|
|
private QueryEngineDescription qed = null;
|
75 |
|
|
|
76 |
|
|
/**
|
77 |
|
|
* Get an instance of the class.
|
78 |
|
|
* @return
|
79 |
|
|
*/
|
80 |
|
|
public static MetacatSolrEngineDescriptionHandler getInstance() throws Exception {
|
81 |
|
|
if(handler == null) {
|
82 |
|
|
handler = new MetacatSolrEngineDescriptionHandler();
|
83 |
|
|
}
|
84 |
|
|
return handler;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
/**
|
88 |
|
|
* Get the QueryEngineDescription
|
89 |
|
|
* @return
|
90 |
|
|
*/
|
91 |
|
|
public QueryEngineDescription getQueryEngineDescritpion() {
|
92 |
|
|
return qed;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
/*
|
96 |
|
|
* Constructor
|
97 |
|
|
*/
|
98 |
|
|
private MetacatSolrEngineDescriptionHandler() throws Exception {
|
99 |
7730
|
tao
|
/*CoreContainer container = SolrServerFactory.getCoreContainer();
|
100 |
7662
|
tao
|
if(container == null) {
|
101 |
7664
|
tao
|
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.");
|
102 |
7662
|
tao
|
}
|
103 |
|
|
String coreName = SolrServerFactory.getCollectionName();
|
104 |
|
|
if(container == null) {
|
105 |
|
|
throw new Exception("MetacatSolrEngineDescriptionHandler - The collection name should not be null. Please check the value of the property \"solr.collectionName\" in the metacat.properties file.");
|
106 |
|
|
}
|
107 |
|
|
SolrCore core = container.getCore(coreName);
|
108 |
|
|
if(core == null) {
|
109 |
|
|
throw new Exception("MetacatSolrEngineDescriptionHandler - Metacat can't find the SolrCore for the given name - "+SolrServerFactory.getCollectionName());
|
110 |
7730
|
tao
|
}*/
|
111 |
|
|
init();
|
112 |
7662
|
tao
|
}
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
7730
|
tao
|
private void init() throws IOException, UnsupportedType, NotFound, ParserConfigurationException, SAXException {
|
117 |
7662
|
tao
|
Map<String, String>fieldDescriptions = loadSchemaFieldDescriptions();
|
118 |
|
|
qed = new QueryEngineDescription();
|
119 |
7772
|
tao
|
qed.setName(EnabledQueryEngines.SOLRENGINE);
|
120 |
7662
|
tao
|
//setSchemaVersionFromPropertiesFile(qed);
|
121 |
|
|
setSolrVersion();
|
122 |
|
|
|
123 |
7730
|
tao
|
//IndexSchema schema = core.getSchema();
|
124 |
|
|
Map<String, SchemaField> fieldMap = SolrQueryServiceController.getInstance().getIndexSchemaFields();
|
125 |
7662
|
tao
|
for (SchemaField schemaField : fieldMap.values()) {
|
126 |
|
|
qed.addQueryField(createQueryFieldFromSchemaField(schemaField, fieldDescriptions));
|
127 |
|
|
}
|
128 |
|
|
Collections.sort(qed.getQueryFieldList(), new QueryFieldAlphaComparator());
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
/**
|
132 |
|
|
* Based on org.apache.solr.handler.admin.SystemInfoHandler.getLuceneInfo()
|
133 |
7730
|
tao
|
* @throws SAXException
|
134 |
|
|
* @throws IOException
|
135 |
|
|
* @throws ParserConfigurationException
|
136 |
|
|
* @throws NotFound
|
137 |
|
|
* @throws UnsupportedType
|
138 |
7662
|
tao
|
*/
|
139 |
7730
|
tao
|
private void setSolrVersion() throws UnsupportedType, NotFound, ParserConfigurationException, IOException, SAXException {
|
140 |
|
|
qed.setQueryEngineVersion(SolrQueryServiceController.getInstance().getSolrSpecVersion());
|
141 |
7662
|
tao
|
}
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
private Map<String, String> loadSchemaFieldDescriptions() throws IOException {
|
147 |
|
|
Map<String, String>fieldDescriptions = new HashMap<String, String>();
|
148 |
|
|
Properties descriptionProperties = new Properties();
|
149 |
|
|
String propertyFilePath = PropertyService.CONFIG_FILE_DIR + FileUtil.getFS() + DESCRIPTIONFILENAME;
|
150 |
|
|
//System.out.println("the input strema is ================= "+propertyFilePath);
|
151 |
|
|
descriptionProperties.load(new FileInputStream(new File(propertyFilePath)));
|
152 |
|
|
Set<Object> names = descriptionProperties.keySet();
|
153 |
|
|
for(Object nameObj : names) {
|
154 |
|
|
String name = (String) nameObj;
|
155 |
|
|
String description = (String) descriptionProperties.get(name);
|
156 |
|
|
fieldDescriptions.put(name, description);
|
157 |
|
|
}
|
158 |
|
|
return fieldDescriptions;
|
159 |
|
|
|
160 |
|
|
}
|
161 |
|
|
|
162 |
|
|
private QueryField createQueryFieldFromSchemaField(SchemaField field,
|
163 |
|
|
Map<String, String> fieldDescriptions) {
|
164 |
|
|
QueryField queryField = new QueryField();
|
165 |
|
|
queryField.setName(field.getName());
|
166 |
|
|
queryField.setType(field.getType().getTypeName());
|
167 |
|
|
String description = fieldDescriptions.get(field.getName());
|
168 |
|
|
if ( description != null && !description.trim().equals("")) {
|
169 |
|
|
queryField.addDescription(description);
|
170 |
|
|
}
|
171 |
|
|
queryField.setSearchable(field.indexed());
|
172 |
|
|
queryField.setReturnable(field.stored());
|
173 |
|
|
queryField.setMultivalued(field.multiValued());
|
174 |
|
|
queryField.setSortable(isSortable(field));
|
175 |
|
|
return queryField;
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
private boolean isSortable(SchemaField field) {
|
179 |
|
|
String type = field.getType().getTypeName();
|
180 |
|
|
if ("int".equals(type) || "long".equals(type) || "float".equals(type)
|
181 |
|
|
|| "double".equals(type)) {
|
182 |
|
|
return false;
|
183 |
|
|
} else {
|
184 |
|
|
return true;
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
private class QueryFieldAlphaComparator implements Comparator<QueryField> {
|
189 |
|
|
public int compare(QueryField arg0, QueryField arg1) {
|
190 |
|
|
String field1Name = arg0.getName();
|
191 |
|
|
String field2Name = arg1.getName();
|
192 |
|
|
return field1Name.compareToIgnoreCase(field2Name);
|
193 |
|
|
}
|
194 |
|
|
}
|
195 |
|
|
}
|