Example usage for org.apache.solr.client.solrj SolrQuery set

List of usage examples for org.apache.solr.client.solrj SolrQuery set

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery set.

Prototype

public ModifiableSolrParams set(String name, String... val) 

Source Link

Document

Replace any existing parameter with the given name.

Usage

From source file:com.sindicetech.siren.solr.qparser.TestConciseTreeQParser.java

License:Open Source License

@Test
public void testConciseTreeAttributeWildcard() throws IOException, SolrServerException, QueryNodeException {
    this.addJsonString("1", "concise-attribute-wildcard", "{ \"aaa\" :  { \"bbb\" : \"ccc\" } }");

    SolrQuery query = new SolrQuery();
    final ConciseQueryBuilder b = new ConciseQueryBuilder();
    query.setQuery(b.newNode("ccc").toString());
    query.setRequestHandler("tree");
    query.set("qf", "concise-attribute-wildcard");
    String[] results = this.search(query, ID_FIELD);
    assertEquals(1, results.length);/*from   w w w  .  jav a2s .c  om*/
}

From source file:com.sindicetech.siren.solr.qparser.TestConciseTreeQParser.java

License:Open Source License

@Test
public void testSpanConciseTreeQuery() throws IOException, SolrServerException, QueryNodeException {
    this.addJsonString("1", "concise", "{ \"aaa\" :  { \"bbb\" : \"ccc\", \"ddd\" : \"eee\" } }");

    SolrQuery query = new SolrQuery();
    final ConciseQueryBuilder b = new ConciseQueryBuilder();

    final TwigQuery twig = b.newTwig("aaa").with(b.newBoolean().with(b.newNode("ccc").setAttribute("bbb"))
            .with(b.newNode("eee").setAttribute("ddd")).setInOrder(false));

    query.setQuery(twig.toString());//from  w ww . j a v a 2 s. c  o m
    query.setRequestHandler("tree");
    query.set("qf", "concise");
    String[] results = this.search(query, ID_FIELD);
    assertEquals(1, results.length);
}

From source file:com.sindicetech.siren.solr.qparser.TestConciseTreeQParser.java

License:Open Source License

/**
 * Issue #66: The query parser applies lowercase the expanded terms
 *//*from   www  . j a v a 2s  .  c o  m*/
@Test
public void testPrefixQueryConciseTreeQuery() throws IOException, SolrServerException, QueryNodeException {
    this.addJsonString("1", "concise-keyword", "{ \"rdfs:label\" :  \"COM_GRANAGLIONE\" }");

    SolrQuery query = new SolrQuery();
    final ConciseQueryBuilder b = new ConciseQueryBuilder();
    query.setQuery(b.newNode("COM_*").setAttribute("rdfs:label").toString());
    query.setRequestHandler("tree");
    query.set("qf", "concise-keyword");
    String[] results = this.search(query, ID_FIELD);
    assertEquals(1, results.length);
}

From source file:com.sindicetech.siren.solr.qparser.TestConciseTreeQParser.java

License:Open Source License

@Test
public void testDebugConciseTreeQuery() throws IOException, SolrServerException, QueryNodeException {
    this.addJsonString("1", "concise",
            "{\"@graph\":[{\"@id\":\"http://dbpedia/Person/1\",\"http://www.w3.org/2000/01/rdf-schema#label\":\"Alfred Hitchkoc\",\"http://www.w3.org/2000/01/rdf-schema#seeAlso\":{\"@id\":\"http://dbpedia/Person/2\",\"http://www.w3.org/2000/01/rdf-schema#label\":\"Brad Pitt\"}},{\"@id\":\"http://dbpedia/Person/2\",\"http://www.w3.org/2000/01/rdf-schema#label\":\"Brad Pitt\"}]}");
    this.addJsonString("2", "concise",
            "{\"@graph\":[{\"http://www.w3.org/2000/01/rdf-schema#label\":\"Alfred Hitchkoc\",\"@id\":\"http://dbpedia/Person/1\",\"http://www.w3.org/2000/01/rdf-schema#seeAlso\":{\"@id\":\"http://dbpedia/Person/2\",\"http://www.w3.org/2000/01/rdf-schema#label\":\"Brad Pitt\"}},{\"@id\":\"http://dbpedia/Person/2\",\"http://www.w3.org/2000/01/rdf-schema#label\":\"Brad Pitt\"}]}");

    SolrQuery query = new SolrQuery();
    final ConciseQueryBuilder b = new ConciseQueryBuilder();

    //    String q = b.newTwig()
    //                .with(
    //      b.newTwig("http://www.w3.org/2000/01/rdf-schema#seeAlso")
    //       .with(b.newNode("'http://dbpedia/Person/2'").setAttribute("@id"))
    //                )
    //                .with(
    //      b.newNode("Alfred Hitchkoc").setAttribute("http://www.w3.org/2000/01/rdf-schema#label")
    //                ).toString();

    String q = b.newTwig()/*from   w w  w. j  av a 2s .com*/
            .with(b.newTwig("http://www.w3.org/2000/01/rdf-schema#seeAlso")
                    .with(b.newNode("'http://dbpedia/Person/2'").setAttribute("@id")), 1)
            .with(b.newNode("Alfred Hitchkoc").setAttribute("http://www.w3.org/2000/01/rdf-schema#label"), 1)
            .toString();

    query.setQuery(q);
    query.setRequestHandler("tree");
    query.set("qf", "concise");
    String[] results = this.search(query, ID_FIELD);
    assertEquals(2, results.length);
}

