Search in sources :

Example 11 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class TestSubQueryTransformer method testJustJohnJavabin.

@SuppressWarnings("unchecked")
@Test
public void testJustJohnJavabin() throws Exception {
    final SolrQueryRequest johnTwoFL = req(johnAndNancyParams);
    ModifiableSolrParams params = new ModifiableSolrParams(johnTwoFL.getParams());
    params.set("q", "name_s:john");
    params.set("wt", "javabin");
    johnTwoFL.setParams(params);
    final NamedList<Object> unmarshalled;
    {
        SolrCore core = johnTwoFL.getCore();
        SolrQueryResponse rsp = new SolrQueryResponse();
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(johnTwoFL, rsp));
        SolrQueryResponse response = h.queryAndResponse(johnTwoFL.getParams().get(CommonParams.QT), johnTwoFL);
        BinaryQueryResponseWriter responseWriter = (BinaryQueryResponseWriter) core.getQueryResponseWriter(johnTwoFL);
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        responseWriter.write(bytes, johnTwoFL, response);
        unmarshalled = (NamedList<Object>) new JavaBinCodec().unmarshal(new ByteArrayInputStream(bytes.toByteArray()));
        johnTwoFL.close();
        SolrRequestInfo.clearRequestInfo();
    }
    SolrDocumentList resultDocs = (SolrDocumentList) (unmarshalled.get("response"));
    {
        Map<String, String> engText = new HashMap<>();
        engText.put("text_t", "These guys develop stuff");
        Map<String, String> engId = new HashMap<>();
        engId.put("text_t", "These guys develop stuff");
        engId.put("dept_id_s_dv", "Engineering");
        for (int docNum : new int[] { 0, peopleMultiplier - 1 }) {
            SolrDocument employeeDoc = resultDocs.get(docNum);
            assertEquals("john", employeeDoc.getFieldValue("name_s_dv"));
            for (String subResult : new String[] { "depts", "depts_i" }) {
                SolrDocumentList subDoc = (SolrDocumentList) employeeDoc.getFieldValue(subResult);
                for (int deptNum : new int[] { 0, deptMultiplier - 1 }) {
                    SolrDocument deptDoc = subDoc.get(deptNum);
                    Object expectedDept = (subResult.equals("depts") ? engText : engId);
                    assertTrue("" + expectedDept + " equals to " + deptDoc, expectedDept.equals(deptDoc));
                }
            }
        }
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrCore(org.apache.solr.core.SolrCore) NamedList(org.apache.solr.common.util.NamedList) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) BinaryQueryResponseWriter(org.apache.solr.response.BinaryQueryResponseWriter) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrDocument(org.apache.solr.common.SolrDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 12 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class TestScoreJoinQPScore method testBoost.

@Ignore("SOLR-7814, also don't forget cover boost at testCacheHit()")
public void testBoost() throws Exception {
    indexDataForScorring();
    ScoreMode score = ScoreMode.values()[random().nextInt(ScoreMode.values().length)];
    final SolrQueryRequest req = req("q", "{!join from=movieId_s to=id score=" + score + " b=200}title:movie", "fl", "id,score", "omitHeader", "true");
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, new SolrQueryResponse()));
    final Query luceneQ = QParser.getParser(req.getParams().get("q"), req).getQuery().rewrite(req.getSearcher().getSlowAtomicReader());
    assertTrue(luceneQ instanceof BoostQuery);
    float boost = ((BoostQuery) luceneQ).getBoost();
    assertEquals("" + luceneQ, Float.floatToIntBits(200), Float.floatToIntBits(boost));
    SolrRequestInfo.clearRequestInfo();
    req.close();
}
Also used : ScoreMode(org.apache.lucene.search.join.ScoreMode) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) Query(org.apache.lucene.search.Query) BoostQuery(org.apache.lucene.search.BoostQuery) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) BoostQuery(org.apache.lucene.search.BoostQuery) Ignore(org.junit.Ignore)

Example 13 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class TestOverriddenPrefixQueryForCustomFieldType method assertQueryEquals.

/**
   * @see org.apache.lucene.search.QueryUtils#check
   * @see org.apache.lucene.search.QueryUtils#checkEqual
   */
protected void assertQueryEquals(final SolrQueryRequest req, final String... inputs) throws Exception {
    final Query[] queries = new Query[inputs.length];
    try {
        SolrQueryResponse rsp = new SolrQueryResponse();
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
        for (int i = 0; i < inputs.length; i++) {
            queries[i] = (QParser.getParser(inputs[i], req).getQuery());
        }
    } finally {
        SolrRequestInfo.clearRequestInfo();
    }
    for (int i = 0; i < queries.length; i++) {
        org.apache.lucene.search.QueryUtils.check(queries[i]);
        for (int j = i; j < queries.length; j++) {
            org.apache.lucene.search.QueryUtils.checkEqual(queries[i], queries[j]);
        }
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo)

Example 14 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo in project lucene-solr by apache.

the class DefaultValueUpdateProcessorTest method processAdd.

/**
   * Runs a document through the specified chain, and returns the final 
   * document used when the chain is completed (NOTE: some chains may 
   * modify the document in place
   */
SolrInputDocument processAdd(final String chain, final SolrInputDocument docIn) throws IOException {
    SolrCore core = h.getCore();
    UpdateRequestProcessorChain pc = core.getUpdateProcessingChain(chain);
    assertNotNull("No Chain named: " + chain, pc);
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrQueryRequest req = new LocalSolrQueryRequest(core, new ModifiableSolrParams());
    try {
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
        AddUpdateCommand cmd = new AddUpdateCommand(req);
        cmd.solrDoc = docIn;
        UpdateRequestProcessor processor = pc.createProcessor(req, rsp);
        processor.processAdd(cmd);
        return cmd.solrDoc;
    } finally {
        SolrRequestInfo.clearRequestInfo();
        req.close();
    }
}
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) UpdateRequestProcessorChain(org.apache.solr.update.processor.UpdateRequestProcessorChain) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 15 with SolrRequestInfo

use of org.apache.solr.request.SolrRequestInfo 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)

Aggregations

SolrRequestInfo (org.apache.solr.request.SolrRequestInfo)29 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)22 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)16 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)9 SolrCore (org.apache.solr.core.SolrCore)9 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)8 Map (java.util.Map)5 QueryResponseWriter (org.apache.solr.response.QueryResponseWriter)5 Query (org.apache.lucene.search.Query)4 SolrException (org.apache.solr.common.SolrException)4 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)4 IOException (java.io.IOException)3 StringWriter (java.io.StringWriter)3 HashMap (java.util.HashMap)3 JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)3 NamedList (org.apache.solr.common.util.NamedList)3 BinaryResponseWriter (org.apache.solr.response.BinaryResponseWriter)3 ResultContext (org.apache.solr.response.ResultContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2