commitWithin for Solr.add is ignored when JSON update API is used (when no `boost` or `fieldUpdates`)
## Expected behaviour
Calling `Solr.add` without `boost` with a `commitWithin` value should still result in `commitWithin` being used for the Solr update.
## Actual behaviour
As of version 3.9.0, the `commitWithin` is sent to Solr if the XML API is used but not if the JSON API is used, which happens if the `boost` arg to `add` is not provided.
## Steps to reproduce the behaviour
Run the following against master, which uses a mock to verify that `commitWithin` is included in the XML API request but not for the JSON API request:
```python
from unittest.mock import patch, MagicMock
import pysolr
solr = pysolr.Solr("http://pysolr.test/")
# `commitWithin` is not included for the JSON API:
with patch.object(solr, "get_session") as mock:
mock().post.return_value = MagicMock(status_code=200)
solr.add([{"id": "doc1", "foo": "bar"}], commitWithin="1000")
mock().post.assert_called_once_with(
'http://pysolr.test/update/',
data=b'[{"id": "doc1", "foo": "bar"}]',
headers={'Content-type': 'application/json; charset=utf-8'},
files=None,
timeout=60,
auth=None,
)
# `commitWithin` is included in the XML doc for the XML API:
with patch.object(solr, "get_session") as mock:
mock().post.return_value = MagicMock(status_code=200)
solr.add([{"id": "doc1", "foo": "bar"}], commitWithin="1000", boost={"foo": 2.0})
mock().post.assert_called_once_with(
'http://pysolr.test/update/',
data=b'<add commitWithin="1000"><doc><field name="id">doc1</field><field name="foo" boost="2.0">bar</field></doc></add>',
headers={'Content-type': 'text/xml; charset=utf-8'},
files=None,
timeout=60,
auth=None,
)
```
I made a start on an update that switches to including the `commitWithin` in the URL for both JSON and XML. This simplifies things compared to including it in the message body, because to do that for the JSON API, you'd have to switch from the structure that you're currently using (a list of docs) to a different structure (such as a dict), because the list of docs format doesn't seem to provide any way to specify `commitWithin`.
See pull request: https://github.com/django-haystack/pysolr/pull/421
## Configuration
* Operating system version: Ubuntu 20.04.6 LTS
* Search engine version: 4.10.4 (Test version)
* Python version: 3.10.11
* pysolr version: master as of 30a2c01b34b6f389392cea653c94e69bb72327a2
关闭于 2023-09-08 1 条评论