DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Testing Solr update XML messages

Testing Solr update XML messages

Bas De Nooijer user avatar by
Bas De Nooijer
·
Apr. 14, 11 · Java Zone · News
Like (0)
Save
Tweet
18.97K Views

Join the DZone community and get the full member experience.

Join For Free
When updating a Solr index with the DataImportHandler or one of the available Solr clients you don’t really need to bother with all the details of updates. Most clients just give a simplified “add”, “delete” and “commit” interface to Solr updates, issued as separate commands.

For most clients these commands are actually translated into Solr XML update messages. Taking a look at the documentation in the Solr wiki (http://wiki.apache.org/solr/UpdateXmlMessages) shows all the options available. It also shows that it is possible to combine multiple commands in a single request.
So if you need to do several deletes and add some documents this can all be done in a single request. This got me wondering, how does this actually work? Are all ‘commands’ executed in the order of the XML message?

To test this I have created a very simple Solr index with just two fields:

  • id (int)
  • name (string)

For posting XML update messages I use Curl:

curl http://localhost:8983/solr/core0/update? -H "Content-Type: text/xml" --data-binary @data.xml

This is the content of the data.xml file:

<update>
  <delete>
    <query>*:*</query>
  </delete>
  <add>
    <doc>
      <field name="id">1</field>
      <field name="name">item 1</field>
    </doc>
    <doc>
      <field name="id">2</field>
      <field name="name">item 2</field>
    </doc>
    <doc>
      <field name="id">3</field>
      <field name="name">item 3</field>
    </doc>
  </add>
  <commit/>
</update>

The index can easily be reset by re-posting this XML. The delete all query ensures a complete cleanup.
For testing a complex update I created this XML:

<update>
  <delete>
    <id>3</id>
  </delete>
  <commit/>
  <add>
    <doc>
      <field name="id">4</field>
      <field name="name">item 4</field>
    </doc>
  </add>
  <rollback/>
  <add>
    <doc>
      <field name="id">5</field>
      <field name="name">item 5</field>
    </doc>
    <doc>
      <field name="id">6</field>
      <field name="name">item 6</field>
    </doc>
  </add>
  <delete>
    <id>1</id>
  </delete>
  <commit/>
  <optimize/>
</update>

The expected behaviour is:

  1. we start with [1,2,3] in the index.
  2. we delete document 3 and commit, so we now have [1,2]
  3. add document 4 and rollback, so no change: [1,2]
  4. add document 5 and 6, delete 1 and commit+optimize: [2, 5, 6]

The outcome: just as expected! After posting the update the index contains document 2, 5 and 6. So Solr does handle the commands in the update XML message in the exact given order.

An important note: this example was made to test Solr. In most cases where the order of the commands matters something is wrong. In this test the order only matters because of the add and rollback of document 4. But in that case document 4 shouldn’t be in the update anyway. And normally you should only commit once, below all add/delete commands.

The other parts of this test are more useful. Like using Curl for testing XML update messages and adding multiple commands to a single update message. The most obvious example is adding multiple documents in a single update, and most clients support this. But combining multiple delete queries or even combining delete queries with adding of documents is not supported by most clients, while it can save a lot of requests.
There is a downside though, if any command fails everything below it will not be executed.

XML

Published at DZone with permission of Bas De Nooijer, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Ultra-Fast Microservices: When Microstream Meets Payara
  • Debugging Deadlocks and Race Conditions
  • How to Generate Fake Test Data
  • Top 11 Cloud Platforms for Internet of Things (IoT)

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo