我正在使用 SolrJ 将 POJO 索引到 Solr,并且具有数值的字符串属性被映射到 org.apache.solr.schema.TrieLongField
类型,这又会导致 BindingException
当我尝试从 Solr 检索文档时。
我的类注释为 @Field
在 setter 上,我使用 client.addBean(object)
添加文档。
以下代码可以重现此问题:
public class SolrIndexTest {
@Field
public Long longField;
@Field
public String stringField;
public static void main(String[] args) {
//test core created with the following command
//sudo su - solr -c "/opt/solr/bin/solr create -c test -n data_driven_schema_configs"
HttpSolrClient client = new HttpSolrClient.Builder("http://localhost:8983/solr/test").build();
client.setParser(new XMLResponseParser());
SolrIndexTest obj1 = new SolrIndexTest();
obj1.longField = 1L;
obj1.stringField = "1"; // 1st doc: numeric value
SolrIndexTest obj2 = new SolrIndexTest();
obj2.longField = 2L;
obj2.stringField = "Text string"; // 2nd doc: text value
try {
client.addBean(obj1);
client.commit();
} catch (Exception e) {
e.printStackTrace();
}
try {
client.addBean(obj2); // This line will throw a BindingException
client.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
最佳答案
当您在 Schemaless mode 中运行 Solr Collection 时,字段类型( double 、整数、字符串等)由添加到字段名称的后缀获取。 或者通过猜测字段类型,当前可以使用 boolean 、整数、长整型、浮点、 double 和日期的解析器(不是字符串)。
Schemaless Mode is a set of Solr features that, when used together, allow users to rapidly construct an effective schema by simply indexing sample data, without having to manually edit the schema. These Solr features, all controlled via solrconfig.xml, are:
- Managed schema: Schema modifications are made at runtime through Solr APIs, which requires the use of schemaFactory that supports these changes - see Schema Factory Definition in SolrConfig for more details.
- Field value class guessing: Previously unseen fields are run through a cascading set of value-based parsers, which guess the Java class of field values - parsers for Boolean, Integer, Long, Float, Double, and Date are currently available.
- Automatic schema field addition, based on field value class(es): Previously unseen fields are added to the schema, based on field value Java classes, which are mapped to schema field types - see Solr Field Types.
简而言之,如果您想正确映射您的字段类型,只需添加正确的后缀:
@Field
public Long longField_l; // _l stands for long
@Field
public String stringField_s; // _s stands for string
您将看到预期的结果:
<doc>
<long name="longField_l">1</long>
<str name="stringField_s">1</str>
</doc>
<doc>
<long name="longField_l">2</long>
<str name="stringField_s">Text string</str>
</doc>
如果您最后打开托管架构
文件,您将看到用于映射类型的动态字段列表。
我在这里复制了其中一些:
<dynamicField name="*_i" type="int" indexed="true" stored="true"/>
<dynamicField name="*_s" type="string" indexed="true" stored="true"/>
<dynamicField name="*_l" type="long" indexed="true" stored="true"/>
<dynamicField name="*_t" type="text_general" indexed="true" stored="true"/>
<dynamicField name="*_b" type="boolean" indexed="true" stored="true"/>
<dynamicField name="*_f" type="float" indexed="true" stored="true"/>
<dynamicField name="*_d" type="double" indexed="true" stored="true"/>
<dynamicField name="*_p" type="location" indexed="true" stored="true"/>
<dynamicField name="*_c" type="currency" indexed="true" stored="true"/>
关于java - 使用 Schemaless Collection 进行错误的 Java 到 Solr 类型映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44640224/