你直接更新文档就好了,比如下面:
利用SolrJ完成Index Document的添加操作
/**
* <b>function:</b> 添加doc文档
* @author hoojo
* @createDate 2011-10-21 上午09:49:10
*/
@Test
public void addDoc() {
//创建doc文档
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", 1);
doc.addField("name", "Solr Input Document");
doc.addField("manu", "this is SolrInputDocument content");
try {
//添加一个doc文档
UpdateResponse response = server.add(doc);
fail(server.commit());//commit后才保存到索引库
fail(response);
fail("query time:" + response.getQTime());
fail("Elapsed Time:" + response.getElapsedTime());
fail("status:" + response.getStatus());
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
query("name:solr");
} 复制代码
在apache-solr-3.4.0\example\solr\conf目录下的schema.xml中可以找到有关于field属性的配置,schema.xml中的field就和上面Document文档中的field(id、name、manu) 对应。如果出现ERROR:unknown field 'xxxx'就表示你设置的这个field在schema.xml中不存在。如果一定要使用这个field,请你在schema.xml中进行filed元素的配置。具体请参考前面的章节。
注意这里对你的问题很重要 :在schema.xml中配置了uniqueKey为id,就表示id是唯一的。如果在添加Document的时候,id重复添加。那么后面添加的相同id的doc会覆盖前面的doc,类似于update更新操作,而不会出现重复的数据,(这里 也就是你说说hits增加,你直接做一个更新不就行了)