2017-03-09 43 views
2

從Marklogic數據庫獲取數據,但代碼僅搜索一個文件。有沒有其他辦法可以達到目標MarkLogic/Python查詢只搜索一個文件

import `http`.`client` 

`conn` = `http`.`client`.`HTTPConnection`("127.0.0.1:8040") 

headers = { 
    'authorization': "Digest `username`=\"root\", realm=\"public\", nonce=\"\", `uri`=\"/v1/documents?`uri`=/scripts/test_insert_2.`json`\", response=\"f5d58bcbccc9119fbf71f851ac4e90f0\", opaque=\"\"", 
    'cache-control': "no-cache", 
    'postman-token': "52c1f629-5bb9-e16c-5693-16d8d6001e2d" 
    } 

`conn`.`request`("GET", "/v1/documents?`uri`=%2Fscripts%2Ftest_insert_2.`json`", headers=headers) 

res = `conn`.`getresponse`() 
data = res.read() 

print(data.decode("utf-8")) 

回答

2
def searchkeyword(self, keyword): 
    try: 
     self.querystring = {"q": keyword} 
     url = self.baseUri + "/search" 
     self.header = {'Accept': "application/json"} 
     resp = requests.get(url, auth=self.auth, params=self.querystring, headers=self.header) 
     result = json.loads(resp.content) 
     res = [] 
     fres = [] 
     for uri in result['results']: 
      x = uri['uri'] 
      if x.__contains__(self.collection): 
       res.append(x) 
     for x in res: 
      data = x.split('/') 
      output = self.FetchData(data[2]) 
      fres.append(output) 
     return fres 
    except requests.HTTPError: 
     raise RuntimeError('HTTPError occurred while fetching data from file on MarkLogic server') 
    except requests.ConnectionError: 
     raise RuntimeError('ConnectionError occurred while fetching data from file on MarkLogic server') 
+0

此功能可以幫助我在MarkLogic DB尋找特定關鍵字Reading and Writing Multiple Documents section。 –

+0

這將幫助您從特定集合中獲取文檔。 –

2

你的代碼不是搜索,它是明確要求單個文件。

要同時檢索多個文檔,你將包括URI在對陣/documents終點的要求:

網址:

http://host:port/version/documents?uri=uri-1&uri=uri-2&... 

捲曲:

$ curl --anyauth --user user:password -X GET -i \ 
    -H "Accept: multipart/mixed; boundary=BOUNDARY" \ 
    'http://host:port/LATEST/documents?uri=uri-1&uri=uri-2' 

如果您想返回多個文檔,則需要使用REST終點來允許跨文檔搜索。更多信息在Query Features section發現的REST Application Developer's Guide