如何配置Solr自定义查询解析器?

时间:2016-11-16 05:51:39

标签: powershell solr powershell-v3.0 solrcloud

我正在云模式下配置Solr,并且想要配置自定义查询解析器。 配置它我使用PowerShell脚本如下。

# Variables ###########
$hostName = 'localhost'
$port = 8983
$numberOfShards = 1
$replicationFactor = 1

# These should be picked from an external file
$collections = 'Collection1', 'Collection2', 'Collection3', 'Collection4', 'Collection5', 'Collection6', 'Collection7', 'Collection8', 'Collection9';
#######################

# Following 3 commands needs file system access and can be extracted out of this script in case we want to use already hosted Solr instance on cloud
# Start Solr in Cloud Mode
'####### Starting Solr #######';
..\bin\solr start -c -h $hostName -p $port "-Denable.runtime.lib=true"

# Upload configuration to ZooKeeper
# We need to upload multiple copies of the configuration to the ZooKeeper one for each collection, so each collection can have it's different schema
$zooKeeperPort = $port + 1000;
foreach ($c in $collections) {
    $configName = $c + 'SearchConfig';
    '####### Uploading Config to ZooKeeper for collection {0} #######' -f $c;
    $configPath = './xuber_basic_config/conf/';
    ..\server\scripts\cloud-scripts\zkcli.bat -zkhost localhost:$zooKeeperPort -cmd upconfig -confdir $configPath -confname $configName;
}

$urlPrefix = 'http://' + $hostName + ':' + $port + '/solr';

function PostToSolr
{
   'HTTP POST: {0}' -f $args[0];
   Invoke-WebRequest -uri $args[0] -Method POST -ContentType "application/json" -Body $args[1];
}

function GetToSolr
{
    'HTTP GET: {0}' -f $args[0];
    Invoke-WebRequest -uri $args[0] -Method GET -ContentType "application/json";
}

function PostSchema
{
    $schemaFileName = $args[0];
    $url = $args[1];
    $jsonToPost = Get-Content ./$schemaFileName;
    PostToSolr $url $jsonToPost;
}

# Get a list of collections already present in Solr
$readCollectionUrl = $urlPrefix + '/admin/collections?action=LIST&wt=json';
$alreadyExistingCollections = ((GetToSolr $readCollectionUrl).Content | ConvertFrom-Json).collections;

# Create .system collection
$systemCollectionName = '.system';
If ($alreadyExistingCollections -contains $systemCollectionName) {
    $deleteCollectionUrl = $urlPrefix + '/admin/collections?action=DELETE&name={0}' -f $systemCollectionName;
    GetToSolr $deleteCollectionUrl;
}

$addSystemCollectionPath =  $urlPrefix + '/admin/collections?action=CREATE&name=' + $systemCollectionName;
GetToSolr $addSystemCollectionPath;

$dataSecurityPlugin = 'customQueryPlugin';
$addBlobPath = $urlPrefix + '/'+ $systemCollectionName + '/blob/' + $dataSecurityPlugin;
'HTTP POST: {0}' -f $addBlobPath;
Invoke-WebRequest -uri $addBlobPath -Method Post -InFile .\customQueryPlugin.jar -ContentType application/octet-stream;

$createCollectionPrefix = $urlPrefix + '/admin/collections?action=CREATE&name={0}&numShards={1}&replicationFactor={2}&collection.configName={3}';
$schemaApiUrlPrefix = $urlPrefix + '/{0}/schema';

# Create Solr Collections
foreach ($c in $collections) {
    # Check to see if the collection already exists, if yes, delete it.
    If ($alreadyExistingCollections -contains $c) {
        '####### Collection {0} already present hence deleting it #######' -f $c
        $deleteCollectionUrl = $urlPrefix + '/admin/collections?action=DELETE&name={0}' -f $c;
        GetToSolr $deleteCollectionUrl;
    }

    '####### Creating collection {0} #######' -f $c;
    $configName = $c + 'SearchConfig';
    $tempPath = $createCollectionPrefix -f $c,$numberOfShards,$replicationFactor,$configName;
    GetToSolr $tempPath;

    # Add data security plugin to runtime
    $addToRuntimePath = $urlPrefix + '/'+ $c + '/config';
    $runtimeJsonData = @{"add-runtimelib" = @{name = "customQueryPlugin"; version = 1}} | ConvertTo-Json;
    $runtimeJsonData;
    PostToSolr $addToRuntimePath $runtimeJsonData;

    $schemaAddUrl = $schemaApiUrlPrefix -f $c;
    # Push the common Schema
    '####### Adding common schema for collection {0} #######' -f $c;
    $schemaFileName = 'Common_Schema.json';
    PostSchema $schemaFileName $schemaAddUrl;

    # Push the Schema specific to the collection
    '####### Adding specific schema for collection {0} #######' -f $c;
    $schemaFileName = $c + '_Schema.json';
    PostSchema $schemaFileName $schemaAddUrl;

    # Register QueryParser
    $addQueryParserPath = $addToRuntimePath;
    $addQueryParserData = @{"create-queryparser" = @{name = "acl"; runtimeLib = $true; class = "customQueryPlugin.customSearchQueryParser"}} | ConvertTo-Json;
    PostToSolr $addQueryParserPath $addQueryParserData;
}

我正在使用edismax,并且能够在没有过滤器查询的情况下搜索我的数据 但是使用过滤器查询它没有提供任何数据,你能判断我是否遗漏了什么吗?

1 个答案:

答案 0 :(得分:0)

我认为线索在你脚本的最后几行:

double heightpixel = metrics.heightPixels;
double widthpixel = metrics.widthPixels;

您确定$addQueryParserData = @{"create-queryparser" = @{name = "acl"; runtimeLib = $true; class = "customQueryPlugin.customSearchQueryParser"}} | ConvertTo-Json; 变量:$true?我打赌它应该是runtimeLib = $true

还值得注意的是Query Parser类的名称:runtimeLib = true。请确保您的class = "customQueryPlugin.customSearchQueryParser"确实将其作为完全限定名称。