Search in sources :

Example 86 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class TestCloudPseudoReturnFields method testAugmentersAndExplicitRTG.

public void testAugmentersAndExplicitRTG() throws Exception {
    // behavior shouldn't matter if we are committed or uncommitted
    for (String id : Arrays.asList("42", "99")) {
        for (SolrParams p : Arrays.asList(params("fl", "id,[docid],[explain],x_alias:[value v=10 t=int]"), params("fl", "id,[docid]", "fl", "[explain],x_alias:[value v=10 t=int]"), params("fl", "id", "fl", "[docid]", "fl", "[explain]", "fl", "x_alias:[value v=10 t=int]"))) {
            SolrDocument doc = getRandClient(random()).getById(id, p);
            String msg = id + ": " + p + " => " + doc;
            assertEquals(msg, 3, doc.size());
            assertTrue(msg, doc.getFieldValue("id") instanceof String);
            // RTG: [explain] should be missing (ignored)
            assertTrue(msg, doc.getFieldValue("x_alias") instanceof Integer);
            assertEquals(msg, 10, doc.getFieldValue("x_alias"));
            assertTrue(msg, doc.getFieldValue("[docid]") instanceof Integer);
            assertTrue(msg, -1 <= ((Integer) doc.getFieldValue("[docid]")).intValue());
        }
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 87 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class TestCloudPseudoReturnFields method testAugmentersRTG.

public void testAugmentersRTG() throws Exception {
    // behavior shouldn't matter if we are committed or uncommitted
    for (String id : Arrays.asList("42", "99")) {
        for (SolrParams p : Arrays.asList(params("fl", "[docid],[shard],[explain],x_alias:[value v=10 t=int]"), params("fl", "[docid],[shard]", "fl", "[explain],x_alias:[value v=10 t=int]"), params("fl", "[docid]", "fl", "[shard]", "fl", "[explain]", "fl", "x_alias:[value v=10 t=int]"))) {
            SolrDocument doc = getRandClient(random()).getById(id, p);
            String msg = id + ": " + p + " => " + doc;
            assertEquals(msg, 3, doc.size());
            assertTrue(msg, doc.getFieldValue("[shard]") instanceof String);
            // RTG: [explain] should be ignored
            assertTrue(msg, doc.getFieldValue("x_alias") instanceof Integer);
            assertEquals(msg, 10, doc.getFieldValue("x_alias"));
            assertTrue(msg, doc.getFieldValue("[docid]") instanceof Integer);
            assertTrue(msg, -1 <= ((Integer) doc.getFieldValue("[docid]")).intValue());
        }
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 88 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class TestCloudPseudoReturnFields method testFunctionsAndExplicit.

public void testFunctionsAndExplicit() throws Exception {
    for (SolrParams p : Arrays.asList(params("q", "*:*", "rows", "1", "fl", "log(val_i),val_i"), params("q", "*:*", "rows", "1", "fl", "log(val_i)", "fl", "val_i"))) {
        SolrDocumentList docs = assertSearch(p);
        assertEquals(p + " => " + docs, 5, docs.getNumFound());
        // doesn't really matter which one
        SolrDocument doc = docs.get(0);
        assertEquals(p + " => " + doc, 2, doc.size());
        assertTrue(p + " => " + doc, doc.getFieldValue("log(val_i)") instanceof Double);
        assertTrue(p + " => " + doc, doc.getFieldValue("val_i") instanceof Integer);
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrDocumentList(org.apache.solr.common.SolrDocumentList)

Example 89 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class TestTolerantUpdateProcessorCloud method createMiniSolrCloudCluster.

@BeforeClass
public static void createMiniSolrCloudCluster() throws Exception {
    final String configName = "solrCloudCollectionConfig";
    final File configDir = new File(TEST_HOME() + File.separator + "collection1" + File.separator + "conf");
    configureCluster(NUM_SERVERS).addConfig(configName, configDir.toPath()).configure();
    assertSpinLoopAllJettyAreRunning(cluster);
    CLOUD_CLIENT = cluster.getSolrClient();
    CLOUD_CLIENT.setDefaultCollection(COLLECTION_NAME);
    CollectionAdminRequest.createCollection(COLLECTION_NAME, configName, NUM_SHARDS, REPLICATION_FACTOR).withProperty("config", "solrconfig-distrib-update-processor-chains.xml").withProperty("schema", // string id for doc routing prefix
    "schema15.xml").process(CLOUD_CLIENT);
    ZkStateReader zkStateReader = CLOUD_CLIENT.getZkStateReader();
    AbstractDistribZkTestBase.waitForRecoveriesToFinish(COLLECTION_NAME, zkStateReader, true, true, 330);
    // really hackish way to get a URL for specific nodes based on shard/replica hosting
    // inspired by TestMiniSolrCloudCluster
    HashMap<String, String> urlMap = new HashMap<>();
    for (JettySolrRunner jetty : cluster.getJettySolrRunners()) {
        URL jettyURL = jetty.getBaseUrl();
        String nodeKey = jettyURL.getHost() + ":" + jettyURL.getPort() + jettyURL.getPath().replace("/", "_");
        urlMap.put(nodeKey, jettyURL.toString());
    }
    zkStateReader.updateClusterState();
    ClusterState clusterState = zkStateReader.getClusterState();
    for (Slice slice : clusterState.getSlices(COLLECTION_NAME)) {
        String shardName = slice.getName();
        Replica leader = slice.getLeader();
        assertNotNull("slice has null leader: " + slice.toString(), leader);
        assertNotNull("slice leader has null node name: " + slice.toString(), leader.getNodeName());
        String leaderUrl = urlMap.remove(leader.getNodeName());
        assertNotNull("could not find URL for " + shardName + " leader: " + leader.getNodeName(), leaderUrl);
        assertEquals("expected two total replicas for: " + slice.getName(), 2, slice.getReplicas().size());
        String passiveUrl = null;
        for (Replica replica : slice.getReplicas()) {
            if (!replica.equals(leader)) {
                passiveUrl = urlMap.remove(replica.getNodeName());
                assertNotNull("could not find URL for " + shardName + " replica: " + replica.getNodeName(), passiveUrl);
            }
        }
        assertNotNull("could not find URL for " + shardName + " replica", passiveUrl);
        if (shardName.equals("shard1")) {
            S_ONE_LEADER_CLIENT = getHttpSolrClient(leaderUrl + "/" + COLLECTION_NAME + "/");
            S_ONE_NON_LEADER_CLIENT = getHttpSolrClient(passiveUrl + "/" + COLLECTION_NAME + "/");
        } else if (shardName.equals("shard2")) {
            S_TWO_LEADER_CLIENT = getHttpSolrClient(leaderUrl + "/" + COLLECTION_NAME + "/");
            S_TWO_NON_LEADER_CLIENT = getHttpSolrClient(passiveUrl + "/" + COLLECTION_NAME + "/");
        } else {
            fail("unexpected shard: " + shardName);
        }
    }
    assertEquals("Should be exactly one server left (nost hosting either shard)", 1, urlMap.size());
    NO_COLLECTION_CLIENT = getHttpSolrClient(urlMap.values().iterator().next() + "/" + COLLECTION_NAME + "/");
    assertNotNull(S_ONE_LEADER_CLIENT);
    assertNotNull(S_TWO_LEADER_CLIENT);
    assertNotNull(S_ONE_NON_LEADER_CLIENT);
    assertNotNull(S_TWO_NON_LEADER_CLIENT);
    assertNotNull(NO_COLLECTION_CLIENT);
    // sanity check that our S_ONE_PRE & S_TWO_PRE really do map to shard1 & shard2 with default routing
    assertEquals(0, CLOUD_CLIENT.add(doc(f("id", S_ONE_PRE + random().nextInt()), f("expected_shard_s", "shard1"))).getStatus());
    assertEquals(0, CLOUD_CLIENT.add(doc(f("id", S_TWO_PRE + random().nextInt()), f("expected_shard_s", "shard2"))).getStatus());
    assertEquals(0, CLOUD_CLIENT.commit().getStatus());
    SolrDocumentList docs = CLOUD_CLIENT.query(params("q", "*:*", "fl", "id,expected_shard_s,[shard]")).getResults();
    assertEquals(2, docs.getNumFound());
    assertEquals(2, docs.size());
    for (SolrDocument doc : docs) {
        String expected = COLLECTION_NAME + "_" + doc.getFirstValue("expected_shard_s") + "_replica";
        String docShard = doc.getFirstValue("[shard]").toString();
        assertTrue("shard routing prefixes don't seem to be aligned anymore, " + "did someone change the default routing rules? " + "and/or the the default core name rules? " + "and/or the numShards used by this test? ... " + "couldn't find " + expected + " as substring of [shard] == '" + docShard + "' ... for docId == " + doc.getFirstValue("id"), docShard.contains(expected));
    }
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) ClusterState(org.apache.solr.common.cloud.ClusterState) SolrDocument(org.apache.solr.common.SolrDocument) HashMap(java.util.HashMap) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) Slice(org.apache.solr.common.cloud.Slice) SolrDocumentList(org.apache.solr.common.SolrDocumentList) File(java.io.File) Replica(org.apache.solr.common.cloud.Replica) URL(java.net.URL) BeforeClass(org.junit.BeforeClass)

Example 90 with SolrDocument

use of org.apache.solr.common.SolrDocument in project lucene-solr by apache.

the class TestRandomFlRTGCloud method assertRTG.

/**
   * Does one or more RTG request for the specified docIds with a randomized fl &amp; fq params, asserting
   * that the returned document (if any) makes sense given the expected SolrInputDocuments
   */
private void assertRTG(final SolrInputDocument[] knownDocs, final int[] docIds) throws IOException, SolrServerException {
    final SolrClient client = getRandClient(random());
    // NOTE: not using SolrClient.getById or getByIds because we want to force choice of "id" vs "ids" params
    final ModifiableSolrParams params = params("qt", "/get");
    // random fq -- nothing fancy, secondary concern for our test
    final Integer FQ_MAX = usually() ? null : random().nextInt();
    if (null != FQ_MAX) {
        params.add("fq", "aaa_i:[* TO " + FQ_MAX + "]");
    }
    final Set<FlValidator> validators = new LinkedHashSet<>();
    // always include id so we can be confident which doc we're looking at
    validators.add(ID_VALIDATOR);
    addRandomFlValidators(random(), validators);
    FlValidator.addParams(validators, params);
    final List<String> idsToRequest = new ArrayList<>(docIds.length);
    final List<SolrInputDocument> docsToExpect = new ArrayList<>(docIds.length);
    for (int docId : docIds) {
        // every docId will be included in the request
        idsToRequest.add("" + docId);
        // only docs that should actually exist and match our (optional) filter will be expected in response
        if (null != knownDocs[docId]) {
            Integer filterVal = (Integer) knownDocs[docId].getFieldValue("aaa_i");
            if (null == FQ_MAX || ((null != filterVal) && filterVal.intValue() <= FQ_MAX.intValue())) {
                docsToExpect.add(knownDocs[docId]);
            }
        }
    }
    // even w/only 1 docId requested, the response format can vary depending on how we request it
    final boolean askForList = random().nextBoolean() || (1 != idsToRequest.size());
    if (askForList) {
        if (1 == idsToRequest.size()) {
            // have to be careful not to try to use "multi" 'id' params with only 1 docId
            // with a single docId, the only way to ask for a list is with the "ids" param
            params.add("ids", idsToRequest.get(0));
        } else {
            if (random().nextBoolean()) {
                // each id in it's own param
                for (String id : idsToRequest) {
                    params.add("id", id);
                }
            } else {
                // add one or more comma separated ids params
                params.add(buildCommaSepParams(random(), "ids", idsToRequest));
            }
        }
    } else {
        assert 1 == idsToRequest.size();
        params.add("id", idsToRequest.get(0));
    }
    final QueryResponse rsp = client.query(params);
    assertNotNull(params.toString(), rsp);
    final SolrDocumentList docs = getDocsFromRTGResponse(askForList, rsp);
    assertNotNull(params + " => " + rsp, docs);
    assertEquals("num docs mismatch: " + params + " => " + docsToExpect + " vs " + docs, docsToExpect.size(), docs.size());
    // NOTE: RTG makes no garuntees about the order docs will be returned in when multi requested
    for (SolrDocument actual : docs) {
        try {
            int actualId = assertParseInt("id", actual.getFirstValue("id"));
            final SolrInputDocument expected = knownDocs[actualId];
            assertNotNull("expected null doc but RTG returned: " + actual, expected);
            Set<String> expectedFieldNames = new TreeSet<>();
            for (FlValidator v : validators) {
                expectedFieldNames.addAll(v.assertRTGResults(validators, expected, actual));
            }
            // ensure only expected field names are in the actual document
            Set<String> actualFieldNames = new TreeSet<>(actual.getFieldNames());
            assertEquals("Actual field names returned differs from expected", expectedFieldNames, actualFieldNames);
        } catch (AssertionError ae) {
            throw new AssertionError(params + " => " + actual + ": " + ae.getMessage(), ae);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) SolrClient(org.apache.solr.client.solrj.SolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) TreeSet(java.util.TreeSet) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse)

Aggregations

SolrDocument (org.apache.solr.common.SolrDocument)230 SolrDocumentList (org.apache.solr.common.SolrDocumentList)93 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)81 ArrayList (java.util.ArrayList)50 SolrQuery (org.apache.solr.client.solrj.SolrQuery)47 Test (org.junit.Test)46 SolrParams (org.apache.solr.common.params.SolrParams)38 SolrServerException (org.apache.solr.client.solrj.SolrServerException)35 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)35 IOException (java.io.IOException)32 SolrInputDocument (org.apache.solr.common.SolrInputDocument)28 HashMap (java.util.HashMap)26 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)24 NamedList (org.apache.solr.common.util.NamedList)21 Map (java.util.Map)20 List (java.util.List)18 SolrClient (org.apache.solr.client.solrj.SolrClient)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)12 SolrException (org.apache.solr.common.SolrException)12