use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.
the class SpellCheckComponentTest method testThresholdTokenFrequency.
@Test
public void testThresholdTokenFrequency() throws Exception {
//"document" is in 2 documents but "another" is only in 1.
//So with a threshold of 29%, "another" is absent from the dictionary
//while "document" is present.
assertJQ(req("qt", rh, SpellCheckComponent.COMPONENT_NAME, "true", "q", "documenq", SpellingParams.SPELLCHECK_DICT, "threshold", SpellingParams.SPELLCHECK_COUNT, "5", SpellingParams.SPELLCHECK_EXTENDED_RESULTS, "true"), "/spellcheck/suggestions/[1]/suggestion==[{'word':'document','freq':2}]");
assertJQ(req("qt", rh, SpellCheckComponent.COMPONENT_NAME, "true", "q", "documenq", SpellingParams.SPELLCHECK_DICT, "threshold_direct", SpellingParams.SPELLCHECK_COUNT, "5", SpellingParams.SPELLCHECK_EXTENDED_RESULTS, "true"), "/spellcheck/suggestions/[1]/suggestion==[{'word':'document','freq':2}]");
//TODO: how do we make this into a 1-liner using "assertQ()" ???
SolrCore core = h.getCore();
SearchComponent speller = core.getSearchComponent("spellcheck");
assertTrue("speller is null and it shouldn't be", speller != null);
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(SpellCheckComponent.COMPONENT_NAME, "true");
params.add(SpellingParams.SPELLCHECK_COUNT, "10");
params.add(SpellingParams.SPELLCHECK_DICT, "threshold");
params.add(SpellingParams.SPELLCHECK_EXTENDED_RESULTS, "true");
params.add(CommonParams.Q, "anotheq");
SolrRequestHandler handler = core.getRequestHandler("spellCheckCompRH");
SolrQueryResponse rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
NamedList values = rsp.getValues();
NamedList spellCheck = (NamedList) values.get("spellcheck");
NamedList suggestions = (NamedList) spellCheck.get("suggestions");
assertTrue(suggestions.get("suggestion") == null);
assertTrue((Boolean) spellCheck.get("correctlySpelled") == false);
params.remove(SpellingParams.SPELLCHECK_DICT);
params.add(SpellingParams.SPELLCHECK_DICT, "threshold_direct");
rsp = new SolrQueryResponse();
rsp.addResponseHeader(new SimpleOrderedMap());
req = new LocalSolrQueryRequest(core, params);
handler.handleRequest(req, rsp);
req.close();
values = rsp.getValues();
spellCheck = (NamedList) values.get("spellcheck");
suggestions = (NamedList) spellCheck.get("suggestions");
assertTrue(suggestions.get("suggestion") == null);
assertTrue((Boolean) spellCheck.get("correctlySpelled") == false);
}
use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.
the class HighlighterTest method testGetHighlightFields.
@Test
public void testGetHighlightFields() {
HashMap<String, String> args = new HashMap<>();
args.put("fl", "id score");
args.put("hl", "true");
args.put("hl.fl", "t*");
assertU(adoc(// static stored
"id", // static stored
"0", // static stored
"title", // static stored
"test", // static not stored
"text", // static not stored
"test", // dynamic stored
"foo_s", // dynamic stored
"test", // dynamic not stored
"foo_sI", // dynamic not stored
"test", // dynamic stored
"bar_s", // dynamic stored
"test", // dynamic not stored
"bar_sI", // dynamic not stored
"test", "weight", // stored but not text
"1.0"));
assertU(commit());
assertU(optimize());
TestHarness.LocalRequestFactory lrf = h.getRequestFactory("standard", 0, 10, args);
SolrQueryRequest request = lrf.makeRequest("test");
SolrHighlighter highlighter = HighlightComponent.getHighlighter(h.getCore());
List<String> highlightFieldNames = Arrays.asList(highlighter.getHighlightFields(null, request, new String[] {}));
assertTrue("Expected to highlight on field \"title\"", highlightFieldNames.contains("title"));
assertFalse("Expected to not highlight on field \"text\"", highlightFieldNames.contains("text"));
assertFalse("Expected to not highlight on field \"weight\"", highlightFieldNames.contains("weight"));
request.close();
args.put("hl.fl", "foo_*");
lrf = h.getRequestFactory("standard", 0, 10, args);
request = lrf.makeRequest("test");
highlighter = HighlightComponent.getHighlighter(h.getCore());
highlightFieldNames = Arrays.asList(highlighter.getHighlightFields(null, request, new String[] {}));
assertEquals("Expected one field to highlight on", 1, highlightFieldNames.size());
assertEquals("Expected to highlight on field \"foo_s\"", "foo_s", highlightFieldNames.get(0));
request.close();
// SOLR-5127
args.put("hl.fl", (random().nextBoolean() ? "foo_*,bar_*" : "bar_*,foo_*"));
lrf = h.getRequestFactory("standard", 0, 10, args);
// hl.fl ordering need not be preserved in output
final Set<String> highlightedSetExpected = new HashSet<String>();
highlightedSetExpected.add("foo_s");
highlightedSetExpected.add("bar_s");
try (LocalSolrQueryRequest localRequest = lrf.makeRequest("test")) {
highlighter = HighlightComponent.getHighlighter(h.getCore());
final Set<String> highlightedSetActual = new HashSet<String>(Arrays.asList(highlighter.getHighlightFields(null, localRequest, new String[] {})));
assertEquals(highlightedSetExpected, highlightedSetActual);
}
}
use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.
the class StatsComponentTest method testFieldStatisticsResultsStringField.
public void testFieldStatisticsResultsStringField() throws Exception {
SolrCore core = h.getCore();
assertU(adoc("id", "1", "active_s", "string1"));
assertU(adoc("id", "2", "active_s", "string2"));
assertU(adoc("id", "3", "active_s", "string3"));
assertU(adoc("id", "4"));
assertU(commit());
Map<String, String> args = new HashMap<>();
args.put(CommonParams.Q, "*:*");
args.put(StatsParams.STATS, "true");
args.put(StatsParams.STATS_FIELD, "active_s");
args.put("f.active_s.stats.calcdistinct", "true");
args.put("indent", "true");
SolrQueryRequest req = new LocalSolrQueryRequest(core, new MapSolrParams(args));
assertQ("test string statistics values", req, "//str[@name='min'][.='string1']", "//str[@name='max'][.='string3']", "//long[@name='count'][.='3']", "//long[@name='missing'][.='1']", "//long[@name='countDistinct'][.='3']", "count(//arr[@name='distinctValues']/str)=3");
assertQ("test string cardinality", req("q", "*:*", "rows", "0", "stats", "true", "stats.field", "{!cardinality=true}active_s"), "//long[@name='cardinality'][.='3']");
// stats over a string function
assertQ("strdist func stats", req("q", "*:*", "stats", "true", "stats.field", "{!func}strdist('string22',active_s,edit)"), "//double[@name='min'][.='0.75']", "//double[@name='max'][.='0.875']", "//double[@name='sum'][.='2.375']", "//long[@name='count'][.='3']", "//long[@name='missing'][.='1']");
}
use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.
the class TestPKIAuthenticationPlugin method test.
public void test() throws Exception {
AtomicReference<Principal> principal = new AtomicReference<>();
String nodeName = "node_x_233";
final MockPKIAuthenticationPlugin mock = new MockPKIAuthenticationPlugin(null, nodeName);
LocalSolrQueryRequest localSolrQueryRequest = new LocalSolrQueryRequest(null, new ModifiableSolrParams()) {
@Override
public Principal getUserPrincipal() {
return principal.get();
}
};
PublicKey correctKey = CryptoKeys.deserializeX509PublicKey(mock.getPublicKey());
mock.remoteKeys.put(nodeName, correctKey);
principal.set(new BasicUserPrincipal("solr"));
mock.solrRequestInfo = new SolrRequestInfo(localSolrQueryRequest, new SolrQueryResponse());
BasicHttpRequest request = new BasicHttpRequest("GET", "http://localhost:56565");
mock.setHeader(request);
final AtomicReference<Header> header = new AtomicReference<>();
header.set(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
assertNotNull(header.get());
assertTrue(header.get().getValue().startsWith(nodeName));
final AtomicReference<ServletRequest> wrappedRequestByFilter = new AtomicReference<>();
HttpServletRequest mockReq = createMockRequest(header);
FilterChain filterChain = (servletRequest, servletResponse) -> wrappedRequestByFilter.set(servletRequest);
mock.doAuthenticate(mockReq, null, filterChain);
assertNotNull(wrappedRequestByFilter.get());
assertEquals("solr", ((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal().getName());
//test 2
// no user
principal.set(null);
header.set(null);
//
wrappedRequestByFilter.set(null);
request = new BasicHttpRequest("GET", "http://localhost:56565");
mock.setHeader(request);
assertNull(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
mock.doAuthenticate(mockReq, null, filterChain);
assertNotNull(wrappedRequestByFilter.get());
assertNull(((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal());
//test 3 . No user request . Request originated from Solr
//create pub key in advance because it can take time and it should be
//created before the header is set
PublicKey key = new CryptoKeys.RSAKeyPair().getPublicKey();
mock.solrRequestInfo = null;
header.set(null);
wrappedRequestByFilter.set(null);
request = new BasicHttpRequest("GET", "http://localhost:56565");
mock.setHeader(request);
header.set(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
assertNotNull(header.get());
assertTrue(header.get().getValue().startsWith(nodeName));
mock.doAuthenticate(mockReq, null, filterChain);
assertNotNull(wrappedRequestByFilter.get());
assertEquals("$", ((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal().getName());
/*test4 mock the restart of a node*/
MockPKIAuthenticationPlugin mock1 = new MockPKIAuthenticationPlugin(null, nodeName) {
int called = 0;
@Override
PublicKey getRemotePublicKey(String nodename) {
try {
return called == 0 ? key : correctKey;
} finally {
called++;
}
}
};
mock1.doAuthenticate(mockReq, null, filterChain);
assertNotNull(wrappedRequestByFilter.get());
assertEquals("$", ((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal().getName());
}
use of org.apache.solr.request.LocalSolrQueryRequest in project lucene-solr by apache.
the class SignatureUpdateProcessorFactoryTest method testNonStringFieldsValues.
@Test
public void testNonStringFieldsValues() throws Exception {
this.chain = "dedupe-allfields";
SolrCore core = h.getCore();
UpdateRequestProcessorChain chained = core.getUpdateProcessingChain(chain);
SignatureUpdateProcessorFactory factory = ((SignatureUpdateProcessorFactory) chained.getProcessors().get(0));
factory.setEnabled(true);
Map<String, String[]> params = new HashMap<>();
MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
params.put(UpdateParams.UPDATE_CHAIN, new String[] { chain });
UpdateRequest ureq = new UpdateRequest();
{
SolrInputDocument doc = new SolrInputDocument();
doc.addField("v_t", "same");
doc.addField("weight", 1.0f);
doc.addField("ints_is", 34);
doc.addField("ints_is", 42);
ureq.add(doc);
}
{
SolrInputDocument doc = new SolrInputDocument();
doc.addField("v_t", "same");
doc.addField("weight", 2.0f);
doc.addField("ints_is", 42);
doc.addField("ints_is", 66);
ureq.add(doc);
}
{
// A and B should have same sig as eachother
// even though the particulars of how the the ints_is list are built
SolrInputDocument docA = new SolrInputDocument();
SolrInputDocument docB = new SolrInputDocument();
UnusualList<Integer> ints = new UnusualList<>(3);
for (int val : new int[] { 42, 66, 34 }) {
docA.addField("ints_is", new Integer(val));
ints.add(val);
}
docB.addField("ints_is", ints);
for (SolrInputDocument doc : new SolrInputDocument[] { docA, docB }) {
doc.addField("v_t", "same");
doc.addField("weight", 3.0f);
ureq.add(doc);
}
}
{
// now add another doc with the same values as A & B above,
// but diff ints_is collection (diff order)
SolrInputDocument doc = new SolrInputDocument();
doc.addField("v_t", "same");
doc.addField("weight", 3.0f);
for (int val : new int[] { 66, 42, 34 }) {
doc.addField("ints_is", new Integer(val));
}
ureq.add(doc);
}
ArrayList<ContentStream> streams = new ArrayList<>(2);
streams.add(new BinaryRequestWriter().getContentStream(ureq));
LocalSolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), mmparams);
try {
req.setContentStreams(streams);
UpdateRequestHandler h = new UpdateRequestHandler();
h.init(new NamedList());
h.handleRequestBody(req, new SolrQueryResponse());
} finally {
req.close();
}
addDoc(commit());
checkNumDocs(4);
}
Aggregations