Index search results using Powershell

In case the traditional indexing is less efficient on your environment, you can use a Powershell script to perform this action.


Configure the following information in a script:

{KanBo Core Name} - your KanBo core,

{Solr admin} - Solr admin name,

{Solr password} - Solr user password,

https://{SOLR url}:{port} - URL To Solr, example: https://localhost:660


# utility methods
function AllowAllCertificates() {
    Add-Type @"
    public class Certs {
        public static void AllowAll() {
                System.Net.ServicePointManager.ServerCertificateValidationCallback += (a, b, c, d) => true;
        }
    }
"@
    [Certs]::AllowAll();
}
Add-Type -AssemblyName System.Web.Extensions
function Create-SolrContext($url, $core, $user, $pass) {
    $secPass = ConvertTo-SecureString $pass -AsPlainText -Force
    $credential = New-Object System.Management.Automation.PSCredential($user, $secPass)
    return @{ url = $url; core = $core; credential = $credential };
}
function Invoke-SolrRequest($solr, $body) {
    $url = $solr.url;
    $core = $solr.core;
    $credential = $solr.credential;
    $serializer = New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer;
    $root = $url + "/solr/" + $core + "/dataimport?wt=json";
    $json = Invoke-WebRequest $root -Credential $credential -Method POST -Body $body;
    $result = $serializer.DeserializeObject($json);
    return $result;
}
function L($b) {
    if($b) {
        return "true";
    } else {
        return "false";
    }
}
function Dataimport-Full($solr, [switch] $clean, [switch] $verbose, [switch] $commit, [switch] $optimize, $entity) {
    $ht = @{ command = "full-import"; clean = L($clean); verbose = L($verbose); commit = L($commit); optimize = L($optimize) };
    if($entity) {
        $ht += @{ entity = $entity };
    }
    $r = Invoke-SolrRequest -solr $solr -body $ht;
    Dataimport-WaitUntilIdle($solr);
}
function Dataimport-WaitUntilIdle($solr) {
    while($True) {
        $result = Invoke-SolrRequest -solr $solr -body @{ command = "status" };
        Write-Host $result['statusMessages']
        if($result['status'] -eq 'idle') {
            return;
        }
        sleep(1)
    }
}
# the example script
AllowAllCertificates
Start-Transcript -path "my-log.log" -append
$ErrorActionPreference = "Stop"
$solr = Create-SolrContext -core "{KanBo Core Name}" -user "{Solr admin}" -pass "{Solr password}" -url "https://{SOLR url}:{port}"
# do full import
Dataimport-Full -solr $solr -commit -clean
# import each entity separately
foreach($e in "board", "notification", "user", "document") {
    Write-Host -ForegroundColor Yellow "Importing $e"
    Dataimport-Full -solr $solr -entity $e -commit;
    Write-Host -ForegroundColor Green "Imported $e"
}
Stop-Transcript


Now you should add this script to be run in Task scheduler.


Is this article helpful for you?