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

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

Introduction

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

Prototype

public SolrQuery() 

Source Link

Usage

From source file:com.sindicetech.siren.demo.ncpr.NCPRQuery.java

License:Open Source License

/**
 * A query that shows how to combine SIREn query with the Solr's facet feature
 * on a Solr field./*w  ww. j av a 2  s  . c  o  m*/
 */
private SolrQuery getDeviceControllerFacet() throws QueryNodeException {
    ConciseQueryBuilder b = new ConciseQueryBuilder();
    String q = b.newTwig("Connector").with(b.newNode("xsd:long([32 TO *])").setAttribute("RatedOutputCurrent"))
            .toString();
    final SolrQuery query = new SolrQuery();
    query.setQuery(q);
    query.addFacetField("DeviceController.OrganisationName");
    return query;
}

From source file:com.sindicetech.siren.demo.ncpr.NCPRQuery.java

License:Open Source License

/**
 * A query that shows how to use the nested query parameter to use Solr's
 * query parsers on Solr's fields.//from ww  w  . j av a 2  s  .  c  om
 */
private SolrQuery getNestedQuery() throws QueryNodeException {
    ConciseQueryBuilder b = new ConciseQueryBuilder();
    String q = b.newTwig("DeviceOwner").with(b.newNode("uri(www.sourcelondon.net)").setAttribute("Website"))
            .toString();
    final SolrQuery query = new SolrQuery();
    query.setQuery(q);
    query.setParam("nested", "{!lucene} ChargeDeviceName:university");
    return query;
}

From source file:com.sindicetech.siren.solr.analysis.TestASCIIFoldingExpansionFilterFactory.java

License:Open Source License

/**
 * SRN-96/* w w w . ja  va2 s. c o  m*/
 */
@Test
public void testASCIIFoldingExpansion() throws IOException, SolrServerException {
    this.addJsonString("1", " { \"value\" : \"cafe\" } ");
    this.addJsonString("2", " { \"value\" : \"caf\" } ");
    SolrQuery query = new SolrQuery();
    query.setQuery("cafe");
    query.setRequestHandler("keyword");
    query.setIncludeScore(true);

    // should match the two documents, with same score
    QueryResponse response = getWrapper().getServer().query(query);
    SolrDocumentList docList = response.getResults();
    assertEquals(2, docList.getNumFound());
    float score1 = (Float) docList.get(0).getFieldValue("score");
    float score2 = (Float) docList.get(1).getFieldValue("score");
    Assert.assertTrue("Score should be identical", score1 == score2);

    // should match the two documents, but should assign different score
    // id2 should receive better score than id1
    query = new SolrQuery();
    query.setQuery("caf");
    query.setRequestHandler("keyword");
    query.setIncludeScore(true);

    response = getWrapper().getServer().query(query);
    docList = response.getResults();
    assertEquals(2, docList.getNumFound());
    if (docList.get(0).getFieldValue("url").equals("id1")) {
        score1 = (Float) docList.get(0).getFieldValue("score");
        score2 = (Float) docList.get(1).getFieldValue("score");
    } else {
        score2 = (Float) docList.get(0).getFieldValue("score");
        score1 = (Float) docList.get(1).getFieldValue("score");
    }
    Assert.assertTrue("id2 should get higher score than id1", score1 < score2);
}

From source file:com.sindicetech.siren.solr.facet.TestSirenFacetProcessorFactory.java

License:Open Source License

public void testCustomDatatypeField() throws Exception {
    String json = "{\"rating\": {\"_datatype_\": \"http://www.w3.org/2001/XMLSchema#double\", \"_value_\":\"5.4\"}}";

    IndexSchema schema = h.getCore().getLatestSchema();

    SolrInputDocument d = processAdd("generate-facets-processor", doc(f("id", "1"), f("json", json)));
    assertNotNull(d);/*from www.j ava  2 s.  c o  m*/
    schema = h.getCore().getLatestSchema();
    assertNotNull(schema.getFieldOrNull("double.json.rating"));
    assertEquals("tdouble", schema.getFieldType("double.json.rating").getTypeName());
    assertTrue((5.4 - (double) d.getFieldValue("double.json.rating")) < 0.01);

    getWrapper().add(d); // add so that we can test facets by querying
    this.commit();

    SolrQuery q = new SolrQuery();
    q.setRequestHandler("keyword");
    q.setParam("nested", "{!lucene} *:*");
    q.setFacet(true);
    q.addFacetField("double.json.rating");
    QueryResponse r = getWrapper().getServer().query(q);
    // we know there is only one facet field with one value
    assertEquals(1, r.getFacetFields().get(0).getValues().get(0).getCount());

    json = "{\"rating\": {\"_datatype_\": \"http://www.w3.org/2001/XMLSchema#float\", \"_value_\":\"-8.4\"}}";
    schema = h.getCore().getLatestSchema();
    d = processAdd("generate-facets-processor", doc(f("id", "2"), f("json", json)));
    assertNotNull(d);
    schema = h.getCore().getLatestSchema();
    assertNotNull(schema.getFieldOrNull("double.json.rating"));
    assertEquals("tdouble", schema.getFieldType("double.json.rating").getTypeName());
    assertTrue((-8.4 + (double) d.getFieldValue("double.json.rating")) < 0.01);

    getWrapper().add(d); // add so that we can test facets by querying
    this.commit();

    r = getWrapper().getServer().query(q);

    // there is only one facet field with two different values each with a single count
    assertEquals(1, r.getFacetFields().get(0).getValues().get(0).getCount());
    assertEquals(1, r.getFacetFields().get(0).getValues().get(1).getCount());
}

