Search in sources :

Example 21 with NodeMetaData

use of org.alfresco.solr.client.NodeMetaData in project SearchServices by Alfresco.

the class DistributedAlfrescoSolrFingerPrintTest method testFingerPrint.

@Test
public void testFingerPrint() throws Exception {
    handle.put("explain", SKIPVAL);
    handle.put("timestamp", SKIPVAL);
    handle.put("score", SKIPVAL);
    handle.put("wt", SKIP);
    handle.put("distrib", SKIP);
    handle.put("shards.qt", SKIP);
    handle.put("shards", SKIP);
    // No longer used can be removed in Solr 6.
    handle.put("spellcheck-extras", SKIP);
    handle.put("q", SKIP);
    handle.put("maxScore", SKIPVAL);
    handle.put("_version_", SKIP);
    handle.put("_original_parameters_", SKIP);
    /*
        * Create and index an AclChangeSet.
        */
    AclChangeSet aclChangeSet = getAclChangeSet(1);
    Acl acl = getAcl(aclChangeSet);
    Acl acl2 = getAcl(aclChangeSet);
    AclReaders aclReaders = getAclReaders(aclChangeSet, acl, list("joel"), list("phil"), null);
    AclReaders aclReaders2 = getAclReaders(aclChangeSet, acl2, list("jim"), list("phil"), null);
    indexAclChangeSet(aclChangeSet, list(acl, acl2), list(aclReaders, aclReaders2));
    // Check for the ACL state stamp.
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term(QueryConstants.FIELD_SOLR4_ID, "TRACKER!STATE!ACLTX")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(LegacyNumericRangeQuery.newLongRange(QueryConstants.FIELD_S_ACLTXID, aclChangeSet.getId(), aclChangeSet.getId() + 1, true, false), BooleanClause.Occur.MUST));
    BooleanQuery waitForQuery = builder.build();
    waitForDocCountAllCores(waitForQuery, 1, 80000);
    /*
        * Create and index a Transaction
        */
    // First create a transaction.
    Transaction txn = getTransaction(0, 4);
    // Next create two nodes to update for the transaction
    Node node1 = getNode(txn, acl, Node.SolrApiNodeStatus.UPDATED);
    Node node2 = getNode(txn, acl, Node.SolrApiNodeStatus.UPDATED);
    Node node3 = getNode(txn, acl, Node.SolrApiNodeStatus.UPDATED);
    Node node4 = getNode(txn, acl, Node.SolrApiNodeStatus.UPDATED);
    // Next create the NodeMetaData for each node. TODO: Add more metadata
    NodeMetaData nodeMetaData1 = getNodeMetaData(node1, txn, acl, "mike", null, false);
    NodeMetaData nodeMetaData2 = getNodeMetaData(node2, txn, acl, "mike", null, false);
    NodeMetaData nodeMetaData3 = getNodeMetaData(node3, txn, acl, "mike", null, false);
    NodeMetaData nodeMetaData4 = getNodeMetaData(node4, txn, acl, "mike", null, false);
    List<String> content = new ArrayList();
    int[] sizes = { 2000, 1000, 1500, 750 };
    Random r = new Random(1);
    String token1 = Integer.toString(Math.abs(r.nextInt()));
    for (int i = 0; i < 4; i++) {
        Random rand = new Random(1);
        StringBuilder buf = new StringBuilder();
        int size = sizes[i];
        for (int s = 0; s < size; s++) {
            if (s > 0) {
                buf.append(" ");
            }
            buf.append(Integer.toString(Math.abs(rand.nextInt())));
        }
        content.add(buf.toString());
    }
    // Index the transaction, nodes, and nodeMetaDatas.
    // Note that the content is automatically created by the test framework.
    indexTransaction(txn, list(node1, node2, node3, node4), list(nodeMetaData1, nodeMetaData2, nodeMetaData3, nodeMetaData4), content);
    // Check for the TXN state stamp.
    builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term(QueryConstants.FIELD_SOLR4_ID, "TRACKER!STATE!TX")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(LegacyNumericRangeQuery.newLongRange(QueryConstants.FIELD_S_TXID, txn.getId(), txn.getId() + 1, true, false), BooleanClause.Occur.MUST));
    waitForQuery = builder.build();
    waitForDocCountAllCores(waitForQuery, 1, 80000);
    /*
        * Query the index for the content
        */
    waitForDocCountAllCores(new TermQuery(new Term(QueryConstants.FIELD_READER, "jim")), 1, 80000);
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", token1)), 4, 80000);
    QueryResponse response = query(getDefaultTestClient(), true, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}", params("q", "FINGERPRINT:" + node1.getId(), "qt", "/afts", "shards.qt", "/afts", "start", "0", "fl", "DBID,score", "rows", "100"));
    SolrDocumentList docs = response.getResults();
    assertTrue(docs.getNumFound() == 4);
    SolrDocument doc0 = docs.get(0);
    long dbid0 = (long) doc0.getFieldValue("DBID");
    assertTrue(dbid0 == node1.getId());
    SolrDocument doc1 = docs.get(1);
    long dbid1 = (long) doc1.getFieldValue("DBID");
    assertTrue(dbid1 == node3.getId());
    SolrDocument doc2 = docs.get(2);
    long dbid2 = (long) doc2.getFieldValue("DBID");
    assertTrue(dbid2 == node2.getId());
    SolrDocument doc3 = docs.get(3);
    long dbid3 = (long) doc3.getFieldValue("DBID");
    assertTrue(dbid3 == node4.getId());
    response = query(getDefaultTestClient(), true, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}", params("q", "FINGERPRINT:" + node1.getId() + "_70", "qt", "/afts", "shards.qt", "/afts", "start", "0", "fl", "DBID,score", "rows", "100"));
    docs = response.getResults();
    assertTrue(docs.getNumFound() == 2);
    doc0 = docs.get(0);
    dbid0 = (long) doc0.getFieldValue("DBID");
    assertTrue(dbid0 == node1.getId());
    doc1 = docs.get(1);
    dbid1 = (long) doc1.getFieldValue("DBID");
    assertTrue(dbid1 == node3.getId());
    response = query(getDefaultTestClient(), true, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}", params("q", "FINGERPRINT:" + node1.getId() + "_45", "qt", "/afts", "shards.qt", "/afts", "start", "0", "fl", "DBID,score", "rows", "100"));
    docs = response.getResults();
    assertTrue(docs.getNumFound() == 3);
    doc0 = docs.get(0);
    dbid0 = (long) doc0.getFieldValue("DBID");
    assertTrue(dbid0 == node1.getId());
    doc1 = docs.get(1);
    dbid1 = (long) doc1.getFieldValue("DBID");
    assertTrue(dbid1 == node3.getId());
    doc2 = docs.get(2);
    dbid2 = (long) doc2.getFieldValue("DBID");
    assertTrue(dbid2 == node2.getId());
    response = query(getDefaultTestClient(), true, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}", params("q", "FINGERPRINT:" + nodeMetaData1.getNodeRef().getId(), "qt", "/afts", "shards.qt", "/afts", "start", "0", "fl", "DBID,score", "rows", "100"));
    docs = response.getResults();
    assertTrue(docs.getNumFound() == 4);
    doc0 = docs.get(0);
    dbid0 = (long) doc0.getFieldValue("DBID");
    assertTrue(dbid0 == node1.getId());
    doc1 = docs.get(1);
    dbid1 = (long) doc1.getFieldValue("DBID");
    assertTrue(dbid1 == node3.getId());
    doc2 = docs.get(2);
    dbid2 = (long) doc2.getFieldValue("DBID");
    assertTrue(dbid2 == node2.getId());
    doc3 = docs.get(3);
    dbid3 = (long) doc3.getFieldValue("DBID");
    assertTrue(dbid3 == node4.getId());
    response = query(getDefaultTestClient(), true, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}", params("q", "FINGERPRINT:" + nodeMetaData1.getNodeRef().getId() + "_70", "qt", "/afts", "shards.qt", "/afts", "start", "0", "fl", "DBID,score", "rows", "100"));
    docs = response.getResults();
    assertTrue(docs.getNumFound() == 2);
    doc0 = docs.get(0);
    dbid0 = (long) doc0.getFieldValue("DBID");
    assertTrue(dbid0 == node1.getId());
    doc1 = docs.get(1);
    dbid1 = (long) doc1.getFieldValue("DBID");
    assertTrue(dbid1 == node3.getId());
    response = query(getDefaultTestClient(), true, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [\"joel\"], \"tenants\": []}", params("q", "FINGERPRINT:" + nodeMetaData1.getNodeRef().getId() + "_45", "qt", "/afts", "shards.qt", "/afts", "start", "0", "fl", "DBID,score", "rows", "100"));
    docs = response.getResults();
    assertTrue(docs.getNumFound() == 3);
    doc0 = docs.get(0);
    dbid0 = (long) doc0.getFieldValue("DBID");
    assertTrue(dbid0 == node1.getId());
    doc1 = docs.get(1);
    dbid1 = (long) doc1.getFieldValue("DBID");
    assertTrue(dbid1 == node3.getId());
    doc2 = docs.get(2);
    dbid2 = (long) doc2.getFieldValue("DBID");
    assertTrue(dbid2 == node2.getId());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) NodeMetaData(org.alfresco.solr.client.NodeMetaData) AlfrescoSolrUtils.getNodeMetaData(org.alfresco.solr.AlfrescoSolrUtils.getNodeMetaData) Node(org.alfresco.solr.client.Node) AlfrescoSolrUtils.getNode(org.alfresco.solr.AlfrescoSolrUtils.getNode) ArrayList(java.util.ArrayList) Acl(org.alfresco.solr.client.Acl) AlfrescoSolrUtils.getAcl(org.alfresco.solr.AlfrescoSolrUtils.getAcl) Term(org.apache.lucene.index.Term) SolrDocumentList(org.apache.solr.common.SolrDocumentList) BooleanClause(org.apache.lucene.search.BooleanClause) SolrDocument(org.apache.solr.common.SolrDocument) Transaction(org.alfresco.solr.client.Transaction) AlfrescoSolrUtils.getTransaction(org.alfresco.solr.AlfrescoSolrUtils.getTransaction) Random(java.util.Random) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) AlfrescoSolrUtils.getAclChangeSet(org.alfresco.solr.AlfrescoSolrUtils.getAclChangeSet) AlfrescoSolrUtils.indexAclChangeSet(org.alfresco.solr.AlfrescoSolrUtils.indexAclChangeSet) AclChangeSet(org.alfresco.solr.client.AclChangeSet) AlfrescoSolrUtils.getAclReaders(org.alfresco.solr.AlfrescoSolrUtils.getAclReaders) AclReaders(org.alfresco.solr.client.AclReaders) Test(org.junit.Test) AbstractAlfrescoDistributedTest(org.alfresco.solr.AbstractAlfrescoDistributedTest)

