use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.
the class TestHarness method query.
/**
* Processes a "query" using a user constructed SolrQueryRequest, and closes the request at the end.
*
* @param handler the name of the request handler to process the request
* @param req the Query to process, will be closed.
* @return The XML response to the query
* @exception Exception any exception in the response.
* @exception IOException if there is a problem writing the XML
* @see LocalSolrQueryRequest
*/
public String query(String handler, SolrQueryRequest req) throws Exception {
try {
SolrCore core = req.getCore();
SolrQueryResponse rsp = new SolrQueryResponse();
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
core.execute(core.getRequestHandler(handler), req, rsp);
if (rsp.getException() != null) {
throw rsp.getException();
}
QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
if (responseWriter instanceof BinaryQueryResponseWriter) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(32000);
BinaryQueryResponseWriter writer = (BinaryQueryResponseWriter) responseWriter;
writer.write(byteArrayOutputStream, req, rsp);
return new String(byteArrayOutputStream.toByteArray(), "UTF-8");
} else {
StringWriter sw = new StringWriter(32000);
responseWriter.write(sw, req, rsp);
return sw.toString();
}
} finally {
req.close();
SolrRequestInfo.clearRequestInfo();
}
}
use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.
the class TestHarness method queryAndResponse.
/** It is the users responsibility to close the request object when done with it.
* This method does not set/clear SolrRequestInfo */
public SolrQueryResponse queryAndResponse(String handler, SolrQueryRequest req) throws Exception {
try (SolrCore core = getCoreInc()) {
SolrQueryResponse rsp = new SolrQueryResponse();
core.execute(core.getRequestHandler(handler), req, rsp);
if (rsp.getException() != null) {
throw rsp.getException();
}
return rsp;
}
}
use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.
the class StatsComponentTest method testStatsFieldWhitebox.
/**
* Whitebox test of {@link StatsField} parsing to ensure expected equivilence
* operations hold up
*/
public void testStatsFieldWhitebox() throws Exception {
StatsComponent component = new StatsComponent();
List<SearchComponent> components = new ArrayList<>(1);
components.add(component);
SolrParams common = params("stats", "true", "q", "*:*", "nested", "foo_t:cow");
// all of these should produce the same SchemaField based StatsField
for (String param : new String[] { "foo_i", "{!func}field(\"foo_i\")", "{!lucene}_val_:\"field(foo_i)\"" }) {
SolrQueryRequest req = req(common);
try {
ResponseBuilder rb = new ResponseBuilder(req, new SolrQueryResponse(), components);
StatsField sf = new StatsField(rb, param);
assertNull("value source of: " + param, sf.getValueSource());
assertNotNull("schema field of: " + param, sf.getSchemaField());
assertEquals("field name of: " + param, "foo_i", sf.getSchemaField().getName());
} finally {
req.close();
}
}
// all of these should produce the same QueryValueSource based StatsField
for (String param : new String[] { "{!lucene}foo_t:cow", "{!func}query($nested)", "{!field f=foo_t}cow" }) {
SolrQueryRequest req = req(common);
try {
ResponseBuilder rb = new ResponseBuilder(req, new SolrQueryResponse(), components);
StatsField sf = new StatsField(rb, param);
assertNull("schema field of: " + param, sf.getSchemaField());
assertNotNull("value source of: " + param, sf.getValueSource());
assertTrue(sf.getValueSource().getClass() + " is vs type of: " + param, sf.getValueSource() instanceof QueryValueSource);
QueryValueSource qvs = (QueryValueSource) sf.getValueSource();
assertEquals("query of :" + param, new TermQuery(new Term("foo_t", "cow")), qvs.getQuery());
} finally {
req.close();
}
}
}
use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.
the class SpellCheckComponentTest method testReloadOnStart.
@Test
public void testReloadOnStart() throws Exception {
assertU(adoc("id", "0", "lowerfilt", "This is a title"));
assertU(commit());
SolrQueryRequest request = req("qt", "spellCheckCompRH", "q", "*:*", "spellcheck.q", "ttle", "spellcheck", "true", "spellcheck.dictionary", "default", "spellcheck.build", "true");
assertQ(request, "//arr[@name='suggestion'][.='title']");
NamedList args = new NamedList();
NamedList spellchecker = new NamedList();
spellchecker.add(SolrSpellChecker.DICTIONARY_NAME, "default");
spellchecker.add(AbstractLuceneSpellChecker.FIELD, "lowerfilt");
spellchecker.add(AbstractLuceneSpellChecker.INDEX_DIR, "spellchecker1");
args.add("spellchecker", spellchecker);
// TODO: this is really fragile and error prone - find a higher level way to test this.
SpellCheckComponent checker = new SpellCheckComponent();
checker.init(args);
checker.inform(h.getCore());
request = req("qt", "spellCheckCompRH", "q", "*:*", "spellcheck.q", "ttle", "spellcheck", "true", "spellcheck.dictionary", "default", "spellcheck.reload", "true");
List<SearchComponent> components = new ArrayList<>();
for (String name : h.getCore().getSearchComponents().keySet()) {
components.add(h.getCore().getSearchComponent(name));
}
ResponseBuilder rb = new ResponseBuilder(request, new SolrQueryResponse(), components);
checker.prepare(rb);
try {
checker.process(rb);
} catch (NullPointerException e) {
fail("NullPointerException due to reload not initializing analyzers");
}
rb.req.close();
}
use of org.apache.solr.response.SolrQueryResponse in project lucene-solr by apache.
the class StatsComponentTest method testPercentiles.
// simple percentiles test
public void testPercentiles() throws Exception {
// NOTE: deliberately not in numeric order
String percentiles = "10.0,99.9,1.0,2.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,98.0,99.0";
List<String> percentilesList = StrUtils.splitSmart(percentiles, ',');
// test empty case
SolrQueryRequest query = req("q", "*:*", "stats", "true", "stats.field", "{!percentiles='" + percentiles + "'}stat_f");
try {
SolrQueryResponse rsp = h.queryAndResponse(null, query);
NamedList<Double> pout = extractPercentils(rsp, "stat_f");
for (int i = 0; i < percentilesList.size(); i++) {
// ensure exact order, but all values should be null (empty result set)
assertEquals(percentilesList.get(i), pout.getName(i));
assertEquals(null, pout.getVal(i));
}
} finally {
query.close();
}
int id = 0;
// add trivial docs to test basic percentiles
for (int i = 0; i < 100; i++) {
// add the same values multiple times (diff docs)
for (int j = 0; j < 5; j++) {
assertU(adoc("id", ++id + "", "stat_f", "" + i));
}
}
assertU(commit());
query = req("q", "*:*", "stats", "true", "stats.field", "{!percentiles='" + percentiles + "'}stat_f");
try {
SolrQueryResponse rsp = h.queryAndResponse(null, query);
NamedList<Double> pout = extractPercentils(rsp, "stat_f");
for (int i = 0; i < percentilesList.size(); i++) {
String p = percentilesList.get(i);
assertEquals(p, pout.getName(i));
assertEquals(Double.parseDouble(p), pout.getVal(i), 1.0D);
}
} finally {
query.close();
}
// test request for no percentiles
query = req("q", "*:*", "stats", "true", "stats.field", "{!percentiles=''}stat_f");
try {
SolrQueryResponse rsp = h.queryAndResponse(null, query);
NamedList<Double> pout = extractPercentils(rsp, "stat_f");
assertNull(pout);
} finally {
query.close();
}
// non-numeric types don't support percentiles
assertU(adoc("id", ++id + "", "stat_dt", "1999-05-03T04:55:01Z"));
assertU(adoc("id", ++id + "", "stat_s", "cow"));
assertU(commit());
query = req("q", "*:*", "stats", "true", "stats.field", "{!percentiles='" + percentiles + "'}stat_dt", "stats.field", "{!percentiles='" + percentiles + "'}stat_s");
try {
SolrQueryResponse rsp = h.queryAndResponse(null, query);
assertNull(extractPercentils(rsp, "stat_dt"));
assertNull(extractPercentils(rsp, "stat_s"));
} finally {
query.close();
}
}
Aggregations