2016-11-16 53 views
1

我在雲模式下配置Solr,並且想要配置自定義查詢解析器。 來配置它我正在使用PowerShell腳本,如下所示。如何配置Solr自定義查詢解析器?

# 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,並能夠搜索到我的數據,而無需過濾查詢 但過濾器查詢它沒有給出任何數據,你能告訴我是否失去了一些東西?

回答

0

我認爲線索是在腳本的最後幾行:

$addQueryParserData = @{"create-queryparser" = @{name = "acl"; runtimeLib = $true; class = "customQueryPlugin.customSearchQueryParser"}} | ConvertTo-Json; 

你確定你有$true變量:runtimeLib = $true?我敢打賭它應該是runtimeLib = true

還值得注意的是您的查詢分析器類的名稱:class = "customQueryPlugin.customSearchQueryParser"。請確保您的customQueryPlugin.jar確實將其作爲完全限定名稱。

+0

$ true是powershell的東西,因爲它不明白,也檢查過插件的名稱。 –

+0

這個腳本運行後有輸出嗎?任何錯誤/例外? –