Project

General

Profile

« Previous | Next » 

Revision 7635

Added by Jing Tao about 11 years ago

Add the code to read the index field information from the schema.xml.

View differences:

src/edu/ucsb/nceas/metacat/index/MetacatSolrIndex.java
24 24

  
25 25
import java.io.ByteArrayInputStream;
26 26
import java.io.File;
27
import java.io.FileInputStream;
27 28
import java.io.IOException;
28 29
import java.io.InputStream;
29 30
import java.io.StringWriter;
30 31
import java.io.Writer;
32
import java.util.Collection;
31 33
import java.util.Iterator;
32 34
import java.util.Map;
35
import java.util.Set;
33 36
import java.util.Vector;
34 37

  
35 38
import javax.xml.parsers.ParserConfigurationException;
......
44 47
import org.apache.solr.common.params.SolrParams;
45 48
import org.apache.solr.common.util.SimpleOrderedMap;
46 49
import org.apache.solr.core.CoreContainer;
50
import org.apache.solr.core.SolrConfig;
47 51
import org.apache.solr.core.SolrCore;
48 52
import org.apache.solr.core.StandardDirectoryFactory;
49 53
import org.apache.solr.core.StandardIndexReaderFactory;
......
58 62
import org.apache.solr.response.RubyResponseWriter;
59 63
import org.apache.solr.response.SolrQueryResponse;
60 64
import org.apache.solr.response.XMLResponseWriter;
65
import org.apache.solr.schema.IndexSchema;
66
import org.apache.solr.schema.SchemaField;
61 67
import org.apache.solr.servlet.SolrRequestParsers;
62 68
import org.dataone.configuration.Settings;
63 69
import org.dataone.service.types.v1_1.QueryEngineDescription;
64 70
import org.dataone.service.types.v1_1.QueryField;
71
import org.xml.sax.InputSource;
65 72
import org.xml.sax.SAXException;
66 73

  
67 74
import edu.ucsb.nceas.metacat.MetaCatServlet;
......
90 97
    private static final String VELOCITY = "velocity";
91 98
    private static final String CSV ="csv";
92 99
    private static final String VERSION = "3.4";
100
    private static final String SOLRCONFDIR = "/conf";
101
    private static final String SOLRSCHEMAFILEPATH = SOLRCONFDIR+"/schema.xml";
102
    //private static final String SOLRCONFIGPATH = "/conf/solrconfig.xml";
93 103
    
94 104
    private static Log log = LogFactory.getLog(MetacatSolrIndex.class);
95 105
    private CoreContainer coreContainer = null;
......
122 132
     */
123 133
    private void generateEmbeddedServer() throws ParserConfigurationException, IOException, SAXException {
124 134
        solrHomeDir = Settings.getConfiguration().getString(SOLR_HOME_PROPERTY_NAME);
135
        System.setProperty("solr.solr.home", solrHomeDir);
125 136
        log.info("The configured solr home from properties is " + solrHomeDir);
126 137
        String configFileName = Settings.getConfiguration().getString(SOLR_CONFIG_FILE_NAME_PROPERTY_NAME);
127 138
        File configFile = new File(solrHomeDir, configFileName);
......
199 210
     * Get the description of solr query engine. It returns the list of index.
200 211
     * @return
201 212
     * @throws IOException 
213
     * @throws SAXException 
214
     * @throws ParserConfigurationException 
202 215
     */
203
    public  QueryEngineDescription getQueryEngineDescription() throws IOException {
204
        LukeRequestHandler handler = new LukeRequestHandler();
216
    public  QueryEngineDescription getQueryEngineDescription() throws IOException, ParserConfigurationException, SAXException {
217
        //LukeRequestHandler handler = new LukeRequestHandler();
218
        //System.out.println("the class path is =============\n"+System.getProperty("java.class.path"));
205 219
        QueryEngineDescription qed = new QueryEngineDescription();
206 220
        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);
221
        qed.setQueryEngineVersion(VERSION);
222
        qed.addAdditionalInfo("This is the SOLR query for Metacat");        
223
        SolrConfig config = new SolrConfig();
224
        InputSource schemaSource = new InputSource(new FileInputStream(new File(solrHomeDir+SOLRSCHEMAFILEPATH)));
225
        IndexSchema indexSchema = new IndexSchema(config, "dataone", schemaSource);
226
        Map<String, SchemaField> fields = indexSchema.getFields();
227
        if(fields != null) {
228
            Collection<SchemaField> schemaFields = fields.values();
229
            if(schemaFields != null) {
230
                /*qed.addAdditionalInfo(handler.getDescription());
231
                StandardIndexReaderFactory indexFactory = new StandardIndexReaderFactory();
232
                StandardDirectoryFactory directoryFactory = new StandardDirectoryFactory();
233
                IndexReader indexReader = indexFactory.newReader(directoryFactory.open(solrHomeDir+"/conf/schema.xml"), true);
234
                SimpleOrderedMap<Object> indexMap = LukeRequestHandler.getIndexInfo(indexReader, false);
235
                Iterator<Map.Entry<String,Object>> iterator = indexMap.iterator();*/
236
                for(SchemaField schemaField : schemaFields) {
237
                    if(schemaField != null) {
238
                        QueryField field = new QueryField();
239
                        //field.addDescription("Indexed field for path '" + name + "'");
240
                        field.setName(schemaField.getName());
241
                        field.setReturnable(true);
242
                        field.setSearchable(true);
243
                        field.setSortable(true);
244
                        field.setMultivalued(schemaField.multiValued());
245
                        field.setType(schemaField.getType().getTypeName());
246
                        qed.addQueryField(field);
247

  
248
                    }
249
                }
250

  
251
            }
231 252
        }
253
        
232 254
        return qed;
233 255
    }
234 256
}

Also available in: Unified diff