upsert

Not clear how to upsert ElasticSearch using python elasticsearch

十年热恋 提交于 2019-12-12 13:29:56
问题 Look here for a similar example: https://stackoverflow.com/a/33247409/1575066 from elasticsearch import Elasticsearch es = Elasticsearch("localhost:9200") es.update(index='test',doc_type='test1',id='1',body={'doc' {'username':'Tom'},'doc_as_upsert':True}) But now imagine the goal is to append to an array or to increment a previous value (without having to get the document first). If you go with official requests, this is in the documentation: POST /website/pageviews/1/_update { "script" :

Syntax error in UPSERT test code

两盒软妹~` 提交于 2019-12-12 10:23:11
问题 I am trying to test the new PostgreSQL upsert syntax with the following test code, but get the syntax error: test=> CREATE TABLE test1 ( test(> key1 integer PRIMARY KEY check (key1 > 0), test(> key2 integer check (key2 > 0) test(> ); CREATE TABLE test=> CREATE OR REPLACE FUNCTION upsert(IN in_json_array jsonb) test-> RETURNS void AS test-> $func$ test$> UPDATE test1 t SET test$> t.key1 = (obj->>'key1')::int, test$> t.key2 = (obj->>'key2')::int test$> FROM JSONB_ARRAY_ELEMENTS(in_json_array)

How to translate PostgreSQL “merge_db” (aka upsert) function into MySQL

大城市里の小女人 提交于 2019-12-12 09:26:45
问题 Straight from the manual, here's the canonical example of merge_db in PostgreSQL: CREATE TABLE db (a INT PRIMARY KEY, b TEXT); CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS $$ BEGIN LOOP -- first try to update the key UPDATE db SET b = data WHERE a = key; IF found THEN RETURN; END IF; -- not there, so try to insert the key -- if someone else inserts the same key concurrently, -- we could get a unique-key failure BEGIN INSERT INTO db(a,b) VALUES (key, data); RETURN; EXCEPTION

how to use python Elasticsearch client upsert api

﹥>﹥吖頭↗ 提交于 2019-12-12 08:21:03
问题 I'm using Elasticsearch python client as http://elasticsearch-py.readthedocs.org/ I tried hard but still could not find the update api with upsert. Could anyone give me an example with ES python client upsert api please. 回答1: The example code as following: from elasticsearch import Elasticsearch es = Elasticsearch("localhost:9200") es.update(index='test',doc_type='test1',id='1',body={'doc':{'username':'Tom'},'doc_as_upsert':True}) if without doc_as_upsert=true it would throw exception when

Do nothing in a trigger procedure

倾然丶 夕夏残阳落幕 提交于 2019-12-12 05:14:08
问题 I got a trouble when a try to execute a trigger. Let's suppose we have 2 tables and I want to copy data from table A to table B but each table got a unique constraint . create table test1 ( test_name varchar); create unique index test1_uc on test1 USING btree (test_name); create table test2 ( test_name2 varchar); create unique index test2_uc on test2 USING btree (test_name2); CREATE OR REPLACE FUNCTION trig_test() RETURNS trigger AS $$ BEGIN IF pg_trigger_depth() <> 1 THEN RETURN NEW; END IF;

Access database UPSERT with pyodbc

拜拜、爱过 提交于 2019-12-12 04:48:02
问题 I am using pyodbc to update an Access database. I need the functionality of an UPSERT. ON DUPLICATE KEY UPDATE doesn't exist in Access SQL, and REPLACE is not an option since I want to keep other fields. There are a lot of suggestions out there how to solve that, so this is the solution which I put together: for table_name in data_source: table = data_source[table_name] for y in table: if table_name == "whatever": SQL_UPDATE = "UPDATE {} set [Part Name] = '{}', [value] = {}, [code] = {},

Using TRY / CATCH to perform INSERT / UPDATE

馋奶兔 提交于 2019-12-11 13:58:02
问题 I have this pattern in a number of stored procedures -- Table1 [id] [int] IDENTITY(1,1) NOT NULL [data] [varchar](512) NULL [count] INT NULL -- 'data' is unique, with a unique index on 'data' in 'Table1' BEGIN TRY INSERT INTO Table1 (data, count) SELECT @data,1; END TRY BEGIN CATCH UPDATE Table1 SET count = count + 1 WHERE data = @data; END CATCH I've been slammed before for using this pattern You should never have exception "catching" in your normal logic flow. (Thus why it is called an

MySQL UPSERT without ON DUPLICATE KEY

馋奶兔 提交于 2019-12-11 13:25:32
问题 I want a UPSERT ( UPDATE if exists, else insert) on a MySQL table with these fields: CREATE TABLE item ( uid int(11) NOT NULL AUTO_INCREMENT, timestamp int(11) NOT NULL DEFAULT '0', category1 int(11) NOT NULL DEFAULT '0', category2 int(11) NOT NULL DEFAULT '0', counter int(11) NOT NULL DEFAULT '1', PRIMARY KEY (`uid`) ) This table is already in productive use, and the combination of (timestamp,category1,category2) is for my INSERT unique, but not for the whole table content. That's the reason

MongoDB: cannot upsert if query object and update object contain a same property , 'Cannot apply $addToSet modifier to non-array'

一笑奈何 提交于 2019-12-11 10:39:26
问题 I want to execute a complex upsert, made by the following elements: query on a field f by regex update of the same field f (that is specified in the query object too) Like this: db.coll.update({ prop: /valueprop/i } , { $addToSet:{ prop:'a value' } } , true, false) My expectation would be that if the document does not exist in the collection it is inserted with prop field equals to 'a value'; in other words it should insert a document with value of prop field that is the one specified in the

how to check if an document is updated or inserted in MongoDB

我们两清 提交于 2019-12-11 05:21:50
问题 I'm just wondering if there's way to check if a given document is updated or inserted in MongoDB after a upsert operation in Java: DBCollection col = getCollection(); // either this col.findAndModify(query, null, null, false, contact, false, true); // or this WriteResult wr = col.update(query, contact, true, false); 回答1: Generally, WriteResult will hold information regarding whether it was successful or not; if successful, the document will be there. You could also do a simple col.count(new