Update documents using various data types
This example shows how to update values for different datatype fields. Some fields can accept value as Raw JSON (e.g. nested, object, geo_point, geo_shape). Object field type can also accept value by nested field (e.g. [object_field.field1] )
Standard SQL query example
This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.
UPDATE my_index
SET
binary_field='SGVsbG8gd29ybGQ=', --base64 value of "Hello world"
boolean_field=false,
byte_field=117,
date_field='2012-12-31T23:59:59.123',
double_field=1.123456789,
float_field=1.123456789,
--raw JSON must be in one line
geo_point_field='{ "lat": 40.7128, "lon": -74.0060 }',
--OR--
--"geo_point_field.lat"=40.7128,
--"geo_point_field.lon"=-74.0060,
--raw JSON must be in one line
geo_shape_field='{ "type": "polygon", "coordinates": [[[-74.0060, 40.7128], [-73.9960, 40.7128], [-73.9960, 40.7028], [-74.0060, 40.7028], [-74.0060, 40.7128]]] }',
integer_field=123,
ip_field='127.0.0.1',
keyword_field='thhi is text',
long_field=1234567890,
--raw JSON must be in one line
nested_field='[{"nested_property_1":"nested text 1", "nested_property_2":100}, {"nested_property_1":"nested text 2", "nested_property_2":101}]',
--raw JSON must be in one line
object_field='{"field1":"A","field2":"B"}',
--OR--
--[object_field.field1]='object field keyword 1',
--[object_field.field1]=123,
short_field=1,
text_field='text field '
WHERE _id=2 --user defined key
--OR-- use auto-generated key
--WHERE _id='MtsicZQBuOa42vmvEtWJ'
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_ELASTICSEARCH_IN_GATEWAY], 'UPDATE my_index
SET
binary_field=''SGVsbG8gd29ybGQ='', --base64 value of "Hello world"
boolean_field=false,
byte_field=117,
date_field=''2012-12-31T23:59:59.123'',
double_field=1.123456789,
float_field=1.123456789,
--raw JSON must be in one line
geo_point_field=''{ "lat": 40.7128, "lon": -74.0060 }'',
--OR--
--"geo_point_field.lat"=40.7128,
--"geo_point_field.lon"=-74.0060,
--raw JSON must be in one line
geo_shape_field=''{ "type": "polygon", "coordinates": [[[-74.0060, 40.7128], [-73.9960, 40.7128], [-73.9960, 40.7028], [-74.0060, 40.7028], [-74.0060, 40.7128]]] }'',
integer_field=123,
ip_field=''127.0.0.1'',
keyword_field=''thhi is text'',
long_field=1234567890,
--raw JSON must be in one line
nested_field=''[{"nested_property_1":"nested text 1", "nested_property_2":100}, {"nested_property_1":"nested text 2", "nested_property_2":101}]'',
--raw JSON must be in one line
object_field=''{"field1":"A","field2":"B"}'',
--OR--
--[object_field.field1]=''object field keyword 1'',
--[object_field.field1]=123,
short_field=1,
text_field=''text field ''
WHERE _id=2 --user defined key
--OR-- use auto-generated key
--WHERE _id=''MtsicZQBuOa42vmvEtWJ''')
Using EXEC in SQL Server (handling larger SQL text)
The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements.
This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).
Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_ELASTICSEARCH_IN_GATEWAY] syntax.
DECLARE @MyQuery NVARCHAR(MAX) = 'UPDATE my_index
SET
binary_field=''SGVsbG8gd29ybGQ='', --base64 value of "Hello world"
boolean_field=false,
byte_field=117,
date_field=''2012-12-31T23:59:59.123'',
double_field=1.123456789,
float_field=1.123456789,
--raw JSON must be in one line
geo_point_field=''{ "lat": 40.7128, "lon": -74.0060 }'',
--OR--
--"geo_point_field.lat"=40.7128,
--"geo_point_field.lon"=-74.0060,
--raw JSON must be in one line
geo_shape_field=''{ "type": "polygon", "coordinates": [[[-74.0060, 40.7128], [-73.9960, 40.7128], [-73.9960, 40.7028], [-74.0060, 40.7028], [-74.0060, 40.7128]]] }'',
integer_field=123,
ip_field=''127.0.0.1'',
keyword_field=''thhi is text'',
long_field=1234567890,
--raw JSON must be in one line
nested_field=''[{"nested_property_1":"nested text 1", "nested_property_2":100}, {"nested_property_1":"nested text 2", "nested_property_2":101}]'',
--raw JSON must be in one line
object_field=''{"field1":"A","field2":"B"}'',
--OR--
--[object_field.field1]=''object field keyword 1'',
--[object_field.field1]=123,
short_field=1,
text_field=''text field ''
WHERE _id=2 --user defined key
--OR-- use auto-generated key
--WHERE _id=''MtsicZQBuOa42vmvEtWJ'''
EXEC (@MyQuery) AT [LS_TO_ELASTICSEARCH_IN_GATEWAY]