MarkLogic/Python 查询只搜索一个文件
MarkLogic/Python query only searches for one file
正在从 Marklogic 数据库中获取数据,但代码仅搜索一个文件。有没有其他方法可以实现 objective
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"))
您的代码不是在搜索,而是明确要求单个文档。
要一次检索多个文档,您可以在针对 /documents
端点的请求中包含 URI:
URL:
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'
如果你想 return 多个,你需要使用 \search
REST 端点来允许跨文档搜索。在 Query Features section and the Reading and Writing Multiple Documents section of the REST Application Developer's Guide
中可以找到更多信息
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')
正在从 Marklogic 数据库中获取数据,但代码仅搜索一个文件。有没有其他方法可以实现 objective
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"))
您的代码不是在搜索,而是明确要求单个文档。
要一次检索多个文档,您可以在针对 /documents
端点的请求中包含 URI:
URL:
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'
如果你想 return 多个,你需要使用 \search
REST 端点来允许跨文档搜索。在 Query Features section and the Reading and Writing Multiple Documents section of the REST Application Developer's Guide
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')