Search in sources :

Example 56 with LocalSolrQueryRequest

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);
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrCore(org.apache.solr.core.SolrCore) NamedList(org.apache.solr.common.util.NamedList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrRequestHandler(org.apache.solr.request.SolrRequestHandler) Test(org.junit.Test)

Example 57 with LocalSolrQueryRequest

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);
    }
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) HashMap(java.util.HashMap) TestHarness(org.apache.solr.util.TestHarness) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 58 with LocalSolrQueryRequest

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']");
}
Also used : LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) MapSolrParams(org.apache.solr.common.params.MapSolrParams) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SolrCore(org.apache.solr.core.SolrCore)

Example 59 with LocalSolrQueryRequest

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());
}
Also used : FilterChain(javax.servlet.FilterChain) ServletRequest(javax.servlet.ServletRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) PublicKey(java.security.PublicKey) HashMap(java.util.HashMap) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) CoreContainer(org.apache.solr.core.CoreContainer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Header(org.apache.http.Header) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrTestCaseJ4(org.apache.solr.SolrTestCaseJ4) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) Mockito(org.mockito.Mockito) HttpServletRequest(javax.servlet.http.HttpServletRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) Principal(java.security.Principal) Map(java.util.Map) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) CryptoKeys(org.apache.solr.util.CryptoKeys) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) PublicKey(java.security.PublicKey) FilterChain(javax.servlet.FilterChain) AtomicReference(java.util.concurrent.atomic.AtomicReference) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Header(org.apache.http.Header) CryptoKeys(org.apache.solr.util.CryptoKeys) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) Principal(java.security.Principal)

Example 60 with LocalSolrQueryRequest

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);
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) HashMap(java.util.HashMap) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrCore(org.apache.solr.core.SolrCore) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ContentStream(org.apache.solr.common.util.ContentStream) UpdateRequestHandler(org.apache.solr.handler.UpdateRequestHandler) Test(org.junit.Test)

Aggregations

LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)107 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)61 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)49 SolrCore (org.apache.solr.core.SolrCore)47 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)41 Test (org.junit.Test)41 HashMap (java.util.HashMap)32 NamedList (org.apache.solr.common.util.NamedList)26 ArrayList (java.util.ArrayList)23 MapSolrParams (org.apache.solr.common.params.MapSolrParams)21 SolrException (org.apache.solr.common.SolrException)18 List (java.util.List)15 LinkedHashMap (java.util.LinkedHashMap)11 SolrParams (org.apache.solr.common.params.SolrParams)10 SearchComponent (org.apache.solr.handler.component.SearchComponent)10 SolrRequestHandler (org.apache.solr.request.SolrRequestHandler)10 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)10 Map (java.util.Map)9 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)9 IOException (java.io.IOException)8