solr cloud 之添加,删除,更新Document

    搭建完成solr cloud的集群之后,接下来的工作就是需要向集群中导入数据,之后可能要更新数据,或者删除数有,假设有有一个服务的schema中的field是这样定义的:

<fields>
   <field name="id"     type="string"  indexed="true" stored="true" required="true" /> 
   <field name="name"   type="text"    indexed="true" stored="true" required="true" />
   <field name="age" type="string"  indexed="true" stored="true" />
   <field name="comefrom" type="string"  indexed="true" stored="true" />
   <field name="_version_" type="long" indexed="true" stored="true"/>
</fields>

   

   新增记录

   首先可以通过最简便的方式向集群中添加数据,在cmd命令行中敲入以下命令:

java -Durl=http://localhost:8983/solr/update -jar   post.jar terminator.xml
   terminator.xml 文件的内容是:
<add>

<doc>
  <field name="id">9999999</field>
  <field name="name">abcdefghijkmn</field>
  <field name="source">kkkkkkkkkkkkkkkkkkkkk</field>
  <!-- Join -->
  <field name="shape">gggggggggggggg</field>
  <field name="ptvector">llllllllllllllll</field>
  <field name="quad">quadquadquadquadquad</field>
  <field name="geohash">geohashgeohashgeohashgeohashgeohashgeohash</field>
</doc>

<doc>
  <field name="id">9999998</field>
  <field name="name">abcdefghijkmn</field>
  <field name="source">kkkkkkkkkkkkkkkkkkkkk</field>
  <!-- Join -->
  <field name="shape">gggggggggggggg</field>
  <field name="ptvector">llllllllllllllll</field>
  <field name="quad">quadquadquadquadquad</field>
  <field name="geohash">geohashgeohashgeohashgeohashgeohashgeohash</field>
</doc>

<doc>
  <field name="id">9999997</field>
  <field name="name">abcdefghijkmn</field>
  <field name="source">kkkkkkkkkkkkkkkkkkkkk</field>
  <!-- Join -->
  <field name="shape">gggggggggggggg</field>
  <field name="ptvector">llllllllllllllll</field>
  <field name="quad">quadquadquadquadquad</field>
  <field name="geohash">geohashgeohashgeohashgeohashgeohashgeohash</field>
</doc>

<doc>
  <field name="id">9999996</field>
  <field name="name">abcdefghijkmn</field>
  <field name="source">kkkkkkkkkkkkkkkkkkkkk</field>
  <!-- Join -->
  <field name="shape">gggggggggggggg</field>
  <field name="ptvector">llllllllllllllll</field>
  <field name="quad">quadquadquadquadquad</field>
  <field name="geohash">geohashgeohashgeohashgeohashgeohashgeohash</field>
</doc>
</add>
 

成功执行命令:

D:\download\solr-4.1.0\solr-4.1.0\example\exampledocs>
java -Durl=http://localhost:8983/solr/update -jar post.jar terminator.xm
SimplePostTool version 1.5 Posting files to base url http://localhost:8983/solr/update using content-type application/xml..
POSTing file terminator.xml 1 files indexed. COMMITting Solr index changes to http://localhost:8983/solr/update..

 Document的share规则:

通过以上命令,成功将4条记录插入到了集群中。在solr节点的查询页面进行查询,显示结果集如下:

<?xml version="1.0" encoding="UTF-8"?>
<response>

<lst name="responseHeader">
  <int name="status">0</int>
  <int name="QTime">3967</int>
  <lst name="params">
    <str name="fl">*,[shard]</str>
    <str name="indent">true</str>
    <str name="q">id:999999*</str>
    <str name="wt">xml</str>
  </lst>
</lst>
<result name="response" numFound="4" start="0" maxScore="1.0">
  <doc>
    <str name="id">9999999</str>
    <str name="name">abcdefghijkmn</str>
    <str name="source">kkkkkkkkkkkkkkkkkkkkk</str>
    <str name="shape">gggggggggggggg</str>
    <str name="ptvector">llllllllllllllll</str>
    <str name="quad">quadquadquadquadquad</str>
    <str name="geohash">geohashgeohashgeohashgeohashgeohashgeohash</str>
    <long name="_version_">1434087019164729344</long>
    <str name="[shard]">10.1.31.91:80/solr/collection1/</str></doc>
  <doc>
    <str name="id">9999998</str>
    <str name="name">abcdefghijkmn</str>
    <str name="source">kkkkkkkkkkkkkkkkkkkkk</str>
    <str name="shape">gggggggggggggg</str>
    <str name="ptvector">llllllllllllllll</str>
    <str name="quad">quadquadquadquadquad</str>
    <str name="geohash">geohashgeohashgeohashgeohashgeohashgeohash</str>
    <long name="_version_">1434087019165777920</long>
    <str name="[shard]">10.1.31.91:80/solr/collection1/</str></doc>
  <doc>
    <str name="id">9999997</str>
    <str name="name">abcdefghijkmn</str>
    <str name="source">kkkkkkkkkkkkkkkkkkkkk</str>
    <str name="shape">gggggggggggggg</str>
    <str name="ptvector">llllllllllllllll</str>
    <str name="quad">quadquadquadquadquad</str>
    <str name="geohash">geohashgeohashgeohashgeohashgeohashgeohash</str>
    <long name="_version_">1434087018344742912</long>
    <str name="[shard]">10.1.31.91:8983/solr/collection1/</str></doc>
  <doc>
    <str name="id">9999996</str>
    <str name="name">abcdefghijkmn</str>
    <str name="source">kkkkkkkkkkkkkkkkkkkkk</str>
    <str name="shape">gggggggggggggg</str>
    <str name="ptvector">llllllllllllllll</str>
    <str name="quad">quadquadquadquadquad</str>
    <str name="geohash">geohashgeohashgeohashgeohashgeohashgeohash</str>
    <long name="_version_">1434087018867982336</long>
    <str name="[shard]">10.1.31.91:8983/solr/collection1/</str></doc>
</result>
</response>

   大家会发现在输出的结果集上有一个[shard]字段,标识该条记录存储在集群中的哪个share分区中。要在结果集中shard字段可以通过设置 fl=*,[shard]做到。从[shard]属性的值来看四条记录被均匀的分布到了两个share中。

更新数据

删除数据

java -Ddata=args  -jar post.jar '<delete><id>9999996</id></delete>'

或者

java -Ddata=args  -jar post.jar '<delete><query>id:9999996</query></delete>'

经过测试,以上两条命令都可以成功删除id为9999996的那条记录。

相关推荐