From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java

License:Open Source License

/**
 * Check that a UUID is generated for JSON documents with no 'id' attribute.
 *///from ww  w. j a va2s  . c  om
@Test
public void testJsonWithNoId() throws IOException, SolrServerException {
    String input = "{ \"aaa\" :  \"bbb\" }";
    this.sendUpdateRequest(input);
    this.commit();

    SolrQuery query = new SolrQuery();
    query.setParam("nested", "{!lucene} *:*");
    query.setRequestHandler("tree");
    query.setFields(IdFieldMapper.INPUT_FIELD);

    SolrDocumentList results = this.search(query);
    assertEquals(1, results.getNumFound());
    assertNotNull(results.get(0).getFieldValue(IdFieldMapper.INPUT_FIELD));
}

From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java

License:Open Source License

/**
 * Check that the value type of the id field do not have impact on the indexing.
 *//*from   w  w w.  j a  v  a  2  s.  c o m*/
@Test
public void testJsonWithNumericId() throws IOException, SolrServerException {
    String input1 = "{ \"id\" : 1, \"aaa\" : null }";
    String input2 = "{ \"id\" : \"2\", \"aaa\" : null }";

    this.sendUpdateRequest(input1);
    this.sendUpdateRequest(input2);
    this.commit();

    SolrQuery query = new SolrQuery();
    query.setParam("nested", "{!lucene} id:1");
    query.setRequestHandler("tree");
    long found = this.search(query).getNumFound();
    assertEquals(1, found);

    query = new SolrQuery();
    query.setParam("nested", "{!lucene} id:2");
    query.setRequestHandler("tree");
    found = this.search(query).getNumFound();
    assertEquals(1, found);
}

From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java

License:Open Source License

/**
 * Check that the JSON document is correctly indexed in a SIREn's json field.
 *///from w w  w .  jav a 2 s  .c  o  m
@Test
public void testJsonField() throws QueryNodeException, IOException, SolrServerException {
    String input = "{ \"id\" : \"1\", \"aaa\" :  \"bbb\" }";

    this.sendUpdateRequest(input);
    this.commit();

    SolrQuery query = new SolrQuery();
    final ConciseQueryBuilder b = new ConciseQueryBuilder();
    query.setQuery(b.newNode("bbb").setAttribute("aaa").toString());
    query.setRequestHandler("tree");
    long found = this.search(query).getNumFound();
    assertEquals(1, found);
}

From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java

License:Open Source License

/**
 * Check if the field is stored as indicated by the fieldtype of the associated path-based mapper.
 *//*from w w w . j ava  2 s.c  o m*/
@Test
public void testStoredField() throws QueryNodeException, IOException, SolrServerException {
    String input = "{ \"id\" : \"1\", \"aaa\" :  \"bbb\" }";

    this.sendUpdateRequest(input);
    this.commit();

    SolrQuery query = new SolrQuery();
    final ConciseQueryBuilder b = new ConciseQueryBuilder();
    query.setQuery(b.newNode("bbb").setAttribute("aaa").toString());
    query.setRequestHandler("tree");
    query.setFields("aaa");

    SolrDocumentList result = this.search(query);
    assertEquals(1, result.getNumFound());
    assertNotNull(result.get(0).getFieldValue("aaa"));
    assertTrue(result.get(0).getFieldValue("aaa") instanceof ArrayList);
    assertEquals("bbb", ((ArrayList) result.get(0).getFieldValue("aaa")).get(0));
}

From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java

License:Open Source License

/**
 * Test the optional mappers. The fields 'bbb' and 'ddd' are not defined, but the String and Boolean
 * value types are defined. They should be indexed.
 *//* ww  w. j  a  v  a2  s  . c  o  m*/
@Test
public void testOptionalMapper() throws QueryNodeException, IOException, SolrServerException {
    String input = "{ \"id\" : \"1\", \"aaa\" : null, \"bbb\" :  \"ccc\", \"ddd\" : false }";

    this.sendUpdateRequest(input);
    this.commit();

    SolrQuery query = new SolrQuery();
    query.setParam("nested", "{!lucene} bbb:ccc");
    query.setRequestHandler("tree");
    long found = this.search(query).getNumFound();
    assertEquals(1, found);

    query = new SolrQuery();
    query.setParam("nested", "{!lucene} ddd:false");
    query.setRequestHandler("tree");
    found = this.search(query).getNumFound();
    assertEquals(1, found);
}

From source file:com.sindicetech.siren.solr.handler.TestSirenUpdateRequestHandler.java

License:Open Source License

/**
 * Test the default mapper and the null field type. The field 'bbb' with an Integer value should
 * not match any mapper, and therefore be processed by the default mapper. The default mapper
 * is configured with the null field type which indicates to ignore the field. Therefore the
 * field 'bbb' should not be indexed and the search should throw an exception.
 *///from ww  w. ja  va 2  s . c om
@Test(expected = SolrException.class)
public void testDefaultNullMapper() throws QueryNodeException, IOException, SolrServerException {
    String input = "{ \"id\" : \"1\", \"bbb\" :  1 }";

    this.sendUpdateRequest(input);
    this.commit();

    SolrQuery query = new SolrQuery();
    query.setParam("nested", "{!lucene} bbb:1");
    query.setRequestHandler("tree");
    this.search(query);
}