From source file:com.sindicetech.siren.solr.qparser.TestDefaultOperator.java

License:Open Source License

@Test
public void testQOpParameter() throws IOException, SolrServerException {
    this.addJsonString("1", "{ \"aaa\" : \"bbb ccc\" }");
    this.addJsonString("2", "{ \"aaa\" : \"bbb\" }");
    this.addJsonString("3", "{ \"aaa\" : \"ccc\" }");

    SolrQuery query = new SolrQuery();
    query.setQuery("bbb ccc");
    query.setRequestHandler("keyword");
    query.set(QueryParsing.OP, "OR");
    String[] results = this.search(query, URL_FIELD);
    // Default Operator = OR : all the documents should match
    assertEquals(3, results.length);//  ww w.  j  a  v  a 2 s  .c  om

    query = new SolrQuery();
    query.setQuery("{ \"node\" : { \"query\" : \"bbb ccc\" } }");
    query.setRequestHandler("tree");
    query.set(QueryParsing.OP, "OR");
    results = this.search(query, URL_FIELD);
    // Default Operator = OR : all the documents should match
    assertEquals(3, results.length);
}

From source file:com.sindicetech.siren.solr.response.TestSirenTransformer.java

License:Open Source License

@Test
public void testTransformer() throws SolrServerException, IOException {
    SolrInputDocument document = new SolrInputDocument();
    document.addField(ID_FIELD, "1");
    document.addField(JSON_FIELD, json);
    getWrapper().add(document);//from  w w  w  .  j a v  a 2s  .c  o m
    getWrapper().commit();

    String queryStr = "{" + "\"twig\" : { " + "\"root\" : \"director\"," + "\"child\" : [{\"occur\":\"MUST\","
            + "\"twig\": {" + "\"child\":[{\"occur\":\"MUST\"," + "\"twig\": {" + "\"root\":\"first_name\","
            + "\"child\":[" + "{\"occur\":\"MUST\"," + "\"node\" : { \"query\" : \"Clint\" } " + "}" + "]" + "}"
            + "}" + "]" + "}" + "}" + ", {\"occur\" : \"MUST\", \"variable\" : {}}" + "]" + "}" + "}";

    final SolrQuery query = new SolrQuery();
    query.setQuery(queryStr);
    query.setRequestHandler("tree");
    query.set(SirenParams.QF, JSON_FIELD);
    query.set("fl", "id,json,[sirenProjection]");
    final String[] results = getWrapper().search(query, JSON_FIELD);
    assertEquals(1, results.length);
    assertEquals("{\"director\":{\"last_name\":\"Eastwood\",\"first_name\":\"Clint\",\"birth_date\":\"1930\"}}",
            results[0]);
}

From source file:com.ssavr.solr.service.impl.IceCreamLocalServiceImpl.java

License:Open Source License

public List<IceCream> searchIceCreamsWithEDisMax(String name, String flavor, String text, String value,
        int start, int rows, String orderByCol, String orderByType) throws PortalException, SystemException {
    if (_log.isDebugEnabled()) {
        _log.debug("searchIceCreams()");
    }/*w  ww  .j av a2  s. c  om*/
    List<IceCream> iceCreams = new ArrayList<IceCream>();
    String activeServerURL = SolrUtil.getActiveSolrServer();
    try {
        CommonsHttpSolrServer server = new CommonsHttpSolrServer(activeServerURL);
        SolrQuery query = new SolrQuery();

        query.setQuery(getQuery(value));
        query.set("qf", "iceCreamName iceCreamFlavor");
        /*
         * List<String> filterQueries = getFilterQueries(name, flavor,
         * text); for (String fq : filterQueries) {
         * query.setFilterQueries(fq); }
         */

        query.setStart(start);
        query.setRows(rows);

        boolean enableFacet = true;
        query.setFacet(enableFacet);
        query.addFacetField("iceCreamName", "iceCreamFlavor");

        QueryResponse response = server.query(query);

        List<SolrResponse> searchResults = response.getBeans(SolrResponse.class);

        for (SolrResponse result : searchResults) {
            String iceCreamId = result.getEntryClassPK();
            IceCream iceCream = iceCreamPersistence.findByPrimaryKey(Long.parseLong(iceCreamId));
            iceCreams.add(iceCream);
        }
    } catch (MalformedURLException e) {
        if (_log.isDebugEnabled()) {
            _log.debug(e.getMessage());
        }
        throw new SystemException(e);
    } catch (SolrServerException e) {
        if (_log.isDebugEnabled()) {
            _log.debug(e.getMessage());
        }
        throw new SystemException(e);
    }

    return iceCreams;
}

