如何更新文档而不丢失字段?

发布于 2024-12-05 14:33:01 字数 1205 浏览 2 评论 0原文

CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr/");
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id", "id1");
doc1.addField("name", "doc1");
doc1.addField("price", new Float(10));
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("id", "id1");
doc2.addField("name", "doc2");

server.add(doc1);
server.add(doc2);
server.commit();

SolrQuery query = new SolrQuery();
query.setQuery("id:id1");
query.addSortField("price", SolrQuery.ORDER.desc);
QueryResponse rsp = server.query(query);
Iterator<SolrDocument> iter = rsp.getResults().iterator();
while(iter.hasNext()){
    SolrDocument doc = iter.next();
    Collection fieldNames = doc.getFieldNames();
    Iterator<String> fieldIter = fieldNames.iterator();
    StringBuffer content = new StringBuffer("");
    while(fieldIter.hasNext()){
        String field = fieldIter.next();
        content.append(field+":"+doc.get(field)).append(" ");
        //System.out.println(field);
    }
    System.out.println(content);
}

问题是我想得到结果“id:id1 name:doc2 Price:10.0”,但输出是“id:id1 name:doc2”... 所以我想知道如果我想得到“id:id1 name:doc2 Price:10.0”的结果,我该如何修改我的编程?

CommonsHttpSolrServer server = new CommonsHttpSolrServer("http://localhost:8983/solr/");
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id", "id1");
doc1.addField("name", "doc1");
doc1.addField("price", new Float(10));
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("id", "id1");
doc2.addField("name", "doc2");

server.add(doc1);
server.add(doc2);
server.commit();

SolrQuery query = new SolrQuery();
query.setQuery("id:id1");
query.addSortField("price", SolrQuery.ORDER.desc);
QueryResponse rsp = server.query(query);
Iterator<SolrDocument> iter = rsp.getResults().iterator();
while(iter.hasNext()){
    SolrDocument doc = iter.next();
    Collection fieldNames = doc.getFieldNames();
    Iterator<String> fieldIter = fieldNames.iterator();
    StringBuffer content = new StringBuffer("");
    while(fieldIter.hasNext()){
        String field = fieldIter.next();
        content.append(field+":"+doc.get(field)).append(" ");
        //System.out.println(field);
    }
    System.out.println(content);
}

The question is that I want to get the result "id:id1 name:doc2 price:10.0", but the output is "id:id1 name:doc2"...
So I want to know if I want to get the result as "id:id1 name:doc2 price:10.0", how can I modify my programming?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2024-12-12 14:33:01

当您添加具有相同 id 的文档时。您基本上是在添加同一个文档两次。
Solr 将更新/覆盖该文档。更新基本上就是删除和添加。

由于您使用相同 id 添加的第二个文档没有价格字段,因此不会添加它,并且您不会在索引中找到它。

当您添加回文档时,您需要更改所有字段并保持不变。

doc2.addField("price", new Float(10)); // should add it back to the document

As you are adding the documents with same id. You are basically adding a same document twice.
Solr will update/overwrite the document. updated is basically delete and add.

As the second document you added with the same id does not have the price field, it won't be added and you wont find it the index.

you would need to have all the fields changed and unchanged when you are adding back the document.

doc2.addField("price", new Float(10)); // should add it back to the document
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文