Skip to content
This repository has been archived by the owner on Feb 7, 2018. It is now read-only.

custom primary key #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ CREATE FOREIGN TABLE articles_es (
id bigint,
title text,
content text
) SERVER multicorn_es OPTIONS (host '127.0.0.1', port '9200', node 'test', index 'articles');
) SERVER multicorn_es OPTIONS (host '127.0.0.1', port '9200', node 'test', index 'articles', primary_key 'id');



Expand Down
16 changes: 7 additions & 9 deletions dite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from multicorn import ForeignDataWrapper
from multicorn.utils import log_to_postgres as log2pg

from functools import partial

import httplib
import json
import logging
Expand All @@ -20,6 +18,7 @@ def __init__(self, options, columns):
self.port = int(options.get('port', '9200'))
self.node = options.get('node', '')
self.index = options.get('index', '')
self._row_id_column = options.get('primary_key', columns.keys()[0])

self.columns = columns

Expand Down Expand Up @@ -52,7 +51,7 @@ def execute(self, quals, columns):
for hit in data['hits']['hits']:
row = {}
for col in columns:
if col == 'id':
if col == self.rowid_column:
row[col] = hit['_id']
elif col in hit['_source']:
row[col] = hit['_source'][col]
Expand All @@ -66,7 +65,7 @@ def rowid_column(self):
This column name should be subsequently present in every
returned resultset.
"""
return 'id';
return self._row_id_column

def es_index(self, id, values):
content = json.dumps(values)
Expand All @@ -85,15 +84,14 @@ def es_index(self, id, values):
def insert(self, new_values):
log2pg('MARK Insert Request - new values: %s' % new_values, logging.DEBUG)

if not 'id' in new_values:
log2pg('INSERT requires "id" column. Missing in: %s' % new_values, logging.ERROR)
if not self.rowid_column in new_values:
log2pg('INSERT requires "%s" column. Missing in: %s' % (self.rowid_colum, new_values), logging.ERROR)

id = new_values['id']
new_values.pop('id', None)
id = new_values.pop(self.rowid_column)
return self.es_index(id, new_values)

def update(self, id, new_values):
new_values.pop('id', None)
new_values.pop(self.rowid_column, None)
return self.es_index(id, new_values)

def delete(self, id):
Expand Down