From source file:com.streamsets.pipeline.solr.impl.Solr04TestUtil.java

License:Apache License

@SuppressWarnings("unchecked")
public List<Map<String, Object>> query(Map<String, String> q) throws Exception {
    SolrQuery parameters = new SolrQuery();
    for (Map.Entry<String, String> entry : q.entrySet()) {
        parameters.set(entry.getKey(), entry.getValue());
    }//from w  w w. jav  a2s .c om
    QueryResponse response = solrServer.query(parameters);

    List<SolrDocument> solrDocumentList = response.getResults();
    List<Map<String, Object>> result = new ArrayList();
    for (SolrDocument document : solrDocumentList) {
        result.add(document);
    }
    return result;
}

From source file:com.streamsets.pipeline.stage.destination.solr.TestSolrTarget.java

License:Apache License

@Test
public void testWriteRecords() throws Exception {
    Target target = createTarget();
    TargetRunner runner = new TargetRunner.Builder(SolrDTarget.class, target).build();
    try {//  ww  w . j a va 2 s .  co m
        SolrServer solrClient = getSolrServer();

        solrClient.ping();

        //delete all index
        solrClient.deleteByQuery("*:*");

        runner.runInit();
        List<Record> records = new ArrayList<>();

        Record record1 = RecordCreator.create();
        record1.set(Field.create(
                ImmutableMap.of("a", Field.create("Hello"), "b", Field.create("i"), "c", Field.create("t"))));

        Record record2 = RecordCreator.create();
        record2.set(Field.create(
                ImmutableMap.of("a", Field.create("Bye"), "b", Field.create("i"), "c", Field.create("t"))));

        records.add(record1);
        records.add(record2);

        runner.runWrite(records);
        Assert.assertTrue(runner.getErrorRecords().isEmpty());
        Assert.assertTrue(runner.getErrors().isEmpty());

        SolrQuery parameters = new SolrQuery();
        parameters.set("q", "name:Hello");
        QueryResponse queryResponse = solrClient.query(parameters);

        SolrDocumentList solrDocuments = queryResponse.getResults();
        Assert.assertEquals(1, solrDocuments.size());

        SolrDocument solrDocument = solrDocuments.get(0);
        String fieldAVal = (String) solrDocument.get("name");
        Assert.assertNotNull(fieldAVal);
        Assert.assertEquals("Hello", fieldAVal);

        String fieldBVal = (String) solrDocument.get("sku");
        Assert.assertNotNull(fieldBVal);
        Assert.assertEquals("i", fieldBVal);

        String fieldCVal = (String) solrDocument.get("manu");
        Assert.assertNotNull(fieldCVal);
        Assert.assertEquals("t", fieldCVal);

    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        runner.runDestroy();
    }
}

From source file:com.streamsets.pipeline.stage.destination.solr.TestSolrTarget.java

License:Apache License

@Test
public void testWriteRecordsOnErrorDiscard() throws Exception {
    Target target = createTarget();
    TargetRunner runner = new TargetRunner.Builder(SolrDTarget.class, target)
            .setOnRecordError(OnRecordError.DISCARD).build();
    try {/*from   ww  w  .  ja v a 2s  .  co m*/

        SolrServer solrClient = getSolrServer();

        //delete all index
        solrClient.deleteByQuery("*:*");

        runner.runInit();
        List<Record> records = new ArrayList<>();

        Record record1 = RecordCreator.create();
        record1.set(Field.create(ImmutableMap.of("nota", Field.create("Hello"), "b", Field.create("i1"), "c",
                Field.create("t1"))));

        Record record2 = RecordCreator.create();
        record2.set(Field.create(
                ImmutableMap.of("a", Field.create("Bye"), "b", Field.create("i2"), "c2", Field.create("t2"))));

        records.add(record1);
        records.add(record2);

        runner.runWrite(records);
        Assert.assertTrue(runner.getErrorRecords().isEmpty());
        Assert.assertTrue(runner.getErrors().isEmpty());

        SolrQuery parameters = new SolrQuery();
        parameters.set("q", "sku:i1");
        QueryResponse queryResponse = solrClient.query(parameters);

        SolrDocumentList solrDocuments = queryResponse.getResults();
        Assert.assertEquals(0, solrDocuments.size());

    } finally {
        runner.runDestroy();
    }
}