简体   繁体   English

将multiValued字段添加到SolrInputDocument

[英]Add multiValued field to a SolrInputDocument

We are using a solr embeded instance for Java SolrJ. 我们正在为Java SolrJ使用solr embeded实例。

I want to add a multivalued field to a document. 我想在文档中添加一个多值字段。 The multivalued field is a coma separated String. 多值字段是逗号分隔的字符串。

In Java I want to do: 在Java中我想做:

solrInputDocument.addField(Field1, "value1,value2,value3");

The definition for Field1 in the schema is as follow 模式中Field1的定义如下

<field name="Field1" type="multiValuedField"   indexed="true"  stored="true"  multiValued="true" required="false"/>

<fieldType name="multiValuedField" class="solr.TextField" positionIncrementGap="100">
     <analyzer type="index">
         <tokenizer class="solr.ClassicTokenizerFactory"/>
     </analyzer>
</fieldType> 

With this configuration we were expecting that when we invoke the addField method Solr was able to check that it is a multiValuedField and so it converts the String into an arrayList with the different values. 通过这种配置,我们期望当我们调用addField方法时,Solr能够检查它是否为multiValuedField,因此它将String转换为具有不同值的arrayList。

Instead we are getting an arraylist with just one value that is in fact the original string added to the document. 相反,我们只得到一个只有一个值的arraylist,实际上是添加到文档中的原始字符串。

Question: should be the tokenizer taking care of this, or should we do it ourselves when we are adding multivalued fields to the document? 问题:应该是标记器处理这个问题,还是应该在我们向文档中添加多值字段时自己做?

Thanks. 谢谢。

The addField method of SolrInputDocument accepts a string and an object. SolrInputDocument的addField方法接受字符串和对象。 So to handle multivalued fields, you can pass in an ArrayList with your desired values for the second parameter, and SolrJ will update the multivalued field accordingly: 因此,要处理多值字段,您可以传入一个ArrayList,其中包含第二个参数的所需值,SolrJ将相应地更新多值字段:

String[] valuesArray = {"value1", "value2", "value3"};
ArrayList<String> values = new ArrayList<String>(Arrays.asList(valuesArray));
solrInputDocument.addField("Field1", values);

You can call SolrInputDocument.addField(String name, Object value) either multiple times passing an Object as the value or a single time passing a Collection as the value. 您可以SolrInputDocument.addField(String name, Object value)调用SolrInputDocument.addField(String name, Object value)作为值传递Object ,或者传递Collection作为值传递一次。

Example #1: 示例#1:

ArrayList<String> values = Arrays.asList({"value1", "value2", "value3"});
solrInputDocument.addField("field", values);

Example #2: 示例#2:

solrInputDocument.addField("field", "value1");
solrInputDocument.addField("field", "value2");
solrInputDocument.addField("field", "value3");

Both of these examples will result in the same thing. 这两个例子都会产生同样的结果。 You could even mix and match the calls if you needed to. 如果需要,您甚至可以混合和匹配呼叫。 To see why this works, trace the calls into the Solr source code and you'll find the multi-valued cases are handled in SolrInputField.addValue(Object v, float b) . 要了解其工作原理,请将调用跟踪到Solr源代码中,您会发现多值案例在SolrInputField.addValue(Object v, float b)

/**
 * Add values to a field.  If the added value is a collection, each value
 * will be added individually.
 */
@SuppressWarnings("unchecked")
public void addValue(Object v, float b) {
  if( value == null ) {
    if ( v instanceof Collection ) {
      Collection<Object> c = new ArrayList<Object>( 3 );
      for ( Object o : (Collection<Object>)v ) {
        c.add( o );
      }
      setValue(c, b);
    } else {
      setValue(v, b);
    }

    return;
  }

  boost *= b;

  Collection<Object> vals = null;
  if( value instanceof Collection ) {
    vals = (Collection<Object>)value;
  }
  else {
    vals = new ArrayList<Object>( 3 );
    vals.add( value );
    value = vals;
  }

  // Add the new values to a collection
  if( v instanceof Iterable ) {
    for( Object o : (Iterable<Object>)v ) {
      vals.add( o );
    }
  }
  else if( v instanceof Object[] ) {
    for( Object o : (Object[])v ) {
      vals.add( o );
    }
  }
  else {
    vals.add( v );
  }
}

As I am not using SOLRJ to add elements to SOLR I am not really sure, but I think you should have used 由于我没有使用SOLRJ向SOLR添加元素,我不太确定,但我认为你应该使用它

solrInputDocument.addField(Field1, "value1");
solrInputDocument.addField(Field1, "value2");
solrInputDocument.addField(Field1, "value3");

Confirmed. 证实。 Tokenizers doesn't "cast" the data for you. 标记符不会为您“转换”数据。 So, the approach is to work on the data during the loading time, to have it in the proper format. 因此,方法是在加载期间处理数据,以使其具有正确的格式。

Thnks for your help. 请耐心等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM