question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Delete specific fields from a document

See original GitHub issue

I have

  • Tested with the latest release
  • Tested with the current master branch
  • Searched for similar existing issues

Expected behaviour

Solr allows deleting a field by providing the null attribute to an add operation. In the example below, the field skills should be deleted from the document with “employeeId” 05991:

<add>
    <doc>
        <field name="employeeId">05991</field>
        <field name="skills" update="set" null="true" />
    </doc>
</add>

I would expect that the following call would be equivalent to the xml above:

docs = [{'empolyeeId':05991, 'skills':None}]
solr.add(docs, fieldUpdates={'skills':'set'})

Actual behaviour

As far as I understand, this operation is not yet supported in pysolr. I could not find a way to input the null attribute to the add method of Solr class.

Configuration

  • Operating system version: Ubuntu 14.04, kernel 3.16.0-77-generic
  • Search engine version: 4.7
  • Python version: 2.7.6
  • pysolr version: 3.5.0

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
jogildercommented, Dec 1, 2016

Hi, I updated the _build_doc method. At the moment it doesn’t do anything if there is a ‘null value’ passed. I have added in an extra check if the user passes in an empty string or list and they are doing a ‘set’ fieldUpdate then it will add in the document but will also add an extra attribute null=true. This works for me as I am passing in empty lists, it wouldn’t work if someone passed in field=None. `

def _build_field(self, value, key, _atomic_update_set, boost=None, fieldUpdates=None):
    attrs = {'name': key}
    if fieldUpdates and key in fieldUpdates:
        attrs['update'] = fieldUpdates[key]
        if self._is_null_value(value) and _atomic_update_set:
            attrs['null'] = u'true'
    if boost and key in boost:
        attrs['boost'] = force_unicode(boost[key])
    field = ElementTree.Element('field', **attrs)
    field.text = self._from_python(value)
    return field

def _is_atomic_update_set(self,field_update_text):
    _atomic_update_set = False
    if field_update_text.lower() == "set":
        _atomic_update_set = True
    return _atomic_update_set

def _build_doc(self, doc, boost=None, fieldUpdates=None):
    doc_elem = ElementTree.Element('doc')
    for key, value in doc.items():
        if key == NESTED_DOC_KEY:
            for child in value:
                doc_elem.append(self._build_doc(child, boost, fieldUpdates))
            continue

        if key == 'boost':
            doc_elem.set('boost', force_unicode(value))
            continue

        _atomic_update_set = self._is_atomic_update_set(fieldUpdates.get(key, u''))
        # To avoid multiple code-paths we'd like to treat all of our values as iterables:
        if isinstance(value, (list, tuple)):
            values = value
            #we want to be able to do the for loop if we have an empty list
            if _atomic_update_set and not values:
                values = (u'',)
        else:
            values = (value, )
        #if this is a list then we go through the list or tuple - doesn't work for an empty list
        for bit in values:
            #allow empty strings if this is a set update but create null attribute
            if self._is_null_value(bit) and not _atomic_update_set:
                continue
            field = self._build_field(bit, key, _atomic_update_set, boost, fieldUpdates)
            doc_elem.append(field)
    return doc_elem

`

0reactions
jogildercommented, Jul 5, 2018

so is this fixed then?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to remove a field completely from a MongoDB document?
Yes, you have to use $unset operator, but this unset is going to remove the words key which does not exist for a...
Read more >
How to Remove a Field from a MongoDB Document ($unset)
In MongoDB, you can use the $unset field update operator to completely remove a field from a document. The $unset operator is designed ......
Read more >
Removing Specific Fields - Microsoft Word Tips
If you use fields in your documents, you may need a way to delete a specific type of field, while leaving all the...
Read more >
How to remove a field from a document in MongoDB
In MongoDB, The $unset operator is used to delete or remove a particular field. Consider the following syntax: { $unset: { <field1>: "",...
Read more >
MongoDB: How to Remove a Field from Every Document
You can use the following methods to remove fields from every document in a collection in MongoDB: Method 1: Remove One Field db.collection....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found