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, [switch] $incremental, $entity) {
    $ht = @{ core = $solr.core; command = "full-import"; clean = L($clean); verbose = L($verbose); commit = L($commit); optimize = L($optimize); incremental = L($incremental) };
    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
$now = [DateTime]::UtcNow;
 
if( $now.Hour -ge 20 -and $now.Hour -le 21) {
    Write-Host "Full crawl";
} else {
    Write-Host "Differential crawl";
}
 

Start-Transcript -path "my-log.log" -append
 
$ErrorActionPreference = "Stop"
$solr = Create-SolrContext -core "{CoreName}" -user "{SolrUser}" -pass "{SolrPassword}" -url "{https://solrurl:port/solr}"
if($now.Hour -eq 2) {
   #do full import
   Dataimport-Full -solr $solr -commit -clean
}
else {
# do incremental import
   Dataimport-Full -solr $solr -commit -incremental
}
 
Stop-Transcript

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

Czy artykuł był pomocny?