使用更新 CBOR 数据格式进行索引
Solr 支持 CBOR 格式用于索引和查询。它支持大多数流行的语言和平台。与 JSON 相比,它更快、更高效。
Python 示例
将以下内容保存为 cbor_post.py
import json
import requests
import cbor2
# JSON data to send (list of dictionaries)
json_data = [
{
"id" : "1",
"name_s": "John Doe",
"age_i": 30,
"city_s": "New York"
},
{
"id": "2",
"name_s": "Jane Smith",
"age_i": 25,
"city_s": "London"
}
]
# If there is only a single doc you can use the following
# json_data = {
# "id" : "6",
# "name_s": "John Doe",
# "age_i": 30,
# "city_s": "New York"
# }
# Convert JSON data to CBOR
cbor_data = cbor2.dumps(json_data)
# Set the endpoint URL
# ensure that the collection 'coll1' is already created
url = "http://localhost:8983/solr/coll1/update/cbor?commit=true"
# Send a POST request with CBOR data
response = requests.post(url, data=cbor_data, headers={"Content-Type": "application/cbor"})
# Check the response status
if response.status_code == 200:
print("POST request sent successfully!")
print("Response Body:", response.text)
else:
print("Unexpected response status:", response.status_code)
Node.js 示例
将以下程序保存到名为 script.js
的文件中
const cbor = require('cbor');
const axios = require('axios');
async function main() {
// JSON data to send (list of JSON objects)
const jsonData = [
{
"id" : "1",
"name_s": "John Doe",
"age_i": 30,
"city_s": "New York"
},
{
"id": "2",
"name_s": "Jane Smith",
"age_i": 25,
"city_s": "London"
},
];
// Convert JSON data to CBOR
const cborData = cbor.encode(jsonData);
// Set the endpoint URL
const url = "http://localhost:8983/solr/coll1/update/cbor?commit=true"
try {
// Send a POST request with CBOR data
const response = await axios.post(url, cborData, {
headers: {
'Content-Type': 'application/cbor',
},
});
// Process the response
console.log('POST request sent successfully!');
console.log('Response:', response.data);
} catch (error) {
console.error('Error sending POST request:', error.message);
}
}
main();