Schemaless模式
schemaless模式是一组solr功能的集合,允许用户通过简单的索引例子数据快速构建一个有效的schema,而不需要手动的编辑schema.这些solr功能都是在solrconfig.xml中指定的.主要是:
schema管理:schema修改是通过Solr API 而不是手动修改来完成的.参考--在solrconfig中管理schema定义.
字段值class的猜测:明显的,不可见的字段运行是通过一组级联的基于值的解析器,这些解析器可以猜测字段值的java类,用来解析Boolean, Integer, Long, Float, Double, 和Date.
基于字段值的java类,自动schema字段添加.
这三个功能预先配置在example/example-schemaless/solr/目录下,为了使用预先配置的schemaless模式,到example目录下,启动solr,使用一下命令设置solr.solr.home系统属性到这个目录.
java -Dsolr.solr.home=example-schemaless/solr -jar start.jar
example-schemaless/solr/collection1/conf/下的schema主要依赖两个字段,id和_version_,这些可以调用schema API的/schema/fields来查看.curl http://localhost:8983/solr/schema/fields :
{
"responseHeader":{
"status":0,
"QTime":1},
"fields":[{
"name":"_version_",
"type":"long",
"indexed":true,
"stored":true},
{
"name":"id",
"type":"string",
"multiValued":false,
"indexed":true,
"required":true,
"stored":true,
"uniqueKey":true}]}
添加一个cvs文档,它的字段没有在schem中添加,具有基于值的字段类型.
curl "http://localhost:8983/solr/update?commit=true" -H "Content-type:application/csv"
-d '
id,Artist,Album,Released,Rating,FromDistributor,Sold
44C,Old Shews,Mead for Walking,1988-08-13,0.01,14,0'
输出表明成功:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader"><int name="status">0</int><int name="QTime">106</int></lst>
</response>
在schema中,现在的字段(curl http://localhost:8983/solr/schema/fields):
{
"responseHeader":{
"status":0,
"QTime":1},
"fields":[{
"name":"Album",
"type":"text_general"}, // Field value guessed as String -> text_general
fieldType
{
"name":"Artist",
"type":"text_general"}, // Field value guessed as String -> text_general
fieldType
{
"name":"FromDistributor",
"type":"tlongs"}, // Field value guessed as Long -> tlongs fieldType
{
"name":"Rating",
"type":"tdoubles"}, // Field value guessed as Double -> tdoubles fieldType
{
"name":"Released",
"type":"tdates"}, // Field value guessed as Date -> tdates fieldType
{
"name":"Sold",
"type":"tlongs"}, // Field value guessed as Long -> tlongs fieldType
{
"name":"_version_",
...
},
{
"name":"id",
...
}]}
一旦一个字段添加到schema中,它的字段类型就是固定的.举例说明,如果已经添加了上一个文档,字段Sold的字段类型就是tlongs,但是下面这个文档这个字段中不是一个整数数字值.
curl "http://localhost:8983/solr/update?commit=true" -H "Content-type:application/csv"
-d '
id,Description,Sold
19F,Cassettes by the pound,4.93'
输出结果表面失败:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader">
<int name="status">400</int>
<int name="QTime">7</int>
</lst>
<lst name="error">
<str name="msg">ERROR: [doc=19F] Error adding field 'Sold'='4.93' msg=For input string: "4.93"</str>
<int name="code">400</int>
</lst>
</response>