Example 22 with NodeMetaData

use of org.alfresco.solr.client.NodeMetaData in project SearchServices by Alfresco.

the class AlfrescoSolrTrackerExceptionTest method testTrackers.

@Test
public void testTrackers() throws Exception {
    /*
        * Create and index an AclChangeSet.
        */
    logger.info("######### Starting tracker test ###########");
    AclChangeSet aclChangeSet = getAclChangeSet(1, 1);
    Acl acl = getAcl(aclChangeSet);
    Acl acl2 = getAcl(aclChangeSet);
    AclReaders aclReaders = getAclReaders(aclChangeSet, acl, list("joel"), list("phil"), null);
    AclReaders aclReaders2 = getAclReaders(aclChangeSet, acl2, list("jim"), list("phil"), null);
    Thread.sleep(20000);
    SOLRAPIQueueClient.setThrowException(false);
    logger.info("#################### Stop throwing exceptions ##############################");
    indexAclChangeSet(aclChangeSet, list(acl, acl2), list(aclReaders, aclReaders2));
    // Check for the ACL state stamp.
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term(QueryConstants.FIELD_SOLR4_ID, "TRACKER!STATE!ACLTX")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(LegacyNumericRangeQuery.newLongRange(QueryConstants.FIELD_S_ACLTXID, aclChangeSet.getId(), aclChangeSet.getId() + 1, true, false), BooleanClause.Occur.MUST));
    BooleanQuery waitForQuery = builder.build();
    waitForDocCount(waitForQuery, 1, MAX_WAIT_TIME);
    logger.info("#################### Passed First Test ##############################");
    // assert(false);
    /*
        * Create and index a Transaction
        */
    // First create a transaction.
    Transaction txn = getTransaction(0, 2, 1);
    // Next create two nodes to update for the transaction
    Node folderNode = getNode(txn, acl, Node.SolrApiNodeStatus.UPDATED);
    Node fileNode = getNode(txn, acl, Node.SolrApiNodeStatus.UPDATED);
    Node errorNode = getNode(txn, acl, Node.SolrApiNodeStatus.UPDATED);
    logger.info("######### error node:" + errorNode.getId());
    // Next create the NodeMetaData for each node. TODO: Add more metadata
    NodeMetaData folderMetaData = getNodeMetaData(folderNode, txn, acl, "mike", null, false);
    NodeMetaData fileMetaData = getNodeMetaData(fileNode, txn, acl, "mike", ancestors(folderMetaData.getNodeRef()), false);
    // The errorNodeMetaData will cause an exception.
    NodeMetaData errorMetaData = getNodeMetaData(errorNode, txn, acl, "lisa", ancestors(folderMetaData.getNodeRef()), true);
    // Index the transaction, nodes, and nodeMetaDatas.
    // Note that the content is automatically created by the test framework.
    indexTransaction(txn, list(errorNode, folderNode, fileNode), list(errorMetaData, folderMetaData, fileMetaData));
    logger.info("#################### Start throwing exceptions ##############################");
    SOLRAPIQueueClient.setThrowException(true);
    Thread.sleep(10000);
    SOLRAPIQueueClient.setThrowException(false);
    logger.info("#################### Stop throwing exceptions ##############################");
    // Check for the TXN state stamp.
    logger.info("#################### Started Second Test ##############################");
    builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term(QueryConstants.FIELD_SOLR4_ID, "TRACKER!STATE!TX")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(LegacyNumericRangeQuery.newLongRange(QueryConstants.FIELD_S_TXID, txn.getId(), txn.getId() + 1, true, false), BooleanClause.Occur.MUST));
    waitForQuery = builder.build();
    waitForDocCount(waitForQuery, 1, MAX_WAIT_TIME);
    logger.info("#################### Passed Second Test ##############################");
    /*
        * Query the index for the content
        */
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_READER, "jim")), 1, MAX_WAIT_TIME);
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", "world")), 2, MAX_WAIT_TIME);
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", Long.toString(fileNode.getId()))), 1, MAX_WAIT_TIME);
    logger.info("#################### Passed Third Test ##############################");
    ModifiableSolrParams params = new ModifiableSolrParams();
    // Query for an id in the content field. The node id is automatically populated into the content field by test framework
    params.add("q", "t1:" + fileNode.getId());
    params.add("qt", "/afts");
    params.add("start", "0");
    params.add("rows", "6");
    params.add("sort", "id asc");
    params.add("fq", "{!afts}AUTHORITY_FILTER_FROM_JSON");
    SolrServletRequest req = areq(params, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [ \"joel\"], \"tenants\": [ \"\" ]}");
    assertQ(req, "*[count(//doc)=1]", "//result/doc[1]/long[@name='DBID'][.='" + fileNode.getId() + "']");
    logger.info("#################### Passed Fourth Test ##############################");
    // Check for the error doc
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_DOC_TYPE, "ErrorNode")), 1, MAX_WAIT_TIME);
    logger.info("#################### Passed Fifth Test ##############################");
    // Mark the folder as needing cascade
    Transaction txn1 = getTransaction(0, 1, 2);
    // Update the properties on the Node and NodeMetaData to simulate an update to the Node.
    folderMetaData.getProperties().put(ContentModel.PROP_CASCADE_TX, new StringPropertyValue(Long.toString(txn1.getId())));
    // Update the txnId
    folderNode.setTxnId(txn1.getId());
    folderMetaData.setTxnId(txn1.getId());
    // Change the ancestor on the file just to see if it's been updated
    NodeRef nodeRef = new NodeRef(new StoreRef("workspace", "SpacesStore"), createGUID());
    fileMetaData.setAncestors(ancestors(nodeRef));
    // This will add the PROP_CASCADE_TX property to the folder.
    logger.info("################### ADDING CASCADE TRANSACTION #################");
    indexTransaction(txn1, list(folderNode), list(folderMetaData));
    // Check for the TXN state stamp.
    builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term(QueryConstants.FIELD_SOLR4_ID, "TRACKER!STATE!TX")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(LegacyNumericRangeQuery.newLongRange(QueryConstants.FIELD_S_TXID, txn1.getId(), txn1.getId() + 1, true, false), BooleanClause.Occur.MUST));
    waitForDocCount(builder.build(), 1, MAX_WAIT_TIME);
    logger.info("#################### Passed Sixth Test ##############################");
    TermQuery termQuery1 = new TermQuery(new Term(QueryConstants.FIELD_ANCESTOR, nodeRef.toString()));
    waitForDocCount(termQuery1, 1, MAX_WAIT_TIME);
    params = new ModifiableSolrParams();
    params.add("q", QueryConstants.FIELD_ANCESTOR + ":\"" + nodeRef.toString() + "\"");
    params.add("qt", "/afts");
    params.add("start", "0");
    params.add("rows", "6");
    params.add("sort", "id asc");
    params.add("fq", "{!afts}AUTHORITY_FILTER_FROM_JSON");
    req = areq(params, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [ \"mike\"], \"tenants\": [ \"\" ]}");
    assertQ(req, "*[count(//doc)=1]", "//result/doc[1]/long[@name='DBID'][.='" + fileNode.getId() + "']");
    logger.info("#################### Passed Seventh Test ##############################");
    // Check that both documents have been indexed and have content.
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", "world")), 2, MAX_WAIT_TIME);
    logger.info("#################### Passed Eighth Test ##############################");
    // Try bulk loading
    Transaction txn2 = getTransaction(0, 550);
    List<Node> nodes = new ArrayList();
    List<NodeMetaData> nodeMetaDatas = new ArrayList();
    for (int i = 0; i < 550; i++) {
        Node n = getNode(txn2, acl, Node.SolrApiNodeStatus.UPDATED);
        NodeMetaData nm = getNodeMetaData(n, txn2, acl, "mike", ancestors(folderMetaData.getNodeRef()), false);
        nodes.add(n);
        nodeMetaDatas.add(nm);
    }
    logger.info("############################ Bulk Nodes:" + nodes.size());
    indexTransaction(txn2, nodes, nodeMetaDatas);
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", "world")), 552, MAX_WAIT_TIME);
    logger.info("#################### Passed Ninth Test ##############################");
    for (int i = 0; i < 1000; i++) {
        Transaction txnX = getTransaction(0, 1);
        List<Node> nodesX = new ArrayList();
        List<NodeMetaData> nodeMetaDatasX = new ArrayList();
        Node n = getNode(txnX, acl, Node.SolrApiNodeStatus.UPDATED);
        NodeMetaData nm = getNodeMetaData(n, txnX, acl, "mike", ancestors(folderMetaData.getNodeRef()), false);
        nodesX.add(n);
        nodeMetaDatasX.add(nm);
        indexTransaction(txnX, nodesX, nodeMetaDatasX);
    }
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", "world")), 1552, MAX_WAIT_TIME);
    logger.info("#################### Passed Tenth Test ##############################");
    // Test the maintenance methods
    fileMetaData.setOwner("amy");
    reindexTransactionId(txn.getId());
    folderMetaData.setOwner("jill");
    reindexNodeId(folderNode.getId());
    // Wait for a document that has the new owner and the content populated.
    builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", "world")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(new TermQuery(new Term(QueryConstants.FIELD_OWNER, "amy")), BooleanClause.Occur.MUST));
    waitForDocCount(builder.build(), 1, MAX_WAIT_TIME);
    logger.info("#################### Passed Eleventh Test ##############################");
    // Wait for a document that has the new owner and the content populated.
    builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", "world")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(new TermQuery(new Term(QueryConstants.FIELD_OWNER, "jill")), BooleanClause.Occur.MUST));
    waitForDocCount(builder.build(), 1, MAX_WAIT_TIME);
    logger.info("#################### Passed Twelth Test ##############################");
    params = new ModifiableSolrParams();
    // Query for an id in the content field. The node id is automatically populated into the content field by test framework
    params.add("q", "t1:" + fileNode.getId());
    params.add("qt", "/afts");
    params.add("start", "0");
    params.add("rows", "6");
    params.add("sort", "id asc");
    params.add("fq", "{!afts}AUTHORITY_FILTER_FROM_JSON");
    req = areq(params, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [ \"amy\"], \"tenants\": [ \"\" ]}");
    assertQ(req, "*[count(//doc)=1]", "//result/doc[1]/long[@name='DBID'][.='" + fileNode.getId() + "']");
    logger.info("#################### Passed Fourteenth Test ##############################");
    params = new ModifiableSolrParams();
    // Query for an id in the content field. The node id is automatically populated into the content field by test framework
    params.add("q", "t1:" + folderNode.getId());
    params.add("qt", "/afts");
    params.add("start", "0");
    params.add("rows", "6");
    params.add("sort", "id asc");
    params.add("fq", "{!afts}AUTHORITY_FILTER_FROM_JSON");
    req = areq(params, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [ \"jill\"], \"tenants\": [ \"\" ]}");
    assertQ(req, "*[count(//doc)=1]", "//result/doc[1]/long[@name='DBID'][.='" + folderNode.getId() + "']");
    logger.info("#################### Passed Fifteenth Test ##############################");
    List<String> readers = aclReaders.getReaders();
    // Change the aclReader
    readers.set(0, "andy");
    indexAclId(acl.getId());
    List<String> readers2 = aclReaders2.getReaders();
    // Change the aclReader
    readers2.set(0, "ice");
    reindexAclId(acl2.getId());
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_READER, "andy")), 1, MAX_WAIT_TIME);
    // Ice should have replaced jim in acl2.
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_READER, "ice")), 1, MAX_WAIT_TIME);
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_READER, "jim")), 0, MAX_WAIT_TIME);
    logger.info("#################### Passed Sixteenth Test ##############################");
    params = new ModifiableSolrParams();
    // Query for an id in the content field. The node id is automatically populated into the content field by test framework
    params.add("q", "t1:" + fileNode.getId());
    params.add("qt", "/afts");
    params.add("start", "0");
    params.add("rows", "6");
    params.add("sort", "id asc");
    params.add("fq", "{!afts}AUTHORITY_FILTER_FROM_JSON");
    req = areq(params, "{\"locales\":[\"en\"], \"templates\": [{\"name\":\"t1\", \"template\":\"%cm:content\"}], \"authorities\": [ \"andy\"], \"tenants\": [ \"\" ]}");
    assertQ(req, "*[count(//doc)=1]", "//result/doc[1]/long[@name='DBID'][.='" + fileNode.getId() + "']");
    logger.info("#################### Passed Seventeenth Test ##############################");
    // Change the aclReader
    readers.set(0, "alan");
    // Change the aclReader
    readers2.set(0, "paul");
    // This should replace "andy" and "ice" with "alan" and "paul"
    reindexAclChangeSetId(aclChangeSet.getId());
    // Test that "alan" and "paul" are in the index
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_READER, "alan")), 1, MAX_WAIT_TIME);
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_READER, "paul")), 1, MAX_WAIT_TIME);
    // Test that "andy" and "ice" are removed
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_READER, "andy")), 0, MAX_WAIT_TIME);
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_READER, "ice")), 0, MAX_WAIT_TIME);
    // Test Maintenance acl purge
    purgeAclId(acl2.getId());
    // Test Maintenance node purge
    purgeNodeId(fileNode.getId());
    purgeTransactionId(txn2.getId());
    // paul should be purged
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_READER, "paul")), 0, MAX_WAIT_TIME);
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", Long.toString(fileNode.getId()))), 0, MAX_WAIT_TIME);
    // Refects the purged node and transaction
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", "world")), 1001, MAX_WAIT_TIME);
    logger.info("#################### Passed Eighteenth Test ##############################");
    purgeAclChangeSetId(aclChangeSet.getId());
    // alan should be purged
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_READER, "alan")), 0, MAX_WAIT_TIME);
    // Fix the error node
    errorMetaData.setNodeRef(new NodeRef(new StoreRef("workspace", "SpacesStore"), createGUID()));
    // Reload the error node.
    logger.info("Retry the error node");
    retry();
    // The error in the index should disappear.
    waitForDocCount(new TermQuery(new Term(QueryConstants.FIELD_DOC_TYPE, "ErrorNode")), 0, MAX_WAIT_TIME);
    // And the error node should be present
    waitForDocCount(new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", Long.toString(errorNode.getId()))), 1, MAX_WAIT_TIME);
    logger.info("#################### Passed Nineteenth Test ##############################");
// assert(false);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) StringPropertyValue(org.alfresco.solr.client.StringPropertyValue) StoreRef(org.alfresco.service.cmr.repository.StoreRef) NodeMetaData(org.alfresco.solr.client.NodeMetaData) AlfrescoSolrUtils.getNodeMetaData(org.alfresco.solr.AlfrescoSolrUtils.getNodeMetaData) Node(org.alfresco.solr.client.Node) AlfrescoSolrUtils.getNode(org.alfresco.solr.AlfrescoSolrUtils.getNode) ArrayList(java.util.ArrayList) Acl(org.alfresco.solr.client.Acl) AlfrescoSolrUtils.getAcl(org.alfresco.solr.AlfrescoSolrUtils.getAcl) Term(org.apache.lucene.index.Term) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) BooleanClause(org.apache.lucene.search.BooleanClause) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Transaction(org.alfresco.solr.client.Transaction) AlfrescoSolrUtils.getTransaction(org.alfresco.solr.AlfrescoSolrUtils.getTransaction) AlfrescoSolrUtils.getAclChangeSet(org.alfresco.solr.AlfrescoSolrUtils.getAclChangeSet) AlfrescoSolrUtils.indexAclChangeSet(org.alfresco.solr.AlfrescoSolrUtils.indexAclChangeSet) AclChangeSet(org.alfresco.solr.client.AclChangeSet) AlfrescoSolrUtils.getAclReaders(org.alfresco.solr.AlfrescoSolrUtils.getAclReaders) AclReaders(org.alfresco.solr.client.AclReaders) Test(org.junit.Test)

Example 23 with NodeMetaData

use of org.alfresco.solr.client.NodeMetaData in project SearchServices by Alfresco.

the class SolrInformationServer method getCascadeNodes.

public List<NodeMetaData> getCascadeNodes(List<Long> txnIds) throws AuthenticationException, IOException, JSONException {
    List<FieldInstance> list = AlfrescoSolrDataModel.getInstance().getIndexedFieldNamesForProperty(ContentModel.PROP_CASCADE_TX).getFields();
    FieldInstance fieldInstance = list.get(0);
    RefCounted<SolrIndexSearcher> refCounted = null;
    IntArrayList docList = null;
    HashSet<Long> childIds = new HashSet();
    try {
        refCounted = core.getSearcher();
        SolrIndexSearcher searcher = refCounted.get();
        String field = fieldInstance.getField();
        SchemaField schemaField = searcher.getSchema().getField(field);
        FieldType fieldType = schemaField.getType();
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        BooleanQuery booleanQuery = null;
        for (Long l : txnIds) {
            BytesRefBuilder bytesRefBuilder = new BytesRefBuilder();
            fieldType.readableToIndexed(l.toString(), bytesRefBuilder);
            TermQuery termQuery = new TermQuery(new Term(field, bytesRefBuilder.toBytesRef()));
            BooleanClause booleanClause = new BooleanClause(termQuery, BooleanClause.Occur.SHOULD);
            builder.add(booleanClause);
        }
        booleanQuery = builder.build();
        DocListCollector collector = new DocListCollector();
        searcher.search(booleanQuery, collector);
        docList = collector.getDocs();
        // System.out.println("################ CASCASDE Parent Nodes:"+docList.size());
        int size = docList.size();
        Set set = new HashSet();
        set.add(FIELD_SOLR4_ID);
        for (int i = 0; i < size; i++) {
            int docId = docList.get(i);
            Document document = searcher.doc(docId, set);
            IndexableField indexableField = document.getField(FIELD_SOLR4_ID);
            String id = indexableField.stringValue();
            TenantAclIdDbId ids = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
            // System.out.println("################## Cascade Parent:"+ ids.dbId);
            childIds.add(ids.dbId);
        }
    } finally {
        refCounted.decref();
    }
    List<NodeMetaData> allNodeMetaDatas = new ArrayList();
    for (Long childId : childIds) {
        NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
        nmdp.setFromNodeId(childId);
        nmdp.setToNodeId(childId);
        nmdp.setIncludeAclId(false);
        nmdp.setIncludeAspects(false);
        nmdp.setIncludeChildAssociations(false);
        nmdp.setIncludeChildIds(true);
        nmdp.setIncludeNodeRef(false);
        nmdp.setIncludeOwner(false);
        nmdp.setIncludeParentAssociations(false);
        // We only care about the path and ancestors (which is included) for this case
        nmdp.setIncludePaths(true);
        nmdp.setIncludeProperties(false);
        nmdp.setIncludeType(false);
        nmdp.setIncludeTxnId(true);
        // Gets only one
        List<NodeMetaData> nodeMetaDatas = repositoryClient.getNodesMetaData(nmdp, 1);
        allNodeMetaDatas.addAll(nodeMetaDatas);
    }
    return allNodeMetaDatas;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Set(java.util.Set) AclChangeSet(org.alfresco.solr.client.AclChangeSet) LinkedHashSet(java.util.LinkedHashSet) IOpenBitSet(org.alfresco.solr.adapters.IOpenBitSet) HashSet(java.util.HashSet) TenantAclIdDbId(org.alfresco.solr.AlfrescoSolrDataModel.TenantAclIdDbId) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) IntArrayList(com.carrotsearch.hppc.IntArrayList) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) NodeMetaDataParameters(org.alfresco.solr.client.NodeMetaDataParameters) FieldInstance(org.alfresco.solr.AlfrescoSolrDataModel.FieldInstance) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) TermQuery(org.apache.lucene.search.TermQuery) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) NodeMetaData(org.alfresco.solr.client.NodeMetaData) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Term(org.apache.lucene.index.Term) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) BooleanClause(org.apache.lucene.search.BooleanClause) IndexableField(org.apache.lucene.index.IndexableField) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 24 with NodeMetaData

use of org.alfresco.solr.client.NodeMetaData in project SearchServices by Alfresco.

the class SolrInformationServer method recreateSolrDoc.

private SolrInputDocument recreateSolrDoc(long dbId, String tenant) throws AuthenticationException, IOException, JSONException {
    NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
    nmdp.setFromNodeId(dbId);
    nmdp.setToNodeId(dbId);
    List<NodeMetaData> nodeMetaDatas = repositoryClient.getNodesMetaData(nmdp, Integer.MAX_VALUE);
    SolrInputDocument newDoc = null;
    if (!nodeMetaDatas.isEmpty()) {
        NodeMetaData nodeMetaData = nodeMetaDatas.get(0);
        newDoc = createNewDoc(nodeMetaData, DOC_TYPE_NODE);
        addFieldsToDoc(nodeMetaData, newDoc);
        boolean isContentIndexedForNode = isContentIndexedForNode(nodeMetaData.getProperties());
        Map<QName, PropertyValue> properties = nodeMetaData.getProperties();
        // Cached doc is null for this method because it is a recreate after a cache miss.
        addPropertiesToDoc(properties, isContentIndexedForNode, newDoc, null, transformContent);
    } else {
        // we get an empty list if a node is deleted
        if (log.isDebugEnabled()) {
            log.debug("Failed to recreate Solr doc with tenant [" + tenant + "] and dbId [" + dbId + "], " + "because node not found in repository.");
        }
    }
    return newDoc;
}
Also used : NodeMetaData(org.alfresco.solr.client.NodeMetaData) SolrInputDocument(org.apache.solr.common.SolrInputDocument) QName(org.alfresco.service.namespace.QName) NodeMetaDataParameters(org.alfresco.solr.client.NodeMetaDataParameters) StringPropertyValue(org.alfresco.solr.client.StringPropertyValue) MLTextPropertyValue(org.alfresco.solr.client.MLTextPropertyValue) ContentPropertyValue(org.alfresco.solr.client.ContentPropertyValue) MultiPropertyValue(org.alfresco.solr.client.MultiPropertyValue) PropertyValue(org.alfresco.solr.client.PropertyValue)

Example 25 with NodeMetaData

use of org.alfresco.solr.client.NodeMetaData in project SearchServices by Alfresco.

the class SolrInformationServer method indexNode.

@Override
public void indexNode(Node node, boolean overwrite) throws IOException, AuthenticationException, JSONException {
    SolrQueryRequest request = null;
    UpdateRequestProcessor processor = null;
    try {
        request = getLocalSolrQueryRequest();
        processor = this.core.getUpdateProcessingChain(null).createProcessor(request, new SolrQueryResponse());
        long start = System.nanoTime();
        if ((node.getStatus() == SolrApiNodeStatus.DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_UPDATED) || (node.getStatus() == SolrApiNodeStatus.UNKNOWN)) {
            // fix up any secondary paths
            NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
            nmdp.setFromNodeId(node.getId());
            nmdp.setToNodeId(node.getId());
            List<NodeMetaData> nodeMetaDatas;
            if ((node.getStatus() == SolrApiNodeStatus.DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_DELETED) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_UPDATED)) {
                // Fake the empty node metadata for this parent deleted node
                NodeMetaData nodeMetaData = createDeletedNodeMetaData(node);
                nodeMetaDatas = Collections.singletonList(nodeMetaData);
            } else {
                nodeMetaDatas = repositoryClient.getNodesMetaData(nmdp, Integer.MAX_VALUE);
            }
            NodeMetaData nodeMetaData = null;
            if (!nodeMetaDatas.isEmpty()) {
                nodeMetaData = nodeMetaDatas.get(0);
                if (!(nodeMetaData.getTxnId() > node.getTxnId())) {
                    if (node.getStatus() == SolrApiNodeStatus.DELETED) {
                        try {
                            // Lock the node to ensure that no other trackers work with this node until this code completes.
                            if (!spinLock(nodeMetaData.getId(), 120000)) {
                                // We haven't acquired the lock in over 2 minutes. This really shouldn't happen unless something has gone wrong.
                                throw new Exception("Unable to acquire lock on nodeId:" + nodeMetaData.getId());
                            }
                            solrContentStore.removeDocFromContentStore(nodeMetaData);
                        } finally {
                            unlock(nodeMetaData.getId());
                        }
                    }
                }
            // else, the node has moved on to a later transaction, and it will be indexed later
            }
            if (log.isDebugEnabled()) {
                log.debug(".. deleting");
            }
            deleteNode(processor, request, node);
        }
        if ((node.getStatus() == SolrApiNodeStatus.UPDATED) || (node.getStatus() == SolrApiNodeStatus.UNKNOWN) || (node.getStatus() == SolrApiNodeStatus.NON_SHARD_UPDATED)) {
            log.info(".. updating");
            long nodeId = node.getId();
            try {
                if (!spinLock(nodeId, 120000)) {
                    // We haven't acquired the lock in over 2 minutes. This really shouldn't happen unless something has gone wrong.
                    throw new Exception("Unable to acquire lock on nodeId:" + nodeId);
                }
                NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
                nmdp.setFromNodeId(node.getId());
                nmdp.setToNodeId(node.getId());
                List<NodeMetaData> nodeMetaDatas = repositoryClient.getNodesMetaData(nmdp, Integer.MAX_VALUE);
                AddUpdateCommand addDocCmd = new AddUpdateCommand(request);
                addDocCmd.overwrite = overwrite;
                if (!nodeMetaDatas.isEmpty()) {
                    NodeMetaData nodeMetaData = nodeMetaDatas.get(0);
                    if (!(nodeMetaData.getTxnId() > node.getTxnId())) {
                    /*
                        if (mayHaveChildren(nodeMetaData))
                        {
                            cascadeUpdate(nodeMetaData, overwrite, request, processor);
                        }
            				 */
                    }
                    if (node.getTxnId() == Long.MAX_VALUE) {
                        // This is a re-index. We need to clear the txnId from the pr
                        this.cleanContentCache.remove(nodeMetaData.getTxnId());
                    }
                    if ((node.getStatus() == SolrApiNodeStatus.UPDATED) || (node.getStatus() == SolrApiNodeStatus.UNKNOWN)) {
                        // check index control
                        Map<QName, PropertyValue> properties = nodeMetaData.getProperties();
                        StringPropertyValue pValue = (StringPropertyValue) properties.get(ContentModel.PROP_IS_INDEXED);
                        if (pValue != null) {
                            Boolean isIndexed = Boolean.valueOf(pValue.getValue());
                            if (!isIndexed.booleanValue()) {
                                if (log.isDebugEnabled()) {
                                    log.debug(".. clearing unindexed");
                                }
                                deleteNode(processor, request, node);
                                SolrInputDocument doc = createNewDoc(nodeMetaData, DOC_TYPE_UNINDEXED_NODE);
                                solrContentStore.storeDocOnSolrContentStore(nodeMetaData, doc);
                                addDocCmd.solrDoc = doc;
                                processor.processAdd(addDocCmd);
                                long end = System.nanoTime();
                                this.trackerStats.addNodeTime(end - start);
                                return;
                            }
                        }
                        // Make sure any unindexed or error doc is removed.
                        if (log.isDebugEnabled()) {
                            log.debug(".. deleting node " + node.getId());
                        }
                        deleteNode(processor, request, node);
                        SolrInputDocument doc = createNewDoc(nodeMetaData, DOC_TYPE_NODE);
                        addToNewDocAndCache(nodeMetaData, doc);
                        addDocCmd.solrDoc = doc;
                        processor.processAdd(addDocCmd);
                    }
                }
            // Ends checking for a nodeMetaData
            } finally {
                unlock(nodeId);
            }
        }
        // Ends checking for updated or unknown node status
        long end = System.nanoTime();
        this.trackerStats.addNodeTime(end - start);
    } catch (Exception e) {
        log.warn("Node index failed and skipped for " + node.getId() + " in Tx " + node.getTxnId(), e);
        if (processor == null) {
            if (request == null) {
                request = getLocalSolrQueryRequest();
            }
            processor = this.core.getUpdateProcessingChain(null).createProcessor(request, new SolrQueryResponse());
        }
        if (log.isDebugEnabled()) {
            log.debug(".. deleting on exception");
        }
        deleteNode(processor, request, node);
        AddUpdateCommand addDocCmd = new AddUpdateCommand(request);
        addDocCmd.overwrite = overwrite;
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField(FIELD_SOLR4_ID, PREFIX_ERROR + node.getId());
        doc.addField(FIELD_VERSION, "0");
        doc.addField(FIELD_DBID, node.getId());
        doc.addField(FIELD_INTXID, node.getTxnId());
        doc.addField(FIELD_EXCEPTION_MESSAGE, e.getMessage());
        doc.addField(FIELD_DOC_TYPE, DOC_TYPE_ERROR_NODE);
        StringWriter stringWriter = new StringWriter(4096);
        PrintWriter printWriter = new PrintWriter(stringWriter, true);
        try {
            e.printStackTrace(printWriter);
            String stack = stringWriter.toString();
            doc.addField(FIELD_EXCEPTION_STACK, stack.length() < 32766 ? stack : stack.substring(0, 32765));
        } finally {
            printWriter.close();
        }
        addDocCmd.solrDoc = doc;
        processor.processAdd(addDocCmd);
    } finally {
        if (processor != null) {
            processor.finish();
        }
        if (request != null) {
            request.close();
        }
    }
}
Also used : StringPropertyValue(org.alfresco.solr.client.StringPropertyValue) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) NodeMetaData(org.alfresco.solr.client.NodeMetaData) QName(org.alfresco.service.namespace.QName) StringPropertyValue(org.alfresco.solr.client.StringPropertyValue) MLTextPropertyValue(org.alfresco.solr.client.MLTextPropertyValue) ContentPropertyValue(org.alfresco.solr.client.ContentPropertyValue) MultiPropertyValue(org.alfresco.solr.client.MultiPropertyValue) PropertyValue(org.alfresco.solr.client.PropertyValue) JSONException(org.json.JSONException) AuthenticationException(org.alfresco.httpclient.AuthenticationException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) StringWriter(java.io.StringWriter) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) NodeMetaDataParameters(org.alfresco.solr.client.NodeMetaDataParameters) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) PrintWriter(java.io.PrintWriter)

Aggregations

NodeMetaData (org.alfresco.solr.client.NodeMetaData)25 ArrayList (java.util.ArrayList)11 AclChangeSet (org.alfresco.solr.client.AclChangeSet)10 Term (org.apache.lucene.index.Term)10 TermQuery (org.apache.lucene.search.TermQuery)10 Test (org.junit.Test)10 Node (org.alfresco.solr.client.Node)9 Transaction (org.alfresco.solr.client.Transaction)9 AlfrescoSolrUtils.getAcl (org.alfresco.solr.AlfrescoSolrUtils.getAcl)8 AlfrescoSolrUtils.getAclChangeSet (org.alfresco.solr.AlfrescoSolrUtils.getAclChangeSet)8 AlfrescoSolrUtils.getAclReaders (org.alfresco.solr.AlfrescoSolrUtils.getAclReaders)8 AlfrescoSolrUtils.getNode (org.alfresco.solr.AlfrescoSolrUtils.getNode)8 AlfrescoSolrUtils.getNodeMetaData (org.alfresco.solr.AlfrescoSolrUtils.getNodeMetaData)8 AlfrescoSolrUtils.getTransaction (org.alfresco.solr.AlfrescoSolrUtils.getTransaction)8 AlfrescoSolrUtils.indexAclChangeSet (org.alfresco.solr.AlfrescoSolrUtils.indexAclChangeSet)8 Acl (org.alfresco.solr.client.Acl)8 AclReaders (org.alfresco.solr.client.AclReaders)8 SolrInputDocument (org.apache.solr.common.SolrInputDocument)8 IOException (java.io.IOException)6 NodeMetaDataParameters (org.alfresco.solr.client.NodeMetaDataParameters)6