开发者

How can I update document without losing fields?

开发者 https://www.devze.com 2023-04-05 21:27 出处:网络
CommonsHttpSolrServer server = new CommonsHttpSolrServer(\"http://localhost:8983/solr/\"); SolrInputDocument doc1 = new SolrInputDocument();
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开发者_StackOverflow社区 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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消