Project

General

Profile

« Previous | Next » 

Revision 7609

Added by Jing Tao about 11 years ago

Add a class to handle solr query.

View differences:

src/edu/ucsb/nceas/metacat/index/MetacatSolrIndex.java
1
/**
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.ByteArrayInputStream;
26
import java.io.File;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.StringWriter;
30
import java.io.Writer;
31

  
32
import org.apache.commons.logging.Log;
33
import org.apache.commons.logging.LogFactory;
34
import org.apache.solr.client.solrj.SolrServer;
35
import org.apache.solr.client.solrj.SolrServerException;
36
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
37
import org.apache.solr.client.solrj.response.QueryResponse;
38
import org.apache.solr.common.params.SolrParams;
39
import org.apache.solr.core.CoreContainer;
40
import org.apache.solr.core.SolrCore;
41
import org.apache.solr.request.LocalSolrQueryRequest;
42
import org.apache.solr.response.CSVResponseWriter;
43
import org.apache.solr.response.JSONResponseWriter;
44
import org.apache.solr.response.PHPResponseWriter;
45
import org.apache.solr.response.PHPSerializedResponseWriter;
46
import org.apache.solr.response.PythonResponseWriter;
47
import org.apache.solr.response.QueryResponseWriter;
48
import org.apache.solr.response.RubyResponseWriter;
49
import org.apache.solr.response.SolrQueryResponse;
50
import org.apache.solr.response.XMLResponseWriter;
51
import org.apache.solr.servlet.SolrRequestParsers;
52
import org.dataone.configuration.Settings;
53

  
54
import edu.ucsb.nceas.metacat.MetaCatServlet;
55

  
56

  
57
/**
58
 * This class will query the solr server and return the result.
59
 * @author tao
60
 *
61
 */
62
public class MetacatSolrIndex {
63
    
64
    public static final String SOLR_HOME_PROPERTY_NAME = "solr.homeDir";
65
    public static final String SOLR_CONFIG_FILE_NAME_PROPERTY_NAME = "solr.configFileName";
66
    public static final String SOLR_COLLECTION_NAME_PROPERTY_NAME = "solr.collectionName";
67
    public static final String SOLR_SERVER_CLASSNAME_PROPERTY_NAME = "solr.server.classname";
68
    private static final String WT = "wt";//the property name to specify the return type
69
    private static final String XML = "xml";
70
    private static final String JSON = "json";
71
    private static final String PYTHON = "python";
72
    private static final String RUBY = "ruby";
73
    private static final String PHP = "php";
74
    private static final String PHPS = "phps";
75
    private static final String VELOCITY = "velocity";
76
    private static final String CSV ="csv";
77
    
78
    private static Log log = LogFactory.getLog(MetacatSolrIndex.class);
79
    private CoreContainer coreContainer = null;
80
    private SolrServer solrServer = null;
81
    private String wt = null;//specify the return format.
82
    private String collectionName = null;
83
    
84
    /**
85
     * Constructor
86
     */
87
    public MetacatSolrIndex() throws Exception {
88
        generateEmbeddedServer();
89
    }
90
    
91
    
92
    /*
93
     * Generate the embedded solr server
94
     */
95
    private void generateEmbeddedServer() throws Exception {
96
        String solrHomeDir = Settings.getConfiguration().getString(SOLR_HOME_PROPERTY_NAME);
97
        log.info("The configured solr home from properties is " + solrHomeDir);
98
        String configFileName = Settings.getConfiguration().getString(SOLR_CONFIG_FILE_NAME_PROPERTY_NAME);
99
        File configFile = new File(solrHomeDir, configFileName);
100
        coreContainer = new CoreContainer(solrHomeDir, configFile);
101
        coreContainer.load(solrHomeDir, configFile);
102
        collectionName = Settings.getConfiguration().getString(SOLR_COLLECTION_NAME_PROPERTY_NAME);
103
        solrServer = new EmbeddedSolrServer(coreContainer, collectionName);
104
    }
105
    
106
    /**
107
     * Query the solr server
108
     * @param query  the solr query
109
     * @return the result as the InputStream
110
     * @throws SolrServerException 
111
     */
112
    public InputStream query(String query) throws SolrServerException, IOException {
113
        InputStream inputStream = null;
114
        SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
115
        wt = solrParams.get(WT);
116
        QueryResponse response = solrServer.query(solrParams);
117
        inputStream = transformResults(solrParams, response);
118
        return inputStream;
119
    }
120
    
121
   
122
    /*
123
     * Transform the Queryresponse to the InputStream
124
     */
125
    private InputStream transformResults(SolrParams request, QueryResponse response) throws SolrServerException, IOException {
126
        //InputStream stream = null;
127
        QueryResponseWriter writer = generateResponseWriter();
128
        Writer results = new StringWriter();
129
        SolrQueryResponse sResponse = new SolrQueryResponse();
130
        sResponse.setAllValues(response.getResponse());
131
        SolrCore core =coreContainer.getCore(collectionName);
132
        writer.write(results, new LocalSolrQueryRequest(core, request), sResponse);
133
        return new ByteArrayInputStream(results.toString().getBytes(MetaCatServlet.DEFAULT_ENCODING));
134
    }
135
    
136
    
137
    /* Here is the list of the handler class to handle different format.
138
     * <queryResponseWriter name="xml" default="true" class="solr.XMLResponseWriter" />
139
     * <queryResponseWriter name="json" class="solr.JSONResponseWriter"/>
140
     * <queryResponseWriter name="python" class="solr.PythonResponseWriter"/>
141
     * <queryResponseWriter name="ruby" class="solr.RubyResponseWriter"/>
142
     * <queryResponseWriter name="php" class="solr.PHPResponseWriter"/>
143
     * <queryResponseWriter name="phps" class="solr.PHPSerializedResponseWriter"/>
144
     * <queryResponseWriter name="velocity" class="solr.VelocityResponseWriter"/>
145
     * <queryResponseWriter name="csv" class="solr.CSVResponseWriter"/>
146
     */
147
    private QueryResponseWriter generateResponseWriter() throws SolrServerException {
148
        QueryResponseWriter writer = null;
149
        if(wt == null || wt.trim().equals("") || wt.equals(XML)) {
150
            writer = new XMLResponseWriter();
151
        } else if(wt.equals(JSON)) {
152
            writer = new JSONResponseWriter();
153
        } else if(wt.equals(PYTHON)) {
154
            writer = new PythonResponseWriter();
155
        } else if(wt.equals(RUBY)) {
156
            writer = new RubyResponseWriter();
157
        } else if(wt.equals(PHP)) {
158
            writer = new PHPResponseWriter();
159
        } else if(wt.equals(PHPS)) {
160
            writer = new PHPSerializedResponseWriter();
161
        /*} else if(wt.equals(VELOCITY)) {
162
            writer = new VelocityResponseWriter();*/
163
        } else if(wt.equals(CSV)) {
164
            writer = new CSVResponseWriter();
165
        } else {
166
            throw new SolrServerException("Metacat doesn't support this response format "+wt);
167
        }
168
        return writer;
169
    }
170
}

Also available in: Unified diff