【问题标题】:Updatable Fields In SolrSolr 中的可更新字段
【发布时间】:2013-06-06 08:39:28
【问题描述】:

我正在使用 Solr 搜索我的网页数据语料库。我的 solr-indexer 将创建几个字段和相应的值。但是,我想更频繁地更新其中一些字段,例如该页面上的点击次数。这些字段不需要可​​索引,我不需要对这些字段值执行搜索。但是我确实想获取它们并经常更新它们。 我是 solr 的新手,所以用一些正在运行的示例/代码提供更具描述性的答案可能会更好地帮助我。

【问题讨论】:

    标签: solr


    【解决方案1】:

    如果您使用的是 Solr 4+,是的,您可以将部分更新推送到 Solr 索引。

    对于部分更新,您的 schema.xml 中的所有字段都需要存储

    这是您的字段部分的外观:

    <fields>
      <field name="id" type="string" indexed="true" stored="true" required="true" />
      <field name="title" type="text_general" indexed="true" stored="true"/>
      <field name="description" type="text_general" indexed="true" stored="true" />
      <field name="body" type="text_general" indexed="true" stored="true"/>
      <field name="clicks" type="integer" indexed="true" stored="true" />
    </fields>
    

    现在,当您向其中一个字段发送部分更新时,例如:在您的情况下是“点击”;在后台 Solr 将去获取该文档的所有其他字段的值,例如标题、描述、正文,删除旧文档并将新更新的文档推送到 Solr 索引。

    localhost:8080/solr/update?commit=true' -H 'Content-type:application/json' -d '[{"id":"1","clicks":{"set":100}}]
    

    这是关于部分更新的好文档:http://solr.pl/en/2012/07/09/solr-4-0-partial-documents-update/

    【讨论】:

    【解决方案2】:

    示例 SOLR- 部分更新代码:

    先决条件:需要存储的字段。

    需要在直接更新处理器下配置更新日志路径

      <updateHandler class="solr.DirectUpdateHandler2">
    
        <!-- Enables a transaction log, used for real-time get, durability, and
             and solr cloud replica recovery.  The log can grow as big as
             uncommitted changes to the index, so use of a hard autoCommit
             is recommended (see below).
             "dir" - the target directory for transaction logs, defaults to the
                    solr data directory.  --> 
        <updateLog>
          <str name="dir">${solr.ulog.dir:}</str>
        </updateLog>
      </updateHandler>
    

    代码:

    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import org.apache.solr.client.solrj.SolrServer;
    import org.apache.solr.client.solrj.SolrServerException;
    import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrServer;
    import org.apache.solr.client.solrj.impl.HttpSolrServer;
    import org.apache.solr.common.SolrInputDocument;
    public class PartialUpdate {
        public static void main(String args[]) throws SolrServerException,
                IOException {
            SolrServer server = new HttpSolrServer("http://localhost:8080/solr");
            SolrInputDocument doc = new SolrInputDocument();
            Map<String, String> partialUpdate = new HashMap<String, String>();
            // set - to set a field.
            // add - to add to a multi-valued field.
            // inc - to increment a field.
            partialUpdate.put("set", "peter"); // value that need to be set
            doc.addField("id", "122344545"); // unique id
            doc.addField("fname", partialUpdate); // value of field fname corresponding to id 122344545 will be set to 'peter'
            server.add(doc);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多