Skip to content

Commit

Permalink
Merge pull request #19 from djangsters/feature/realtime_get
Browse files Browse the repository at this point in the history
Add support for RealTime Get
  • Loading branch information
delijati committed Sep 28, 2015
2 parents 2a59c63 + dfa14cb commit 0a41f1c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
18 changes: 17 additions & 1 deletion docs/query.rst
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ For background, see http://wiki.apache.org/solr/FieldCollapsing.

Solr 3.3 added support for result grouping.

A example call lookes like this:
An example call looks like this:

::

Expand Down Expand Up @@ -823,3 +823,19 @@ Example::

>>> si.query().spellcheck().options()
{u'q': u'*:*', u'spellcheck': 'true'}

Realtime Get
------------

For background, see https://wiki.apache.org/solr/RealTimeGet

Solr 4.0 added support for retrieval of documents that are not yet commited.
The retrieval can only by done by id: ::

>>> resp = si.get("978-1423103349")

You can also pass multiple ids: ::

>>> resp = si.get(["978-0641723445", "978-1423103349"])

The return value is the same as for a normal search
41 changes: 41 additions & 0 deletions scorched/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self, url, http_connection, mode, retry_timeout,
self.update_url = self.url + "update/json"
self.select_url = self.url + "select/"
self.mlt_url = self.url + "mlt/"
self.get_url = self.url + "get/"
self.retry_timeout = retry_timeout
self.max_length_get_url = max_length_get_url
self.search_timeout = search_timeout
Expand All @@ -75,6 +76,33 @@ def request(self, *args, **kwargs):
time.sleep(self.retry_timeout)
return self.http_connection.request(*args, **kwargs)

def get(self, ids, fl=None):
"""
Perform a RealTime Get
"""
# We always send the ids parameter to force the standart output format,
# but use the id parameter for our actual data as `ids` can no handle
# ids with commas
params = [
("ids", ""),
("wt", "json"),
]
if is_iter(ids):
for id in ids:
params.append(("id", id))
else:
params.append(("id", ids))
if fl:
params.append(("fl", ",".join(fl)))

qs = scorched.compat.urlencode(params)
url = "%s?%s" % (self.get_url, qs)

response = self.request("GET", url)
if response.status_code != 200:
raise scorched.exc.SolrError(response)
return response.text

def update(self, update_doc, **kwargs):
"""
:param update_doc: data send to solr
Expand Down Expand Up @@ -383,6 +411,19 @@ def delete_all(self):
"""
self.delete_by_query(self.Q(**{"*": "*"}))

def get(self, ids, fields=None):
"""
RealTime Get document(s) by id(s)
:param ids: id(s) of the document(s)
:type ids: list, string or int
:param fields: optional -- list of fields to return
:type fileds: list of strings
"""
ret = scorched.response.SolrResponse.from_get_json(
self.conn.get(ids, fields), self._datefields)
return ret

def search(self, **kwargs):
"""
:returns: SolrResponse -- A solr response object.
Expand Down
9 changes: 9 additions & 0 deletions scorched/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ def from_json(cls, jsonmsg, datefields=()):
self.interesting_terms = doc.get('interestingTerms', None)
return self

@classmethod
def from_get_json(cls, jsonmsg, datefields=()):
"""Generate instance from the response of a RealTime Get"""
self = cls()
self.original_json = jsonmsg
doc = json.loads(jsonmsg)
self.result = SolrResult.from_json(doc['response'], datefields)
return self

@classmethod
def parse_term_vectors(cls, lst, path=""):
"""Transform a solr list to dict
Expand Down
24 changes: 24 additions & 0 deletions scorched/tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ def tearDown(self):
"http://localhost:8983/solr")
si = SolrInterface(dsn)
si.delete_all()
si.commit()

@scorched.testing.skip_unless_solr
def test_get(self):
dsn = os.environ.get("SOLR_URL",
"http://localhost:8983/solr")
si = SolrInterface(dsn)
res = si.get("978-1423103349")
self.assertEqual(len(res), 0)

si.add(self.docs)
res = si.get("978-1423103349")
self.assertEqual(len(res), 1)
self.assertEqual(res[0]["name"], "The Sea of Monsters")

res = si.get(["978-0641723445", "978-1423103349", "nonexist"])
self.assertEqual(len(res), 2)
self.assertEqual([x["name"] for x in res],
[u"The Lightning Thief", u"The Sea of Monsters"])

si.commit()
res = si.get(ids="978-1423103349", fields=["author"])
self.assertEqual(len(res), 1)
self.assertEqual(list(res[0].keys()), ["author"])

@scorched.testing.skip_unless_solr
def test_query(self):
Expand Down

0 comments on commit 0a41f1c

Please sign in to comment.