You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by no...@apache.org on 2023/06/09 02:59:16 UTC
[solr] 01/01: Python example
This is an automated email from the ASF dual-hosted git repository.
noble pushed a commit to branch jira/solr16812-refguide
in repository https://gitbox.apache.org/repos/asf/solr.git
commit 9ef60c33a7507924622d9b0987ca63bd1ed87fda
Author: Noble Paul <no...@gmail.com>
AuthorDate: Fri Jun 9 12:59:00 2023 +1000
Python example
---
.../modules/indexing-guide/indexing-nav.adoc | 1 +
.../indexing-guide/pages/indexing-with-cbor.adoc | 79 ++++++++++++++++++++++
2 files changed, 80 insertions(+)
diff --git a/solr/solr-ref-guide/modules/indexing-guide/indexing-nav.adoc b/solr/solr-ref-guide/modules/indexing-guide/indexing-nav.adoc
index 1f10acc9f83..532f56eb10c 100644
--- a/solr/solr-ref-guide/modules/indexing-guide/indexing-nav.adoc
+++ b/solr/solr-ref-guide/modules/indexing-guide/indexing-nav.adoc
@@ -51,6 +51,7 @@
* Indexing & Data Operations
** xref:indexing-with-update-handlers.adoc[]
*** xref:transforming-and-indexing-custom-json.adoc[]
+** xref:indexing-with-cbor.adoc[]
** xref:indexing-with-tika.adoc[]
** xref:indexing-nested-documents.adoc[]
** xref:post-tool.adoc[]
diff --git a/solr/solr-ref-guide/modules/indexing-guide/pages/indexing-with-cbor.adoc b/solr/solr-ref-guide/modules/indexing-guide/pages/indexing-with-cbor.adoc
new file mode 100644
index 00000000000..ec8a29ac914
--- /dev/null
+++ b/solr/solr-ref-guide/modules/indexing-guide/pages/indexing-with-cbor.adoc
@@ -0,0 +1,79 @@
+= Indexing with Update CBOR data format
+
+Solr supports http://cbor.io/[CBOR] format for indexing as well as querying. It supports most popular languages and platforms. It's much faster and efficient compared to JSON.
+
+== Python example
+
+Save the following as `cbor_post.py`
+[,python]
+----
+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)
+----
+
+=== Running the program
+
+To run the Python program mentioned above, you can follow these steps:
+
+1. Set up Python: Make sure you have Python installed on your system.
+2. Ensure that the dependencies are installed
+
+ pip install requests cbor2
+
+3. Run the program
+
+ python3 cbor_post.py
+
+4. Check the output
+
+ POST request sent successfully!
+ Response Body: {
+ "responseHeader":{
+ "rf":1,
+ "status":0,
+ "QTime":70
+ }
+ }
+