use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class SolrExampleTests method doPivotFacetTest.
private void doPivotFacetTest(boolean missing) throws Exception {
SolrClient client = getSolrClient();
// Empty the database...
// delete everything!
client.deleteByQuery("*:*");
client.commit();
// make sure it got in
assertNumFound("*:*", 0);
int id = 1;
ArrayList<SolrInputDocument> docs = new ArrayList<>();
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", false));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "a", "inStock", true));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", false));
docs.add(makeTestDoc("id", id++, "features", "aaa", "cat", "b", "inStock", true));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", false));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "a", "inStock", true));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", false));
docs.add(makeTestDoc("id", id++, "features", "bbb", "cat", "b", "inStock", true));
// something not matching all fields
docs.add(makeTestDoc("id", id++, "cat", "b"));
client.add(docs);
client.commit();
SolrQuery query = new SolrQuery("*:*");
query.addFacetPivotField("features,cat", "cat,features", "features,cat,inStock");
query.setFacetMinCount(0);
query.setFacetMissing(missing);
query.setRows(0);
QueryResponse rsp = client.query(query);
assertEquals(docs.size(), rsp.getResults().getNumFound());
NamedList<List<PivotField>> pivots = rsp.getFacetPivot();
assertEquals(3, pivots.size());
// for(Map.Entry<String, List<PivotField>> entry : pivots ) {
// System.out.println( "PIVOT: "+entry.getKey() );
// for( PivotField p : entry.getValue() ) {
// p.write(System.out, 0 );
// }
// System.out.println();
// }
// PIVOT: features,cat
// features=bbb (6)
// cat=b (4)
// cat=a (2)
// features=aaa (5)
// cat=a (3)
// cat=b (2)
// features missing (1)
// cat=b (1)
assertEquals("features,cat", pivots.getName(0));
List<PivotField> pivot = pivots.getVal(0);
assertEquals(missing ? 3 : 2, pivot.size());
PivotField ff = pivot.get(0);
assertEquals("features", ff.getField());
assertEquals("bbb", ff.getValue());
assertEquals(6, ff.getCount());
List<PivotField> counts = ff.getPivot();
assertEquals(2, counts.size());
assertEquals("cat", counts.get(0).getField());
assertEquals("b", counts.get(0).getValue());
assertEquals(4, counts.get(0).getCount());
assertEquals("a", counts.get(1).getValue());
assertEquals(2, counts.get(1).getCount());
ff = pivot.get(1);
assertEquals("features", ff.getField());
assertEquals("aaa", ff.getValue());
assertEquals(5, ff.getCount());
counts = ff.getPivot();
assertEquals(2, counts.size());
assertEquals("cat", counts.get(0).getField());
assertEquals("a", counts.get(0).getValue());
assertEquals(3, counts.get(0).getCount());
assertEquals("b", counts.get(1).getValue());
assertEquals(2, counts.get(1).getCount());
if (missing) {
ff = pivot.get(2);
assertEquals("features", ff.getField());
assertEquals(null, ff.getValue());
assertEquals(1, ff.getCount());
counts = ff.getPivot();
assertEquals(1, counts.size());
assertEquals("cat", counts.get(0).getField());
assertEquals("b", counts.get(0).getValue());
assertEquals(1, counts.get(0).getCount());
}
// PIVOT: cat,features
// cat=b (7)
// features=bbb (4)
// features=aaa (2)
// features missing (1)
// cat=a (5)
// features=aaa (3)
// features=bbb (2)
assertEquals("cat,features", pivots.getName(1));
pivot = pivots.getVal(1);
assertEquals(2, pivot.size());
ff = pivot.get(0);
assertEquals("cat", ff.getField());
assertEquals("b", ff.getValue());
assertEquals(7, ff.getCount());
counts = ff.getPivot();
assertEquals(missing ? 3 : 2, counts.size());
assertEquals("features", counts.get(0).getField());
assertEquals("bbb", counts.get(0).getValue());
assertEquals(4, counts.get(0).getCount());
assertEquals("aaa", counts.get(1).getValue());
assertEquals(2, counts.get(1).getCount());
if (missing) {
assertEquals(null, counts.get(2).getValue());
assertEquals(1, counts.get(2).getCount());
}
ff = pivot.get(1);
assertEquals("cat", ff.getField());
assertEquals("a", ff.getValue());
assertEquals(5, ff.getCount());
counts = ff.getPivot();
assertEquals(2, counts.size());
assertEquals("features", counts.get(0).getField());
assertEquals("aaa", counts.get(0).getValue());
assertEquals(3, counts.get(0).getCount());
assertEquals("bbb", counts.get(1).getValue());
assertEquals(2, counts.get(1).getCount());
// Three deep:
// PIVOT: features,cat,inStock
// features=bbb (6)
// cat=b (4)
// inStock=false (2)
// inStock=true (2)
// cat=a (2)
// inStock=false (1)
// inStock=true (1)
// features=aaa (5)
// cat=a (3)
// inStock=true (2)
// inStock=false (1)
// cat=b (2)
// inStock=false (1)
// inStock=true (1)
// features missing (1)
// cat=b (1)
// inStock missing (1)
assertEquals("features,cat,inStock", pivots.getName(2));
pivot = pivots.getVal(2);
assertEquals(missing ? 3 : 2, pivot.size());
// get(1) should be features=AAAA, then get(0) should be cat=a
PivotField p = pivot.get(1).getPivot().get(0);
assertEquals("cat", p.getField());
assertEquals("a", p.getValue());
counts = p.getPivot();
// p.write(System.out, 5 );
// 2 trues and 1 false under features=AAAA,cat=a
assertEquals(2, counts.size());
assertEquals("inStock", counts.get(0).getField());
assertEquals(Boolean.TRUE, counts.get(0).getValue());
assertEquals(2, counts.get(0).getCount());
if (missing) {
p = pivot.get(2);
assertEquals("features", p.getField());
assertEquals(null, p.getValue());
assertEquals(1, p.getCount());
assertEquals(1, p.getPivot().size());
p = p.getPivot().get(0);
assertEquals("cat", p.getField());
assertEquals("b", p.getValue());
assertEquals(1, p.getCount());
assertEquals(1, p.getPivot().size());
p = p.getPivot().get(0);
assertEquals("inStock", p.getField());
assertEquals(null, p.getValue());
assertEquals(1, p.getCount());
assertEquals(null, p.getPivot());
}
// -- SOLR-2255 Test excluding a filter Query --
// this test is a slight modification to the first pivot facet test
query = new SolrQuery("*:*");
query.addFacetPivotField("{!ex=mytag key=mykey}features,cat");
//filters out one
query.addFilterQuery("{!tag=mytag}-(features:bbb AND cat:a AND inStock:true)");
query.setFacetMinCount(0);
query.setRows(0);
rsp = client.query(query);
//one less due to filter
assertEquals(docs.size() - 1, rsp.getResults().getNumFound());
//The rest of this test should be just like the original since we've
// excluded the 'fq' from the facet count
pivots = rsp.getFacetPivot();
pivot = pivots.getVal(0);
assertEquals("mykey", pivots.getName(0));
assertEquals(2, pivot.size());
ff = pivot.get(0);
assertEquals("features", ff.getField());
assertEquals("bbb", ff.getValue());
assertEquals(6, ff.getCount());
counts = ff.getPivot();
assertEquals(2, counts.size());
assertEquals("cat", counts.get(0).getField());
assertEquals("b", counts.get(0).getValue());
assertEquals(4, counts.get(0).getCount());
assertEquals("a", counts.get(1).getValue());
assertEquals(2, counts.get(1).getCount());
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class SolrExampleTests method testSetNullUpdates.
@Test
public void testSetNullUpdates() throws Exception {
SolrClient solrClient = getSolrClient();
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "testSetNullUpdates");
doc.addField("single_s", "test-value");
doc.addField("multi_ss", Arrays.asList("first", "second"));
solrClient.add(doc);
solrClient.commit(true, true);
doc.removeField("single_s");
doc.removeField("multi_ss");
Map<String, Object> map = Maps.newHashMap();
map.put("set", null);
doc.addField("multi_ss", map);
solrClient.add(doc);
solrClient.commit(true, true);
QueryResponse response = solrClient.query(new SolrQuery("id:testSetNullUpdates"));
assertNotNull("Entire doc was replaced because null update was not written", response.getResults().get(0).getFieldValue("single_s"));
assertNull("Null update failed. Value still exists in document", response.getResults().get(0).getFieldValue("multi_ss"));
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class SolrExampleTestsBase method testCommitWithinOnDelete.
@Test
public void testCommitWithinOnDelete() throws Exception {
// make sure it is empty...
SolrClient client = getSolrClient();
// delete everything!
client.deleteByQuery("*:*");
client.commit();
QueryResponse rsp = client.query(new SolrQuery("*:*"));
Assert.assertEquals(0, rsp.getResults().getNumFound());
// Now add one document...
SolrInputDocument doc3 = new SolrInputDocument();
doc3.addField("id", "id3");
doc3.addField("name", "doc3");
doc3.addField("price", 10);
client.add(doc3);
client.commit();
// now check that it comes out...
rsp = client.query(new SolrQuery("id:id3"));
Assert.assertEquals(1, rsp.getResults().getNumFound());
// now test commitWithin on a delete
UpdateRequest up = new UpdateRequest();
up.setCommitWithin(1000);
up.deleteById("id3");
up.process(client);
// the document should still be there
rsp = client.query(new SolrQuery("id:id3"));
Assert.assertEquals(1, rsp.getResults().getNumFound());
// check if the doc has been deleted every 250 ms for 30 seconds
TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS);
do {
// wait 250 ms
Thread.sleep(250);
rsp = client.query(new SolrQuery("id:id3"));
if (rsp.getResults().getNumFound() == 0) {
return;
}
} while (!timeout.hasTimedOut());
Assert.fail("commitWithin failed to commit");
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class SolrExampleTestsBase method testStreamingRequest.
@Test
public void testStreamingRequest() throws Exception {
SolrClient client = getSolrClient();
// Empty the database...
// delete everything!
client.deleteByQuery("*:*");
client.commit();
// make sure it got in
assertNumFound("*:*", 0);
// Add some docs to the index
UpdateRequest req = new UpdateRequest();
for (int i = 0; i < 10; i++) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "" + i);
doc.addField("cat", "foocat");
req.add(doc);
}
req.setAction(ACTION.COMMIT, true, true);
req.process(client);
// Make sure it ran OK
SolrQuery query = new SolrQuery("*:*");
query.set(CommonParams.FL, "id,score,_docid_");
QueryResponse response = client.query(query);
assertEquals(0, response.getStatus());
assertEquals(10, response.getResults().getNumFound());
// Now make sure each document gets output
final AtomicInteger cnt = new AtomicInteger(0);
client.queryAndStreamResponse(query, new StreamingResponseCallback() {
@Override
public void streamDocListInfo(long numFound, long start, Float maxScore) {
assertEquals(10, numFound);
}
@Override
public void streamSolrDocument(SolrDocument doc) {
cnt.incrementAndGet();
// Make sure the transformer works for streaming
Float score = (Float) doc.get("score");
assertEquals("should have score", new Float(1.0), score);
}
});
assertEquals(10, cnt.get());
}
use of org.apache.solr.common.SolrInputDocument in project lucene-solr by apache.
the class SolrExampleTests method testExampleConfig.
/**
* query the example
*/
@Test
public void testExampleConfig() throws Exception {
SolrClient client = getSolrClient();
// Empty the database...
// delete everything!
client.deleteByQuery("*:*");
// Now add something...
SolrInputDocument doc = new SolrInputDocument();
String docID = "1112211111";
doc.addField("id", docID);
doc.addField("name", "my name!");
Assert.assertEquals(null, doc.getField("foo"));
Assert.assertTrue(doc.getField("name").getValue() != null);
UpdateResponse upres = client.add(doc);
// System.out.println( "ADD:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = client.commit(true, true);
// System.out.println( "COMMIT:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = client.optimize(true, true);
// System.out.println( "OPTIMIZE:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
SolrQuery query = new SolrQuery();
query.setQuery("id:" + docID);
QueryResponse response = client.query(query);
Assert.assertEquals(docID, response.getResults().get(0).getFieldValue("id"));
// Now add a few docs for facet testing...
List<SolrInputDocument> docs = new ArrayList<>();
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("id", "2");
doc2.addField("inStock", true);
doc2.addField("price", 2);
doc2.addField("timestamp_dt", new java.util.Date());
docs.add(doc2);
SolrInputDocument doc3 = new SolrInputDocument();
doc3.addField("id", "3");
doc3.addField("inStock", false);
doc3.addField("price", 3);
doc3.addField("timestamp_dt", new java.util.Date());
docs.add(doc3);
SolrInputDocument doc4 = new SolrInputDocument();
doc4.addField("id", "4");
doc4.addField("inStock", true);
doc4.addField("price", 4);
doc4.addField("timestamp_dt", new java.util.Date());
docs.add(doc4);
SolrInputDocument doc5 = new SolrInputDocument();
doc5.addField("id", "5");
doc5.addField("inStock", false);
doc5.addField("price", 5);
doc5.addField("timestamp_dt", new java.util.Date());
docs.add(doc5);
upres = client.add(docs);
// System.out.println( "ADD:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = client.commit(true, true);
// System.out.println( "COMMIT:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = client.optimize(true, true);
// System.out.println( "OPTIMIZE:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
query = new SolrQuery("*:*");
query.addFacetQuery("price:[* TO 2]");
query.addFacetQuery("price:[2 TO 4]");
query.addFacetQuery("price:[5 TO *]");
query.addFacetField("inStock");
query.addFacetField("price");
query.addFacetField("timestamp_dt");
query.removeFilterQuery("inStock:true");
response = client.query(query);
Assert.assertEquals(0, response.getStatus());
Assert.assertEquals(5, response.getResults().getNumFound());
Assert.assertEquals(3, response.getFacetQuery().size());
Assert.assertEquals(2, response.getFacetField("inStock").getValueCount());
Assert.assertEquals(4, response.getFacetField("price").getValueCount());
// test a second query, test making a copy of the main query
SolrQuery query2 = query.getCopy();
query2.addFilterQuery("inStock:true");
Assert.assertFalse(query.getFilterQueries() == query2.getFilterQueries());
response = client.query(query2);
Assert.assertEquals(1, query2.getFilterQueries().length);
Assert.assertEquals(0, response.getStatus());
Assert.assertEquals(2, response.getResults().getNumFound());
for (SolrDocument outDoc : response.getResults()) {
assertEquals(true, outDoc.getFieldValue("inStock"));
}
// sanity check round tripping of params...
query = new SolrQuery("foo");
query.addFilterQuery("{!field f=inStock}true");
query.addFilterQuery("{!term f=name}hoss");
query.addFacetQuery("price:[* TO 2]");
query.addFacetQuery("price:[2 TO 4]");
response = client.query(query);
assertTrue("echoed params are not a NamedList: " + response.getResponseHeader().get("params").getClass(), response.getResponseHeader().get("params") instanceof NamedList);
NamedList echo = (NamedList) response.getResponseHeader().get("params");
List values = null;
assertEquals("foo", echo.get("q"));
assertTrue("echoed fq is not a List: " + echo.get("fq").getClass(), echo.get("fq") instanceof List);
values = (List) echo.get("fq");
Assert.assertEquals(2, values.size());
Assert.assertEquals("{!field f=inStock}true", values.get(0));
Assert.assertEquals("{!term f=name}hoss", values.get(1));
assertTrue("echoed facet.query is not a List: " + echo.get("facet.query").getClass(), echo.get("facet.query") instanceof List);
values = (List) echo.get("facet.query");
Assert.assertEquals(2, values.size());
Assert.assertEquals("price:[* TO 2]", values.get(0));
Assert.assertEquals("price:[2 TO 4]", values.get(1));
if (jetty != null) {
// check system wide system handler + "/admin/info/system"
String url = jetty.getBaseUrl().toString();
try (HttpSolrClient adminClient = getHttpSolrClient(url)) {
SolrQuery q = new SolrQuery();
q.set("qt", "/admin/info/system");
QueryResponse rsp = adminClient.query(q);
assertNotNull(rsp.getResponse().get("mode"));
assertNotNull(rsp.getResponse().get("lucene"));
}
}
}
Aggregations