Search in sources :

Example 6 with CommonsHttpSolrServer

use of org.apache.solr.client.solrj.impl.CommonsHttpSolrServer in project play-cookbook by spinscale.

the class SolrPlugin method getSearchServer.

public static SolrServer getSearchServer() {
    String url = Play.configuration.getProperty("solr.server", "http://localhost:8983/solr");
    CommonsHttpSolrServer server = null;
    try {
        server = new CommonsHttpSolrServer(url);
        server.setRequestWriter(new BinaryRequestWriter());
    } catch (MalformedURLException e) {
        Logger.error(e, "Problem creating solr server object: %s", e.getMessage());
    }
    return server;
}
Also used : CommonsHttpSolrServer(org.apache.solr.client.solrj.impl.CommonsHttpSolrServer) MalformedURLException(java.net.MalformedURLException) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter)

Example 7 with CommonsHttpSolrServer

use of org.apache.solr.client.solrj.impl.CommonsHttpSolrServer in project Solbase by Photobucket.

the class CSVFileImporter method process.

private static void process(String str, HTablePool hTablePool) {
    HTableInterface idMapTable = hTablePool.getTable("DocKeyIdMap");
    HTableInterface seqTable = hTablePool.getTable("Sequence");
    try {
        StringTokenizer tokenizer = new StringTokenizer(str, ",");
        if (tokenizer.countTokens() == 9) {
            String id = tokenizer.nextToken();
            String cat = tokenizer.nextToken();
            String name = tokenizer.nextToken();
            float price = Float.parseFloat(tokenizer.nextToken());
            String inStock = tokenizer.nextToken();
            String author_t = tokenizer.nextToken();
            String series_t = tokenizer.nextToken();
            String sequence_i = tokenizer.nextToken();
            String genre_s = tokenizer.nextToken();
            Get get = new Get(Bytes.toBytes(id));
            Result result = idMapTable.get(get);
            byte[] docId = result.getValue(Bytes.toBytes("docId"), Bytes.toBytes(""));
            int docNumber = 1;
            SolrInputDocument doc = new SolrInputDocument();
            if (docId != null) {
                // we've indexed this doc previously
                docNumber = Bytes.toInt(docId);
                doc.addField("edit", true);
            } else {
                docNumber = new Long(seqTable.incrementColumnValue(Bytes.toBytes("sequence"), Bytes.toBytes("id"), Bytes.toBytes(""), 1, true)).intValue();
            }
            doc.addField("docId", docNumber);
            doc.addField("global_uniq_id", id);
            doc.addField("cat", cat);
            doc.addField("name", name);
            // Solbase currently do not support float embedded field comparison
            doc.addField("price", new Integer((int) price).toString());
            doc.addField("inStock", inStock);
            doc.addField("author_t", author_t);
            doc.addField("series_t", series_t);
            doc.addField("sequence_i", sequence_i);
            doc.addField("genre_s", genre_s);
            // whether we want to store to hbase or not
            doc.addField("updateStore", true);
            CommonsHttpSolrServer solbaseServer = new CommonsHttpSolrServer("http://localhost:8080/solbase/books~0");
            solbaseServer.add(doc);
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SolrServerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        hTablePool.putTable(idMapTable);
    }
}
Also used : CommonsHttpSolrServer(org.apache.solr.client.solrj.impl.CommonsHttpSolrServer) MalformedURLException(java.net.MalformedURLException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) Result(org.apache.hadoop.hbase.client.Result) StringTokenizer(java.util.StringTokenizer) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Get(org.apache.hadoop.hbase.client.Get)

Example 8 with CommonsHttpSolrServer

use of org.apache.solr.client.solrj.impl.CommonsHttpSolrServer in project whirr by apache.

the class SolrServiceTest method testSolr.

@Test
public void testSolr() throws IOException, SolrServerException {
    Set<Cluster.Instance> instances = cluster.getInstancesMatching(role(SOLR_ROLE));
    for (Cluster.Instance instance : instances) {
        String publicIp = instance.getPublicIp();
        LOG.info("Adding a document to instance " + instance.getId() + " @ " + publicIp);
        CommonsHttpSolrServer solrServer = new CommonsHttpSolrServer(String.format("http://%s:%s/solr/core0", instance.getPublicHostName(), SOLR_PORT));
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("name", "Apache Whirr");
        doc.addField("inceptionYear", "2010");
        solrServer.add(doc);
        solrServer.commit();
        LOG.info("Committed document to instance " + instance.getId() + " @ " + publicIp);
        LOG.info("Performing a search on instance " + instance.getId() + " @ " + publicIp);
        SolrQuery query = new SolrQuery("name:whirr");
        QueryResponse response = solrServer.query(query);
        SolrDocumentList results = response.getResults();
        assertEquals("Search on instance " + instance.getId() + " did NOT return a document!", 1, results.size());
        SolrDocument resultDoc = results.get(0);
        assertEquals("name field on document of instance " + instance.getId() + " is incorrect", "Apache Whirr", resultDoc.get("name"));
    }
}
Also used : CommonsHttpSolrServer(org.apache.solr.client.solrj.impl.CommonsHttpSolrServer) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Cluster(org.apache.whirr.Cluster) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Aggregations

CommonsHttpSolrServer (org.apache.solr.client.solrj.impl.CommonsHttpSolrServer)8 SolrInputDocument (org.apache.solr.common.SolrInputDocument)4 MalformedURLException (java.net.MalformedURLException)3 SolrQuery (org.apache.solr.client.solrj.SolrQuery)3 SolrServerException (org.apache.solr.client.solrj.SolrServerException)3 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)3 SolrDocument (org.apache.solr.common.SolrDocument)3 SolrDocumentList (org.apache.solr.common.SolrDocumentList)3 IOException (java.io.IOException)2 BinaryRequestWriter (org.apache.solr.client.solrj.impl.BinaryRequestWriter)2 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 Get (org.apache.hadoop.hbase.client.Get)1 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)1 Result (org.apache.hadoop.hbase.client.Result)1 XMLResponseParser (org.apache.solr.client.solrj.impl.XMLResponseParser)1 Cluster (org.apache.whirr.Cluster)1 Before (org.junit.Before)1 Test (org.junit.Test)1