Created
December 16, 2020 06:24
-
-
Save YasuhiroABE/e066b5c6d15c21757588dd2e64d7a506 to your computer and use it in GitHub Desktop.
A ruby script for setting up apache solr schema and field-types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# coding: utf-8 | |
def usage | |
puts <<EOF | |
It posts the solr schema data to the server. | |
Usage: $ #{$0} operation files.. | |
opration: | |
* add | |
* delete | |
* update | |
files: | |
* list of file path | |
Example of the field-type file | |
{ | |
"field-type": { | |
"name": "text_ja_edgengram", | |
"class": "solr.TextField", | |
"autoGeneratePhraseQueries": "true", | |
"positionIncrementGap": "100", | |
"analyzer": { | |
"charFilter": [ | |
{ | |
"class": "solr.ICUNormalizer2CharFilterFactory" | |
} | |
], | |
"tokenizer": { | |
"class": "solr.EdgeNGramTokenizerFactory", | |
"minGramSize": "1", | |
"maxGramSize": "1" | |
}, | |
"filter": [ | |
{ | |
"class": "solr.CJKWidthFilterFactory", | |
"class": "solr.LowerCaseFilterFactory" | |
} | |
] | |
} | |
} | |
} | |
Example of the field file | |
{ | |
"field" : { | |
"name" : "content_edgengram", | |
"type" : "text_ja_edgengram", | |
"multiValued" : "true", | |
"indexed" : "true", | |
"required" : "true", | |
"stored" : "true" | |
} | |
} | |
EOF | |
end | |
$: << "./lib" | |
require 'bundler/setup' | |
require 'json' | |
require 'httpclient' | |
OPTDEL_LABEL = "delete" | |
OPTADD_LABEL = "add" | |
OPTUPD_LABEL = "update" | |
OPERATIONS = [ OPTDEL_LABEL, OPTADD_LABEL, OPTUPD_LABEL ] | |
KEY_LABEL = { "field" => { OPTDEL_LABEL => "delete-field", | |
OPTADD_LABEL => "add-field", | |
OPTUPD_LABEL => "replace-field" }, | |
"field-type" => { OPTDEL_LABEL => "delete-field-type", | |
OPTADD_LABEL => "add-field-type", | |
OPTUPD_LABEL => "replace-field-type" } | |
} | |
SOLR_URL = "http://localhost:8983/solr/solrbook/schema" | |
operation = nil | |
if ARGV.length > 0 and OPERATIONS.include?(ARGV[0]) | |
operation = ARGV.shift | |
else | |
usage() | |
exit 1 | |
end | |
ARGV.each { |f| | |
ret = {} | |
json = nil | |
open(f) {|data| | |
json = JSON.load(data) | |
} | |
if json and json.keys.length == 1 | |
## prepare variables | |
data_type = json.keys[0] ## one of "field" or "field-type" | |
key_name = KEY_LABEL[data_type][operation] | |
## setup the binary data | |
if operation == OPTDEL_LABEL | |
ret[key_name] = {} | |
ret[key_name]["name"] = json[data_type]["name"] | |
elsif operation == OPTADD_LABEL or operation == OPTUPD_LABEL | |
ret[key_name] = json[data_type] | |
end | |
end | |
puts ret | |
client = HTTPClient.new | |
resp = client.post(SOLR_URL, ret.to_json, "Content-Type" => "application/json") | |
puts resp.body | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment