json_insert.rst 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. .. _http_json_insert:
  2. json/insert
  3. -----------
  4. Documents can be inserted into RT indexes using the ``/json/insert`` endpoint. As with SphinxQL's :ref:`insert_and_replace_syntax`, documents with ids that already exist will not be overwritten. You can also use the ``/json/create`` endpoint, it's a synonym for ``json/insert``.
  5. Here's how you can index a simple document:
  6. ::
  7. {
  8. "index":"test",
  9. "id":1
  10. }
  11. This creates a document with an id specified by ``id`` in an index specified by the ``index`` property. This document has empty fulltext fields and all attributes are set to their default values. However, you can use the optional ``doc`` property to set field and attribute values:
  12. ::
  13. {
  14. "index":"test",
  15. "id":1,
  16. "doc":
  17. {
  18. "gid" : 10,
  19. "content" : "new document"
  20. }
  21. }
  22. The daemon will respond with a JSON object stating if the operation was successfull or not:
  23. ::
  24. {
  25. "_index": "test",
  26. "_id": 1,
  27. "created": true,
  28. "result": "created"
  29. }
  30. MVA attributes are inserted as arrays of numbers. JSON attributes can be inserted either as JSON objects or as strings containing escaped JSON:
  31. ::
  32. {
  33. "index":"test",
  34. "id":1,
  35. "doc":
  36. {
  37. "mva" : [1,2,3,4,5],
  38. "json1":
  39. {
  40. "string": "name1",
  41. "int": 1,
  42. "array" : [100,200],
  43. "object": {}
  44. },
  45. "json2": "{\"string\":\"name2\",\"int\":2,\"array\":[300,400],\"object\":{}}",
  46. "content" : "new document"
  47. }
  48. }
  49. To index a document in a cluster's index ``cluster`` property should be set along with ``index`` property:
  50. ::
  51. {
  52. "cluster":"nodes4",
  53. "index":"test",
  54. "id":1,
  55. "doc":
  56. {
  57. "gid" : 10,
  58. "content" : "new document"
  59. }
  60. }