On 02/24/2011 12:16 PM, Devangini wrote:
> Can you please show me how an http implementation of solrj querying can be
> converted to one for embedded solr with the help of an example?
Hi, heres an example that almost compiles. You should be able to get
going with this.
T

class EmbeddedSolrExample {

    public static void main (String[] args) {

        setupSolrContainer();
        addDocument();
       


    SolrDocumentList getResults(QueryResponse response) {
        if (response.getStatus() != 0) {
            return new SolrDocumentList();
        }
        return response.getResults();
    }
    private void addDocument() throws IncompleteDocumentException,
SolrServerException, IOException {

        SolrInputDocument res = new SolrInputDocument();
        res.setField("body", "test");
res.setField("id", 12);


        UpdateResponse s = server.add(res);
        assertEquals((int) s.getStatus(), 0 );
        server.commit();

        SolrDocumentList res = getResults(search("test"));
        System.out.println("I got " + res.size() + " documents");

    }

    private void setupSolrContainer() throws
ParserConfigurationException, IOException, SAXException,
            IncompleteDocumentException, SolrServerException {
        File home = new File("/tmp/solr");
        File f = new File(home, "solr.xml");
        CoreContainer container = new CoreContainer();
        container.load("/tmp/solr", f);

        server = new EmbeddedSolrServer(container, "model");
        addDocument();
    }

    QueryResponse search(String words) throws SolrServerException {
        SolrQuery query = new SolrQuery();
        query.addField("id").addField("body").addField("score");
        query.setTimeAllowed(1000);
        query.setRows(50);
        query.set("q", words);
        query.setSortField("timestamp", ORDER.desc); // sorter på dato
        return server.query(query);

    }

-- 
Regards / Med vennlig hilsen
Tarjei Huse
Mobil: 920 63 413

Reply via email to