#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Run_AU3Check=Y #AutoIt3Wrapper_AU3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include-once #include "..\_Json(2019.01.17)\Json.au3" ;https://solr.apache.org/guide/7_0/implicit-requesthandlers.html#implicit-requesthandlers Global Const $SOLR_HTTP_STATUS_OK = 200, _ ; Solr recieved a readable query and was able to give a response $SOLR_HTTP_BAD_REQUEST = 400, _ ; exceeds upload limit, malformed query $SOLR_HTTP_FORBIDDEN = 403, _ ; Auth issue $SOLR_HTTP_NOT_FOUND = 404, _ ; incorrect container specified? $SOLR_HTTP_UNAUTHORIZED = 401, _ ;Auth issue ==> https://solr.apache.org/guide/7_0/kerberos-authentication-plugin.html#browser-configuration $SOLR_HTTP_VERSION_CONFLICT = 409, _ ; https://yonik.com/solr/optimistic-concurrency/ $SOLR_SOLR_HTTP_UNSUPPORTED = 415, _ ; tried to put multiple values in a single value field? $SOLR_HTTP_SERVER_ERROR = 500, _ ; possible unclean shutdown, mem issue $SOLR_HTTP_SERVICE_UNAVAILABLE = 503, _ ;retry request? ==> https://docs.websolr.com/article/170-http-503 $SOLR_HTTP_UNKNOWN = 0 ; possible solr timeout ==> https://hostedapachesolr.com/support/internal-solr-server-error-when-indexing Global Const $SOLR_START_MAX = 45000, _ $SOLR_JARPOST_MAX = 20000, _ $SOLR_JAVA_HOME = EnvGet('SOLR_JAVA_HOME'), _ $SOLR_HOME = 'C:\solr-8.10.1' ;EnvGet('SOLR_HOME') Global $obj_HTTP = ObjCreate('WinHttp.WinHttpRequest.5.1') Example() Func Example() Local $sHost = '127.0.0.1', $sPort = 8983, $sContainer = 'techproducts', $sWow64 = "" If @AutoItX64 Then $sWow64 = "\Wow6432Node" Local $dir_autoit = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE" & $sWow64 & "\AutoIt v3\AutoIt", "InstallDir") Local $st_URL = 'http://' & $sHost & ':' & $sPort & '/solr/' & $sContainer If _solr_Webping($st_URL) = -1 Then _solr_cmdstart($SOLR_HOME, $sContainer) _solr_enablestreamoverlay($st_URL) _solr_Webdelall($st_URL, True) Local $aPairs[5][2] = [ _ [4, Default], _ ['literal.id', 5], _ ['literal.value_s', 'anything'], _ ['literal.resourcename', 'API Errors Constants'], _ ['literal.indextime_dt',_solr_tnow()]] _Solr_uploadPkg($dir_autoit & '\Include\APIErrorsConstants.au3', $st_URL, $aPairs, Null, 'commit=true&overrideLiterals=true') _solr_Webupdate($st_URL, '[{"id":"' & 5 & '","value_s" : {"set":"something"}}]') _solr_Webcommit($st_URL) _solr_jarpost($SOLR_HOME & '\example\exampledocs\post.jar', $sContainer, $dir_autoit & '\Include\GuiListView.au3', 'literal.id=6&literal.cat=Help Files&commit=true') Local $qry_response = _solr_Webselect($st_URL, 'q=$ERROR_VOLSNAP_PREPARE_HIBERNATE') Local $json_response = Json_Encode($qry_response) json_dump($json_response) ConsoleWrite(json_get($qry_response, '.response.docs[0].content[0]') & @CRLF) EndFunc ;==>Example ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_uploadpkg ; Description ...: Packages a file and key/value pairs (2D array) for a multipart upload/extract ; Syntax.........: _solr_uploadPkg($strFilePath, $st_URL, $aPairs[, $int_max = Null[, $str_Params = '']]) ; Parameters ....: $strFilePath - File to package ; $st_URL - Solr URL ; $aPairs - A 2 dimensional array wherein [n][0] = key, [n][1] = value ; $int_max - fileread byte truncate limit ; $str_Params - Solr Parameters such as commit and overrideLiterals ; Return values .: Success ; ; Failure ; Author ........: mushy ; Modified.......: 12/28/2021 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _Solr_uploadPkg($strFilePath, $st_URL, $aPairs, $int_max = Null, $str_Params = '') ;https://doc.lucidworks.com/solr-reference-guide/8888/uploading-data-with-solr-cell-using-apache-tika If Not FileExists($strFilePath) Then Return SetError(4, 0, 0) Local $strFilename = StringMid($strFilePath, StringInStr($strFilePath, "\", 0, -1) + 1) Local $smBoundary = "----WebKitFormBoundary" Local $aSpace[3] For $i = 1 To 16 $aSpace[0] = Chr(Random(65, 90, 1)) ;A-Z $aSpace[1] = Chr(Random(97, 122, 1)) ;a-z $aSpace[2] = Chr(Random(48, 57, 1)) ;0-9 $smBoundary &= $aSpace[Random(0, 2, 1)] Next Local $h = FileOpen($strFilePath, 16) ;$FO_BINARY Local $bytFile If $int_max <> Null Then $bytFile = FileRead($h, $int_max) Else $bytFile = FileRead($h) EndIf FileClose($h) Local $strFormEnd = @CRLF & "--" & $smBoundary & "--" & @CRLF Local $strFormStart For $u = 1 To $aPairs[0][0] $strFormStart &= "--" & $smBoundary & @CRLF & _ "Content-Disposition: form-data; " & _ "name=""" & $aPairs[$u][0] & """" & _ @CRLF & @CRLF & _ $aPairs[$u][1] & @CRLF Next $strFormStart &= "--" & $smBoundary & @CRLF & _ "Content-Disposition: form-data; " & _ "name=""" & '' & """; " & _ "filename=""" & $strFilename & """" & @CRLF & _ "Content-Type: application/upload" & _ ; bogus, but it works @CRLF & @CRLF Local $bytFormData = StringToBinary($strFormStart) & $bytFile & StringToBinary($strFormEnd) Local $str_ExtractURL = $st_URL & '/update/extract?' If $str_Params Then $str_ExtractURL &= $str_Params Local $obj_Json = _solr_PostMultiPart($str_ExtractURL, $smBoundary, $bytFormData) Return $obj_Json EndFunc ;==>_Solr_uploadPkg ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_rstat ; Description ...: Validates a Solr Json response by reading the responseHeader status and providing a boolean output ; Syntax.........: _solr_rstat($obj_Json) ; Parameters ....: $obj_Json - A json object response ; Return values .: Success - True ; Failure - False ; Author ........: mushy ; Modified.......: 11/18/2021 ; Remarks .......: Designed to be used as an expression to validate the contents of a json response ; Related .......: all _solr_Web functions ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_rstat($obj_Json) Switch json_Get($obj_Json, '["responseHeader.status"]') Case 0 Return True Case Else Return False EndSwitch EndFunc ;==>_solr_rstat ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_enablestreamoverlay ; Description ...: Enables RemoteStreaming and StreamBody within a configuration overlay ; Syntax.........: _solr_enablestreamoverlay($st_URL) ; Parameters ....: $st_URL - Solr URL ; Return values .: Success - Returns 0 and sets @extended to QTime if any operation was required ; Failure - Returns -1 and sets @error and @extended to -1 ; No Solr - Returns 1 and sets @error and @extended to 1 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: Permanent configuration changes should be made to the schema in XML ; Related .......: _solr_Webgetconfig, _solr_Websetconfig ; Link ..........: - https://solr.apache.org/guide/7_4/config-api.html ; - https://solr.apache.org/guide/7_5/content-streams.html#content-stream-sources ; - http://localhost:8983/solr/techproducts/config/overlay ; Example .......: ; =============================================================================================================================== Func _solr_enablestreamoverlay($st_URL) If _solr_Webping($st_URL) = False Then Return SetError(-1, -1, -1) Local $obj_Json, _ $stream_body = 'requestDispatcher.requestParsers.enableStreamBody', _ $remote_stream = 'requestDispatcher.requestParsers.enableRemoteStreaming' Local $boo_streambody = _solr_Webgetconfig($st_URL, 1, 'overlay.props.' & $stream_body) If (Not $boo_streambody) Then Json_Put($obj_Json, '["set-property"]["' & $stream_body & '"]', 'True') Local $boo_remotestreaming = _solr_Webgetconfig($st_URL, 1, 'overlay.props.' & $remote_stream) If (Not $boo_remotestreaming) Then Json_Put($obj_Json, '["set-property"]["' & $remote_stream & '"]', 'True') If $obj_Json Then _solr_Websetconfig($st_URL, Json_Encode($obj_Json)) Switch @error Case 0 Return SetError(0, @extended, 0) Case Else Return SetError(1, 1, 1) EndSwitch Return SetError(0, 0, 0) EndFunc ;==>_solr_enablestreamoverlay Func _solr_status($sHost, $iPort) Local $url_stat = 'http://' & $sHost & ':' & $iPort & '/solr/admin/cores?action=STATUS' Local $obj_Json = _solr_GetJson($url_stat) If _solr_rstat($obj_Json) Then ;Local $containers = json_Get($obj_Json,'.status') Local $name = Json_Get($obj_Json, '["status"][0]["name"]') ;Local $count = Json_ObjGetCount($containers) ConsoleWrite($name & @CRLF) ;.status.techproducts.name EndIf EndFunc ;==>_solr_status ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_jarpost ; Description ...: Posts documents to solr using the Java SimplePostTool ; Syntax.........: _solr_jarpost($str_PostJar, $str_Container, $slr_sFile[, $strDataPairs = '']) ; Parameters ....: $str_PostJar - ; $str_Container - ; $slr_sFile - ; $strDataPairs - ; Return values .: Success - ; Failure - ; Author ........: mushy - ; Modified.......: 11/20/2021 ; Remarks .......: ; Related .......: _solr_Webpostdoc ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_jarpost($str_PostJar, $str_Container, $slr_sFile, $strDataPairs = '') ;https://solr.apache.org/guide/6_6/post-tool.html#simpleposttool If (Not FileExists($str_PostJar)) Then Return SetError(4, 0, 0) Local $timeout = TimerInit(), $i, $sOutput = '' If $strDataPairs <> '' And StringInStr($strDataPairs, '=') = 0 Then Return SetError(1, 1, 1) Local $str_cmd = 'java -jar -Dc=' & $str_Container & ' -Dauto' If $strDataPairs Then $str_cmd &= ' -Dparams="' Local $split = StringSplit($strDataPairs, "&") For $i = 1 To $split[0] Local $splitagain = StringSplit($split[$i], "=") ;$slr_jarpost_cmd &= 'literal.' & $splitagain[1] & '=' & __URLEncode($splitagain[2]) $str_cmd &= $splitagain[1] & '=' & __URLEncode($splitagain[2]) Switch $i Case $split[0] $str_cmd &= '"' ;end Case Else $str_cmd &= '&' ;another EndSwitch Next EndIf $str_cmd &= ' "' & $str_PostJar & '" "' & $slr_sFile & '"' Local $iPID = Run($str_cmd, '', @SW_HIDE, BitOR(4, 2)) While 1 $sOutput &= StdoutRead($iPID) Select Case @error Or (Not ProcessExists($iPID)) ExitLoop Case TimerDiff($timeout) > $SOLR_JARPOST_MAX Return SetError(3, TimerDiff($timeout), 1) ;timeout EndSelect WEnd ;ConsoleWrite($sOutput & @CRLF) Select Case StringInStr($sOutput, '1 files indexed') > 0 Return SetError(0, TimerDiff($timeout), 0) Case StringInStr($sOutput, '0 files indexed') > 0 Return SetError(1, TimerDiff($timeout), 1) Case Else Return SetError(2, TimerDiff($timeout), 1) EndSelect EndFunc ;==>_solr_jarpost ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_cmdstart ; Description ...: Starts Apache Solr ; Syntax.........: _solr_cmdstart($sCMD, $sInstance[, $sWait = 0[, $sExample = 0]]) ; Parameters ....: $_sSolrCMD - Path to solr.cmd ; replace with SOLR_HOME ; $sInstance - Name of Solr instance ; $sWait - Maximum wait time before returning ; $sExample - Boolean value that designates if this is an example (loads example configs and documents) ; Return values .: Success - The console output stream of the Solr Start command, sets @error to 0 and @extended to the PID ; of the new Solr Java process????????? ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: ; Related .......: _solr_cmdstop ; Link ..........: https://solr.apache.org/guide/8_10/installing-solr.html ; https://solr.apache.org/guide/7_1/taking-solr-to-production.html ; Example .......: ; =============================================================================================================================== Func _solr_cmdstart($_sSolrHome, $sInstance, $sExample = False) Local $str_PortFile = $_sSolrHome & '\bin\solr-8983.port', _ $str_SolrCMD = $_sSolrHome & '\bin\solr.cmd' If Not FileExists($str_SolrCMD) Then Return SetError(4, 0, 0) If FileExists($str_PortFile) Then FileDelete($str_PortFile) Local $tOut = TimerInit(), $sOutput = '', _ $str_cmd = '"' & $str_SolrCMD & '" start' & ' ' Switch $sExample Case True $str_cmd &= '-e ' & $sInstance Case Else $str_cmd &= '-s "..\example\' & $sInstance & '\solr"' EndSwitch Local $iPID = Run($str_cmd, '', @SW_HIDE, BitOR(4, 2)) While 1 $sOutput &= StdoutRead($iPID) Select Case @error Or (Not ProcessExists($iPID)) Or FileExists($str_PortFile) ExitLoop Case TimerDiff($tOut) > $SOLR_START_MAX ExitLoop EndSelect WEnd Select Case StringInStr($sOutput, "Happy searching!") > 0 SetError(0) ;good Case StringInStr($sOutput, "ERROR: Solr home directory") > 0 SetError(1) ; container error Case StringInStr($sOutput, "Please set the JAVA_HOME environment variable") > 0 SetError(2) ; no java Case StringInStr($sOutput, "is already listening on port") > 0 ;ERROR: Process 20716 is already listening on port 8983. If this is Solr, please stop it first SetError(0) Case Else SetError(3) ; unknown EndSelect ConsoleWrite($sOutput & @CRLF) Return $sOutput EndFunc ;==>_solr_cmdstart ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_cmdstop ; Description ...: Stops a running instance of Solr and optionally waits to return the STDOUT stream ; Syntax.........: _solr_cmdstop($_sSolrCMD[, $sWait = 5000[, $_sSolrPort = 8983]]) ; Parameters ....: $_sSolrCMD - Path to solr.cmd ; $sWait - Maximum wait time, can be set to 0 ; $_sSolrPort - Port of Solr instance to stop ; Return values .: Success - The console output stream of the Solr Stop command, sets @error to 0 and @extended to the PID ; of the stop command used. ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: The ouput stream could be parsed to return the PID of the process that was just stopped instead of providing ; the PID of the process that ended it.???????????????? ; Related .......: _solr_cmdstart ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_cmdstop($_sSolrHome, $sWait = 5000, $_sSolrPort = '') Local $pid_stop, $sOutput = '', $tOut = TimerInit() If $_sSolrPort And IsInt($_sSolrPort) Then $pid_stop = Run('"' & $_sSolrHome & '\bin\solr.cmd" stop -p ' & $_sSolrPort, '', @SW_HIDE, BitOR(4, 2)) Else $pid_stop = Run('"' & $_sSolrHome & '\bin\solr.cmd" stop -all', '', @SW_HIDE, BitOR(4, 2)) EndIf While $sWait > 0 $sOutput &= StdoutRead($pid_stop) Select Case @error ; eof ExitLoop Case TimerDiff($tOut) > $sWait ExitLoop EndSelect WEnd Switch StringInStr($sOutput, 'Stopping Solr process') Case 0 Return SetError(-1, -1, $sOutput) Case Else Local $pid_solr = Int(StringRegExp($sOutput, '(?:process )([0-9]{1,9}) (?:running)', 1)) ; attempt to get pid If IsArray($pid_solr) Then Return SetError(0, $pid_solr[0], $sOutput) Return SetError(0, 1, $sOutput) EndSwitch EndFunc ;==>_solr_cmdstop ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_Webupdate ; Description ...: Updates documents using the Solr Update API ; Syntax.........: _solr_Webupdate($st_URL, $query) ; Parameters ....: $st_URL - Solr URL ; $query - An update modifier query ; Return values .: Success - Returns 0 and @extended is set to QTime ; Failure - Returns -1 and sets @error and @extended to -1 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: This function will take any Solr Json update modifier, if an object is detected, it will Json Pretty print ; Related .......: _solr_GetJson ; Link ..........: https://yonik.com/solr/atomic-updates/ ; Example .......: ; =============================================================================================================================== Func _solr_Webupdate($st_URL, $query) Local $obj_Json = _solr_GetJson($st_URL & '/update', $query) If _solr_rstat($obj_Json) Then Return SetError(0, json_Get($obj_Json, '["responseHeader.QTime"]'), 0) Return SetError(-1, -1, -1) EndFunc ;==>_solr_Webupdate ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_Webcommit ; Description ...: Finalizes all add/deletes made to the solr index ; Syntax.........: _solr_Webcommit($st_URL[,$i_CommitLvl = 0]) ; Parameters ....: $st_URL - Solr URL ; $i_CommitLvl - Sets the commit level (0 for hard commit, 1 for soft commit) ; Return values .: Success - Returns 0, sets @error to 0 and @extended to QTime ; Failure - Returns -1, sets @error and @extended to -1 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: ; Related .......: ; Link ..........: https://solr.apache.org/guide/6_6/updatehandlers-in-solrconfig.html ; Example .......: ; =============================================================================================================================== Func _solr_Webcommit($st_URL, $i_CommitLvl = 0) Local $cmd_url = $st_URL & '/update?' Switch $i_CommitLvl Case 0 $cmd_url &= 'commit=true' Case 1 $cmd_url &= 'softCommit=true' EndSwitch Local $obj_Json = _solr_GetJson($cmd_url) If _solr_rstat($obj_Json) Then Return SetError(0, json_Get($obj_Json, '["responseHeader.QTime"]'), 0) Return SetError(-1, -1, -1) EndFunc ;==>_solr_Webcommit ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_Webgetconfig ; Description ...: Retrieves configurations and configuration parameters from Solr config API endpoints ; Syntax.........: _solr_Webgetconfig($st_URL[, $st_cfglvl = 0[, $str_cfg = '']]) ; Parameters ....: $st_URL - Solr URL ; $st_cfglvl - The configuration endpoint to query (0=full effective config, 1=config overlay, 2=override ; parameter sets) ; $str_cfg - Specific configuration to be returned as a string ; Return values .: Success - Returns an object containing the multi-leveled configuration in Json, If $str_cfg is specified, ; will return a single config paramater as a string. @extended is set to QTime ; Failure - Returns -1 and sets @error and @extended to -1 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: ; Related .......: _solr_Websetconfig ; Link ..........: https://solr.apache.org/guide/7_4/config-api.html ; http://localhost:8983/solr/techproducts/config/overlay ; Example .......: ; =============================================================================================================================== Func _solr_Webgetconfig($st_URL, $st_cfglvl = 0, $str_cfg = '') Local $cmd_url = $st_URL & '/config' Switch $st_cfglvl Case 1 $cmd_url &= '/overlay' Case 2 $cmd_url &= '/params' EndSwitch Local $obj_Json = _solr_GetJson($cmd_url) Select Case _solr_rstat($obj_Json) And $str_cfg Return SetError(0, json_Get($obj_Json, '["responseHeader.QTime"]'), json_Get($obj_Json, '["' & $str_cfg & '"]')) Case _solr_rstat($obj_Json) Return SetError(0, json_Get($obj_Json, '["responseHeader.QTime"]'), $obj_Json) Case Else Return SetError(-1, -1, -1) EndSelect EndFunc ;==>_solr_Webgetconfig ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_Websetconfig ; Description ...: Modifies a running Solr instance via the config endpoint/API ; Syntax.........: _solr_Websetconfig($st_URL, $command) ; Parameters ....: $st_URL - Solr URL ; $command - Json encoded command configuration from the Solr Config API ; Return values .: Success - Returns 0, sets @error to 0 and @extended to QTime ; Failure - Returns -1 and sets @error and @extended to -1 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: Permanent configuration changes should be made to the schema in XML ; Related .......: _solr_Webgetconfig, _solr_PostJson ; Link ..........: https://solr.apache.org/guide/7_4/config-api.html ; http://localhost:8983/solr/techproducts/config/overlay ; Example .......: ; =============================================================================================================================== Func _solr_Websetconfig($st_URL, $command) Local $obj_Json = _solr_PostJson($st_URL & '/config', $command) If _solr_rstat($obj_Json) Then Return SetError(0, json_Get($obj_Json, '["responseHeader.QTime"]'), 0) Return SetError(-1, -1, -1) EndFunc ;==>_solr_Websetconfig ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_Webselect ; Description ...: Sends queries to the /select request handler ; Syntax.........: _solr_Webselect($st_URL, $query) ; Parameters ....: $st_URL - Solr URL ; $query - Query to execute ; Return values .: Success - A Json object with a solr response, sets @error to 0 and the number of hits to @extended ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: ; Related .......: _solr_WebSelectEx, _solr_GetJson ; Link ..........: https://solr.apache.org/guide/8_11/solr-tutorial.html ; Example .......: ; =============================================================================================================================== Func _solr_Webselect($st_URL, $query) Local $obj_Json = _solr_GetJson($st_URL & '/select?' & $query & '&wt=json') If _solr_rstat($obj_Json) Then Local $numfound = json_Get($obj_Json, '.response.numFound') Return SetError(0, $numfound, $obj_Json) Else Return SetError(-1, -1, -1) EndIf EndFunc ;==>_solr_Webselect ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_arrayfl ; Description ...: Parses a previously queried Solr Json Select response into a field array based on it's field list parameter ; Syntax.........: _solr_arrayfl($obj_Json) ; Parameters ....: $obj_Json - A Solr Json Select response object ; Return values .: Success - 2 dimensional array wherein each column is a field and each row is a hit ; Bad Json - ; No Hits - ; Author ........: mushy ; Modified.......: 11/20/2021 ; Remarks .......: The [0][0]th value of the array contains its size (UBound) ; Related .......: _solr_Webselect, _solr_Webgetv, _solr_Webgetmv ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_arrayfl($obj_Json) Local $arr_results[1][1] $arr_results[0][0] = 0 Select Case _solr_rstat($obj_Json) = True Local $str_fields = Json_ObjGet($obj_Json, "responseHeader.params.fl") Local $arr_fields = StringSplit($str_fields, ',') Local $int_rowsFound = json_Get($obj_Json, '.response.numFound') If (Not IsArray($arr_fields)) Then Return SetError(-1, $int_rowsFound, $arr_results) Switch $int_rowsFound Case 0, '' Return SetError(-1, -1, $arr_results) Case Else Local $docs = Json_ObjGet($obj_Json, ".response.docs") ReDim $arr_results[UBound($docs) + 1][$arr_fields[0]] For $i = 1 To UBound($docs) For $u = 1 To $arr_fields[0] $arr_results[$i][$u - 1] = Json_Get($obj_Json, '.response.docs[' & $i - 1 & '].' & $arr_fields[$u]) Next Next $arr_results[0][0] = UBound($arr_results) - 1 Return SetError(0, $int_rowsFound, $arr_results) EndSwitch Case Else Return SetError(-1, @error & @extended, $arr_results) EndSelect EndFunc ;==>_solr_arrayfl ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_Webping ; Description ...: Issues a ping request to check whether the core is up and responding to requests. ; Syntax.........: _solr_Webping($st_URL) ; Parameters ....: $st_URL - Solr URL ; Return values .: Success - Returns QTime, sets @error and @extended to 0 ; Failure - Returns False, sets @error and @extended ; Author ........: mushy ; Modified.......: 12/30/2021 ; Remarks .......: Does not require _solr_rstat as it validates itself ; Related .......: _solr_tcpcheck ; Link ..........: https://solr.apache.org/guide/6_6/ping.html ; Example .......: ; =============================================================================================================================== Func _solr_Webping($st_URL) If Not IsObj($obj_HTTP) Then $obj_HTTP = ObjCreate('WinHttp.WinHttpRequest.5.1') $obj_HTTP.Open("GET", $st_URL & '/admin/ping', False) If (@error) Then Return SetError(5, @error, -1) $obj_HTTP.SetRequestHeader('Content-Type', 'application/json') $obj_HTTP.SetTimeouts(50, 50, 50, 50) If (@error) Then Return SetError(4, @error, -1) $obj_HTTP.Send() If (@error) Then Return SetError(3, @error, -1) Local $int_Status = $obj_HTTP.Status If ($int_Status <> $SOLR_HTTP_STATUS_OK) Then Return SetError(2, $int_Status, -1) Else Local $bin_Recv = $obj_HTTP.ResponseText Local $str_Recv = BinaryToString($bin_Recv) Local $obj_Json = Json_Decode($str_Recv) If (@error) Then Return SetError(1, @error, -1) Local $json_status = json_Get($obj_Json, '.status') Local $json_QTime = json_Get($obj_Json, '.responseHeader.QTime') Switch $json_status Case 'OK' Return SetError(0, 0, $json_QTime) Case Else Return SetError(-1, -1, -1) EndSwitch EndIf EndFunc ;==>_solr_Webping ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_tcpcheck ; Description ...: Connects to the Solr core to see if services are available and/or starting ; Syntax.........: _solr_tcpcheck($_sHost[, $_sSolrPort, $_iRetry = 4]) ; Parameters ....: $_sHost - Solr Host, can be IP or hostname ; $_SolrPort - Port of the currently running solr instance ; $_iRetry - Number of 500ms retries to connect before timeout ; Return values .: Success - Returns True and sets @error to 0 and @extended to connection time in ms ; Failure - Returns False and sets @error to 1 and @extended to 0 ; No Network - Returns -1 and sets @error and @extended to -1 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: Solr is meant to recieve HTTP instruction sets but this has worked well ; Related .......: _solr_WebPing ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_tcpcheck($_sHost, $_sSolrPort = 8983, $_iRetry = 4) Local $boo_Res = False, $tim_tcp = TimerInit(), _ $int_Timout = Opt("TCPTimeout") Opt("TCPTimeout", 500) If TCPStartup() = 0 Then Return SetError(-1, -1, -1) If __StringIPTypeValid($_sHost) = False Then $_sHost = TCPNameToIP($_sHost) While 1 If TimerDiff($tim_tcp) > (500 * $_iRetry) Then ExitLoop Local $skt_Solr = TCPConnect($_sHost, $_sSolrPort) Switch $skt_Solr Case -1, 0 ContinueLoop Case Else $boo_Res = True ExitLoop EndSwitch WEnd TCPCloseSocket($skt_Solr) TCPShutdown() Opt("TCPTimeout", $int_Timout) Switch $boo_Res Case True Return SetError(0, TimerDiff($tim_tcp), True) Case False Return SetError(1, 0, False) EndSwitch Return $boo_Res EndFunc ;==>_solr_tcpcheck ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_Webgetv ; Description ...: Returns an array of predefined values related to a single document ; Syntax.........: _solr_Webgetv($st_URL, $str_Id[, $str_fl = 'id']) ; Parameters ....: $st_url - Solr URL ; $str_Id - Id of document to request ; $str_fl - A list of comma seperated fields ; Return values .: Returns a 2 dimensional array of keys and values, sets @error and @extended to 0 ; Failure - Returns -1 and sets @error and @extended to -1 ; No values - Returns 1 and sets @error and @extended to 1 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: The [0][0]th value of the array contains its size (UBound), does not return multivalue fields ; Related .......: _solr_Webgetmv ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_Webgetv($st_URL, $str_Id, $str_fl = 'id') Local $obj_Json = _solr_GetJson($st_URL & '/get?id=' & $str_Id & '&fl=' & $str_fl) Local $obj_JsonDoc = Json_ObjGet($obj_Json, 'doc') If _solr_rstat($obj_Json) Then Local $arr_Keys = Json_ObjGetKeys($obj_JsonDoc) Switch IsArray($arr_Keys) Case 1 Local $int_Itemct = UBound($arr_Keys) Local $arr_Items = Json_ObjGetItems($obj_JsonDoc) Local $arr_out[$int_Itemct + 1][2] For $iU = 1 To $int_Itemct $arr_out[$iU][0] = $arr_Keys[$iU - 1] $arr_out[$iU][1] = $arr_Items[$iU - 1] Next $arr_out[0][0] = $int_Itemct Return SetError(0, 0, $arr_out) Case 0 Return SetError(1, 1, 1) EndSwitch Else Return SetError(-1, -1, -1) EndIf EndFunc ;==>_solr_Webgetv ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_Webgetmv ; Description ...: Returns an array of values from a multivalue field of a single document ; Syntax.........: _solr_Webgetmv($st_URL, $str_Id, $str_field) ; Parameters ....: $st_url - Solr URL ; $str_Id - Id of document to request ; $str_field - Field to enumerate into array ; Return values .: Success - Returns a 1 dimensional array of values and sets @error and @extended to 0 ; Failure - Returns -1 and sets @error and @extended to -1 ; No values - Returns 1 and sets @error and @extended to 1 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: The [0]th value of the array contains its size (UBound) ; Related .......: _solr_Webgetv ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_Webgetmv($st_URL, $str_Id, $str_field) Local $str_cmd = $st_URL & '/get?id=' & $str_Id & '&fl=' & $str_field ;__URLEncode($urlText) Local $obj_Json = _solr_GetJson($str_cmd) If _solr_rstat($obj_Json) Then Local $a_multivalue = Json_ObjGet($obj_Json, '.doc.' & $str_field) Switch IsArray($a_multivalue) Case 1 $a_multivalue = __InsertCountInFirstArray($a_multivalue, True) Return SetError(0, 0, $a_multivalue) Case 0 SetError(1, 1, 1) EndSwitch Else Return SetError(-1, -1, -1) EndIf EndFunc ;==>_solr_Webgetmv ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_Webdel ; Description ...: Deletes a document by its id ; Syntax.........: _solr_Webdel($st_url, $id[, $sCommit = True]) ; Parameters ....: $st_url - Solr URL ; $id - Id of document to delete ; $sCommit - Optionally commits changes ; Return values .: Success - Returns 0 and @extended is set to QTime ; Failure - Returns -1 and sets @error and @extended to -1 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: ; Related .......: _solr_Webdel ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_Webdel($st_URL, $str_Id, $boo_Commit = True) Local $url_cmd = $st_URL & '/update?' If $boo_Commit Then $url_cmd &= 'commit=true' Local $str_query = '{ "delete": {"query":"id:' & $str_Id & '"} }' Local $obj_Json = _solr_GetJson($url_cmd, $str_query) If _solr_rstat($obj_Json) Then Return SetError(0, json_Get($obj_Json, '["responseHeader.QTime"]'), 0) Return SetError(-1, -1, -1) EndFunc ;==>_solr_Webdel ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_Webdelall ; Description ...: Deletes all of the documents within a given Solr URL ; Syntax.........: _solr_Webdelall($st_url[, $sCommit = True]) ; Parameters ....: $st_url - Solr URL ; $sCommit - Optionally commits changes ; Return values .: Success - Returns 0 and @extended is set to QTime ; Failure - Returns -1 and sets @error and @extended to -1 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: ; Related .......: _solr_Webdel ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_Webdelall($st_URL, $boo_Commit = True) Local $url_cmd = $st_URL & '/update?' If $boo_Commit Then $url_cmd &= 'commit=true' Local $obj_Json = _solr_GetJson($url_cmd, '{ "delete": {"query":"*:*"} }') If _solr_rstat($obj_Json) Then Return SetError(0, json_Get($obj_Json, '["responseHeader.QTime"]'), 0) Return SetError(-1, -1, -1) EndFunc ;==>_solr_Webdelall ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_tnow ; Description ...: Returns the current date and time in Java/Solr format ; Syntax.........: _solr_tnow() ; Parameters ....: none ; Return values .: Success - Current time in Java/Solr Format ; Author ........: mushy ; Modified.......: 11/18/2021 ; Remarks .......: This function is useful for storing processing times into solr ; Related .......: _solr_tfile, _NowTime ; Link ..........: https://solr.apache.org/guide/6_6/working-with-dates.html ; Example .......: ; =============================================================================================================================== Func _solr_tnow() ; should convert a time array or return existing time if no params Return @YEAR & '-' & @MON & '-' & @MDAY & 'T' & @HOUR & ':' & @MIN & ':' & @SEC & '.' & @MSEC & 'Z' EndFunc ;==>_solr_tnow ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_tfile ; Description ...: Returns the Java/Solr time of a file ; Syntax.........: _solr_tfile($sFile[, $iOption = 0]) ; Parameters ....: $sFile - File to request time from ; $iOption - Passthrough to FileGetTime() ; Return values .: Success - Time of file in Java/Solr UTC, @error set to 0 ; Failure - 0, @error is set to 1 ; Author ........: mushy ; Modified.......: 11/18/2021 ; Remarks .......: ; Related .......: _solr_tnow, FileGetTime ; Link ..........: https://solr.apache.org/guide/6_6/working-with-dates.html ; Example .......: ; =============================================================================================================================== Func _solr_tfile($sFile, $iOption = 0) Local $arr_fileTime = FileGetTime($sFile, $iOption, 0) ;$FT_ARRAY If IsArray($arr_fileTime) Then Return $arr_fileTime[0] & '-' & $arr_fileTime[1] & '-' & $arr_fileTime[2] & 'T' & $arr_fileTime[3] & ':' & $arr_fileTime[4] & ':' & $arr_fileTime[5] & 'Z' Else SetError(1) Return 0 EndIf EndFunc ;==>_solr_tfile ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_PostJson ; Description ...: Posts Json querys and commands to Solr ; Syntax.........: _solr_PostJson($url[, $query = '']) ; Parameters ....: $url - URL to send Json POST request to ; $query - Optional query to send along with URL and json request header ; Return values .: Success - Json Object and @error is set to 0 ; Failure - nothing and sets @error to non 0 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: ; Related .......: _solr_GetJson, _solr_PostMultiPart ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_PostJson($url, $query = '') ;If Not IsObj($obj_HTTP) Then $obj_HTTP = ObjCreate('WinHttp.WinHttpRequest.5.1') $obj_HTTP.Open("POST", $url, False) $obj_HTTP.SetRequestHeader('Content-Type', 'application/json') If (@error) Then Return SetError(1, 0, 0) $obj_HTTP.Send($query) If (@error) Then Return SetError(2, 0, 0) Local $int_Status = $obj_HTTP.Status If ($int_Status <> $SOLR_HTTP_STATUS_OK) Then Return SetError(3, $int_Status, 0) Else Local $bin_Recv = $obj_HTTP.ResponseText Local $str_Recv = BinaryToString($bin_Recv) ;json_dump($str_Recv) Local $obj_Json = Json_Decode($str_Recv) If (@error) Then Return SetError(1, @error) Return SetError(0, 0, $obj_Json) EndIf EndFunc ;==>_solr_PostJson ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_PostMultiPart ; Description ...: Posts data and parameters to Solr ; Syntax.........: _solr_PostMultiPart($url, $sMBoundary, $sData) ; Parameters ....: $url - URL to POST data to ; $sMBoundary - Boundary used to delimit data and data pairs ; $sData - Post data prepackaged by _solr_pkgdoc ; Return values .: Success - Json Object and @error is set to 0 ; Failure - nothing and sets @error to non 0 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: ; Related .......: _solr_PostJson, _solr_PostMultiPart ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_PostMultiPart($url, $smBoundary, $sdata) If Not IsObj($obj_HTTP) Then $obj_HTTP = ObjCreate('WinHttp.WinHttpRequest.5.1') $obj_HTTP.Open("POST", $url, False) If (@error) Then Return SetError(5, @error) $obj_HTTP.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" & $smBoundary) If @error Then Return SetError(4, 0) $obj_HTTP.Send($sdata) If @error Then Return SetError(3, 0) Local $int_Status = $obj_HTTP.Status If ($int_Status <> $SOLR_HTTP_STATUS_OK) Then Return SetError(2, $int_Status) Else Local $bin_Recv = $obj_HTTP.ResponseText ;ConsoleWrite($obj_HTTP.ResponseText & @CRLF) Local $str_Recv = BinaryToString($bin_Recv) ;json_dump($str_Recv) Local $obj_Json = Json_Decode($str_Recv) If (@error) Then Return SetError(1, @error) Return SetError(0, 0, $obj_Json) EndIf EndFunc ;==>_solr_PostMultiPart ; #FUNCTION# ==================================================================================================================== ; Name...........: _solr_GetJson ; Description ...: Querys Solr and provides decodes json objects as responses ; Syntax.........: _solr_GetJson($url[, $query = '']) ; Parameters ....: $url - URL to send Json GET request to ; $query - Optional query to send along with URL and json request header ; Return values .: Success - Json Object and @error is set to 0 ; Failure - nothing and sets @error to non 0 ; Author ........: mushy ; Modified.......: 11/19/2021 ; Remarks .......: ; Related .......: _solr_PostJson, _solr_PostMultiPart ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _solr_GetJson($url, $str_query = '') If Not IsObj($obj_HTTP) Then $obj_HTTP = ObjCreate('WinHttp.WinHttpRequest.5.1') $obj_HTTP.Open('GET', $url, False) If (@error) Then Return SetError(5, @error) $obj_HTTP.SetRequestHeader('Content-Type', 'application/json') If (@error) Then Return SetError(4, @error) $obj_HTTP.Send($str_query) If (@error) Then Return SetError(3, @error) Local $int_Status = $obj_HTTP.Status If ($int_Status <> $SOLR_HTTP_STATUS_OK) Then Return SetError(2, $int_Status) Else Local $bin_Recv = $obj_HTTP.ResponseText Local $str_Recv = BinaryToString($bin_Recv) ;json_dump($str_Recv) Local $obj_Json = Json_Decode($str_Recv) If (@error) Then Return SetError(1, @error) Return SetError(0, 0, $obj_Json) EndIf EndFunc ;==>_solr_GetJson ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __URLEncode ; Description ...: Encodes strings for HTTP transfer. Most importantly, removes spaces. ; Syntax.........: __URLEncode($urlText) ; Parameters ....: $urlText - String to URL encode ; Return values .: Success - URL encoded string ; Author ........: unknown - I dont know where this came from, but thank you! ; Modified.......: unknown ; Remarks .......: This is the simplest version of this encoder/decoder pair that has worked with every file and path ; Related .......: __URLDecode ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __URLEncode($urlText) Local $url = "", $acode For $i = 1 To StringLen($urlText) $acode = Asc(StringMid($urlText, $i, 1)) Select Case ($acode >= 48 And $acode <= 57) Or _ ($acode >= 65 And $acode <= 90) Or _ ($acode >= 97 And $acode <= 122) $url &= StringMid($urlText, $i, 1) Case $acode = 32 $url &= "+" Case Else $url &= "%" & Hex($acode, 2) EndSelect Next Return $url EndFunc ;==>__URLEncode ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __URLDecode ; Description ...: Decodes encodes strings back for human-readable use ; Syntax.........: __URLDecode($urlText) ; Parameters ....: $urlText - String to Decode ; Return values .: Success - URL decoded string ; Author ........: unknown - I dont know where this came from, but thank you! ; Modified.......: unknown ; Remarks .......: This is the simplest version of this encoder/decoder pair that has worked with every file and path ; Related .......: __URLEncode ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __URLDecode($urlText) $urlText = StringReplace($urlText, "+", " ") Local $matches = StringRegExp($urlText, "\%([abcdefABCDEF0-9]{2})", 3) If Not @error Then For $match In $matches $urlText = StringReplace($urlText, "%" & $match, BinaryToString('0x' & $match)) Next EndIf Return $urlText EndFunc ;==>__URLDecode ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __StringIPTypeValid ; Description ...: Validates a string as an IP up to 255 integer in each octet ; Syntax.........: __StringIPTypeValid($sIP) ; Parameters ....: $sIP - IP address to validate ; Return values .: Success - True ; Failure - False ; Author ........: @SmOke_N - https://www.autoitscript.com/forum/profile/4813-smoke_n/ ; Modified.......: 01/23/2007 ; Remarks .......: This is a bare minimum used to validate and boolean a string into 'IP or hostname?' ; Related .......: ; Link ..........: https://www.autoitscript.com/forum/topic/39932-_stringipisvalid-_stringipptr/ ; Example .......: ; =============================================================================================================================== Func __StringIPTypeValid($sIP) If StringRegExp($sIP, "(?:(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.)){3}(?:(25[0-5]$|2[0-4]\d$|1\d{2}$|[1-9]\d$|\d$))") Then Return True Return False EndFunc ;==>__StringIPTypeValid ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __InsertCountInFirstArray ; Description ...: Adds the size of the array to the 0th value and shifts existing values up accordingly ; Syntax.........: __InsertCountInFirstArray($oldArray[, $sForce = False]) ; Parameters ....: $oldArray - Array to modify ; $sForce - Boolean operator to force value out of 0th position ; Return values .: $NewArray - New array with 0th value set to UBound ; Author ........: @vip - https://www.autoitscript.com/forum/profile/103606-vip/ ; Modified.......: 01/08/2016 ; Remarks .......: Used to match many of the other functions in AutoIT, its helpful for beginners ; Related .......: UBound, ReDim ; Link ..........: https://www.autoitscript.com/forum/topic/179776-solved-insert-count-in-the-first-element-on-array/ ; Example .......: ; =============================================================================================================================== Func __InsertCountInFirstArray($oldArray, $sForce = False) If Not IsArray($oldArray) Then Return SetError(1, 0, "") Local $sCount = UBound($oldArray) If ($oldArray[0] = $sCount - 1) And (Not $sForce) Then Return SetError(0, 1, $oldArray) Local $NewArray[$sCount + 1] $NewArray[0] = $sCount For $i = 1 To $sCount $NewArray[$i] = $oldArray[$i - 1] Next Return $NewArray EndFunc ;==>__InsertCountInFirstArray