使用 Java 更新 Solr 中的文档

发布于 2024-11-07 06:29:56 字数 795 浏览 0 评论 0原文

众所周知,wiki 中 Solrj 的文档非常贫乏。我设法使用 CommonsHttpSolrServer 查询索引,但从未使用嵌入式版本查询索引。不管怎样,现在我使用 EdgeNGrams 来显示自动建议,并且我的索引中有一个字段“count”,这样我就可以按照人们查询元素的次数对结果进行排序。

我现在想做的是能够更新我的 Java 程序中的这个“count”字段,我想这应该很容易吧?我从源代码中查看了测试文件,但它非常复杂,并且尝试做类似的事情对我来说总是失败。也许通过使用 Solrj ?

感谢您的帮助。

编辑: 在我的 java 代码中,我有:

CoreContainer.Initializer initializer = new CoreContainer.Initializer();
CoreContainer coreContainer = initializer.initialize();

此时我期望得到的是 coreContainer 中 solr.xml 中定义的核心,但那里没有核心(但 defaultCoreName 表示 collection1)。我的 solr.xml 文件与示例目录中的相同:

<solr persistent="false">
  <cores adminPath="/admin/cores" defaultCoreName="collection1">
    <core name="collection1" instanceDir="." />
  </cores>
</solr>

As everybody knows, the documentation of Solrj in the wiki is pretty poor. I managed to query the index using the CommonsHttpSolrServer, but never with the Embedded version. Anyway, now I'm using the EdgeNGrams to display auto-suggestions, and I have a field "count" in my index, so that I can sort the results by the number of times people queried the element.

What I want to do now, is to be able to update this "count" field in my Java program, which should be quite easy I guess? I looked at the test files from the source code, but it's very complicated, and trying to do something similar always failed for me. Maybe by using Solrj?

Thanks for your help.

Edit:
In my java code, I have:

CoreContainer.Initializer initializer = new CoreContainer.Initializer();
CoreContainer coreContainer = initializer.initialize();

What I expect to get at this point, is the cores defines in solr.xml present in the coreContainer, but there is no core there (but defaultCoreName says collection1). My solr.xml file is the same as in the example dir:

<solr persistent="false">
  <cores adminPath="/admin/cores" defaultCoreName="collection1">
    <core name="collection1" instanceDir="." />
  </cores>
</solr>

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

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

发布评论

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

评论(2

请你别敷衍 2024-11-14 06:29:56

修改自 这个测试示例。要将值添加到 Solr,然后对其进行修改,您可以执行以下操作:

//add value to Solr
doc = new SolrInputDocument();
doc.addField("id", "A");
doc.addField("value", 10);
client.add(doc);
client.commit();

//query Solr
SolrQuery q = new SolrQuery("id:A");
QueryResponse r = client.query(q);

//update value
SolrDocument oldDoc = r.getResults().get(0);
SolrInputDocument newDoc = new SolrInputDocument();
newDoc.addField("id", oldDoc.getFieldValue("id");
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("inc", 15);
newDoc.addField("value", map);
client.add(newDoc);
client.commit();

这会将原始 10 值增加到 25。您还可以“添加”到现有字段,或者通过更改您输入的命令来“设置”现有值你在 HashMap 中放入的内容。

Modified from this test example. To add a value to Solr and then subsequently modify it you can do the following:

//add value to Solr
doc = new SolrInputDocument();
doc.addField("id", "A");
doc.addField("value", 10);
client.add(doc);
client.commit();

//query Solr
SolrQuery q = new SolrQuery("id:A");
QueryResponse r = client.query(q);

//update value
SolrDocument oldDoc = r.getResults().get(0);
SolrInputDocument newDoc = new SolrInputDocument();
newDoc.addField("id", oldDoc.getFieldValue("id");
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("inc", 15);
newDoc.addField("value", map);
client.add(newDoc);
client.commit();

This increments the original 10 value to 25. You can also "add" to an existing field or simply "set" an existing value by changing what command you put what you put the in the HashMap.

十二 2024-11-14 06:29:56

我最后只是将这个计数存储在 Solr 中,然后检索它并更新它,然后运行更新,因为不可能更新 Solr 中的单个字段以及计数字段,这可能非常方便!

I finally just store this count in Solr, then retrieve it and update it, then run an update, since it is not possible to update a single field in Solr and also the count field, which could be very handy!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文