時間字段類型特別說明json
Solr中提供的時間字段類型( DatePointField, DateRangeField,廢除的TrieDateField )是以時間毫秒數來存儲時間的。api
要求字段值以ISO-8601標準格式來表示時間:YYYY-MM-DDThh:mm:ssZapp
Z表示是UTC時間(注意:就沒有時區了)。1999-05-20T17:33:18Zcurl
秒上能夠帶小數來表示毫秒,超出精度部分會被忽略: post
1972-05-20T17:33:18.772Z
1972-05-20T17:33:18.77Z
1972-05-20T17:33:18.7Zurl
公元前:在前面加減號 - 9999後,在前面加加號 +spa
注意:查詢時若是是直接的時間串,須要用轉移符轉義:code
datefield:1972-05-20T17\:33\:18.772Z
datefield:"1972-05-20T17:33:18.772Z"
datefield:[1972-05-20T17:33:18.772Z TO *]orm
DateRangeField 時間段類型特別說明xml
DateRangeField用來支持對時間段數據的索引,它遵照上一頁講到的時間格式,支持兩種時間段表示方式:
方式一:截斷日期,它表示整個日期跨度的精確指示。
方式二:範圍語法 [ TO ] { TO }
2000-11 表示2000年11月整個月. 2000-11T13 表示200年11月天天的13點這一個小時 -0009 公元前10年,0000是公元前1年。 [2000-11-01 TO 2014-12-01] 日到日 [2014 TO 2014-12-01] 2014年開始到2014-12-01止. [* TO 2014-12-01] 2014-12-01(含)前.
時間數學表達式
Solr中還支持用 NOW +- 時間的數學表達式來靈活表示時間。語法 NOW +- 帶單位的時間數,/單位 截斷。可用來表示時間段。
NOW+2MONTHS NOW-1DAY NOW/HOUR NOW+6MONTHS+3DAYS/DAY 1972-05-20T17:33:18.772Z+6MONTHS+3DAYS/DAY
NOW在查詢中使用時,可爲NOW指定值
q=solr&fq=start_date:[* TO NOW]&NOW=1384387200000
EnumFieldType 枚舉字段類別說明
EnumFieldType 用於字段值是一個枚舉集,且排序順序可預約的狀況,如新聞分類這樣的字段。定義很是簡單:
<fieldType name="priorityLevel" class="solr.EnumFieldType" docValues="true" enumsConfig="enumsConfig.xml" enumName="priority"/>
enumsConfig:指定枚舉值的配置文件,絕對路徑或相對 內核conf/的相對路徑
enumName:指定配置文件的枚舉名。排序順序是按配置的順序。
docValues : 枚舉類型字段必須設置 true;
枚舉配置示例
<?xml version="1.0" ?> <enumsConfig> <enum name="priority"> <value>Not Available</value> <value>Low</value> <value>Medium</value> <value>High</value> <value>Urgent</value> </enum> <enum name="risk"> <value>Unknown</value> <value>Very Low</value> <value>Low</value> <value>Medium</value> <value>High</value> <value>Critical</value> </enum> </enumsConfig>
練習4 定義Field
prodId:商品id,字符串,不索引、存儲;
name: 商品名稱,字符串,分詞、索引、存儲
simpleIntro:商品簡介,字符串,分詞、索引、不存儲
price:價格,整數(單位分),索引,存儲
uptime:上架時間,索引、docValues 支持排序
<field name=「」 type=「」 default=「」 indexed="true" stored="true"/>
dynamic Field 動態字段
問:若是模式中有近百個字段須要定義,其中有不少字段的定義是相同,重複地定義是否是很煩?
可不能夠定一個規則,字段名以某前綴開頭或結尾的是相同的定義配置,那這些重複字段就只須要配置一個,保證提交的字段名稱遵照這個前綴、後綴便可。
這就是動態字段。
如:整型字段都是同樣的定義,則能夠定義一個動態字段以下:
<dynamicField name="*_i" type=「my_int" indexed="true" stored="true"/>
也能夠是前綴,如 name=「i_*」
uniqueKey 惟一鍵
指定用做惟一鍵的字段,非必需。
<uniqueKey>id</uniqueKey>
惟一鍵字段不能夠是保留字段、複製字段,且不能分詞。
Similarity 相關性計算類配置
若是默認的相關性計算模型BM25Similarity不知足你應用的特殊須要,你可在schema中指定全局的或字段類型局部的相關性計算類
<similarity class="solr.SchemaSimilarityFactory"> <str name="defaultSimFromFieldType">text_dfr</str> </similarity> <fieldType name="text_dfr" class="solr.TextField"> <analyzer ... /> <similarity class="solr.DFRSimilarityFactory"> <str name="basicModel">I(F)</str> <str name="afterEffect">B</str> <str name="normalization">H3</str> <float name="mu">900</float> </similarity> </fieldType>
小結
內容概覽
一、Schema操做API整體介紹
Solr中強烈推薦使用Schema API來管理集合/內核的模式信息,能夠讀、寫模式信息。經過API來更新模式信息,solr將自動重載內核。可是請注意:模式修改並不會自動重索引已索引的文檔,只會對後續的文檔起做用,若是必要,你須要手動重索引(刪除原來的,從新提交文檔)。
更新 Schema
發送 post請求到 /collection/schema ,以JSON格式提交數據,在json中說明你要進行的更新操做及對應的數據(一次請求可進行多個操做),操做的定義見下頁。
更新操做定義
add-field: 添加一個新字段. delete-field: 刪除一個字段. replace-field: 替換一個字段,修改. add-dynamic-field: 添加一個新動態字段. delete-dynamic-field: 刪除一個動態字段 replace-dynamic-field: 替換一個已存在的動態字段 add-field-type: 添加一個fieldType. delete-field-type: 刪除一個fieldType. replace-field-type: 更新一個存在的fieldType add-copy-field: 添加一個複製字段規則. delete-copy-field: 刪除一個複製字段規則.
二、V一、V2兩個版本API說明
V1老版本的api,V2新版本的API,當前兩個版本的API都支持,未來會統一到新版本。兩個版本的API只是請求地址上的區別,參數沒區別。
V1: http://localhost:8983/solr/gettingstarted/schema V2: http://localhost:8983/api/cores/gettingstarted/schema
三、FieldType字段類別操做
添加一個字段類別 add-field-type
一個Analyzer:
curl -X POST -H 'Content-type:application/json' --data-binary '{ "add-field-type" : { "name":"myNewTxtField", "class":"solr.TextField", "positionIncrementGap":"100", "analyzer" : { "tokenizer":{ "class":"solr.WhitespaceTokenizerFactory" }, "filters":[{ "class":"solr.WordDelimiterFilterFactory", "preserveOriginal":"0" }]}} }' http://localhost:8983/solr/gettingstarted/schema
兩個Analyzer:
curl -X POST -H 'Content-type:application/json' --data-binary '{ "add-field-type":{ "name":"myNewTextField", "class":"solr.TextField", "indexAnalyzer":{ "tokenizer":{ "class":"solr.PathHierarchyTokenizerFactory", "delimiter":"/" }}, "queryAnalyzer":{ "tokenizer":{ "class":"solr.KeywordTokenizerFactory" }}} }' http://localhost:8983/api/cores/gettingstarted/schema
練習:
一、添加示例;
二、添加一個FieldType myNewZhText,索引的分詞器用IKAnalyzer,查詢的分詞器用IKAnaylzer + 同義詞Filter
刪除一個字段類別 delete-field-type
curl -X POST -H 'Content-type:application/json' --data-binary '{ "delete-field-type":{ "name":"myNewTxtField" } }' http://localhost:8983/api/cores/gettingstarted/schema
替換一個字段類別 replace-field-type
curl -X POST -H 'Content-type:application/json' --data-binary '{ "replace-field-type":{ "name":"myNewTxtField", "class":"solr.TextField", "positionIncrementGap":"100", "analyzer":{ "tokenizer":{ "class":"solr.StandardTokenizerFactory" }}} }' http://localhost:8983/api/cores/gettingstarted/schema
四、Field 字段操做
添加一個字段 add-field
curl -X POST -H 'Content-type:application/json' --data-binary '{ "add-field":{ "name":"sell_by", "type":"pdate", "stored":true } }' http://localhost:8983/api/cores/gettingstarted/schema
刪除一個字段 delete-field
curl -X POST -H 'Content-type:application/json' --data-binary '{ "delete-field" : { "name":"sell_by" } }' http://localhost:8983/api/cores/gettingstarted/schema
替換一個字段 replace-field
curl -X POST -H 'Content-type:application/json' --data-binary '{ "replace-field":{ "name":"sell_by", "type":"date", "stored":false } }' http://localhost:8983/api/cores/gettingstarted/schema
練習:
添加以下商品字段
type:商品類別,字符串,多值,不分詞、索引,須要分面查詢(docValues)
brand: 品牌,字符串,單值,不分詞、索引,須要分面查詢(docValues)
msales:月銷量,整數,單值,索引、存儲,支持排序
修改以下商品字段
price:價格,整數(單位分),索引,存儲,支持排序
五、dynamicField 動態字段操做
添加一個動態字段 add-dynamic-field
curl -X POST -H 'Content-type:application/json' --data-binary '{ "add-dynamic-field":{ "name":"*_s", "type":"string", "stored":true } }' http://localhost:8983/api/cores/gettingstarted/schema
刪除一個動態字段 delete-dynamic-field
curl -X POST -H 'Content-type:application/json' --data-binary '{ "delete-dynamic-field":{ "name":"*_s" } }' http://localhost:8983/api/cores/gettingstarted/schema
替換一個動態字段 replace-dynamic-field
curl -X POST -H 'Content-type:application/json' --data-binary '{ "replace-dynamic-field":{ "name":"*_s", "type":"text_general", "stored":false } }' http://localhost:8983/solr/gettingstarted/schema
六、copyField 複製字段操做
添加複製字段 add-copy-field
curl -X POST -H 'Content-type:application/json' --data-binary '{ "add-copy-field":{ "source":"shelf", "dest":[ "location", "catchall" ]} }' http://localhost:8983/api/cores/gettingstarted/schema
刪除複製字段 delete-copy-field
curl -X POST -H 'Content-type:application/json' --data-binary '{ "delete-copy-field":{ "source":"shelf", "dest":"location" } }' http://localhost:8983/api/cores/gettingstarted/schema
一次請求多個操做示例-1
curl -X POST -H 'Content-type:application/json' --data-binary '{ "add-field-type":{ "name":"myNewTxtField", "class":"solr.TextField", "positionIncrementGap":"100", "analyzer":{"tokenizer":{ "class":"solr.WhitespaceTokenizerFactory" }, "filters":[{ "class":"solr.WordDelimiterFilterFactory", "preserveOriginal":"0" }]}}, "add-field" : { "name":"sell_by", "type":"myNewTxtField", "stored":true } }' http://localhost:8983/solr/gettingstarted/schema
一次請求多個操做示例-2
curl -X POST -H 'Content-type:application/json' --data-binary '{ "add-field":[ { "name":"shelf", "type":"myNewTxtField", "stored":true }, { "name":"location", "type":"myNewTxtField", "stored":true }] }' http://localhost:8983/solr/gettingstarted/schema
七、獲取schema信息
獲取整個schema
GET /collection/schema
能夠經過wt請求參數指定返回的格式:json,xml, schema.xml
http://localhost:8983/api/cores/mycore/schema?wt=xml
獲取字段
GET /collection/schema/fields GET /collection/schema/fields/fieldname
請求參數有:
wt: json/xml fl:指定須要返回的字段名,以逗號或空格間隔 showDefaults:true/false ,是否返回字段的默認屬性 includeDynamic:true/false,在path中帶有fieldname 或指定了 fl的狀況下才有用。
獲取動態字段
GET /collection/schema/dynamicfields GET /collection/schema/dynamicfields/name
可用請求參數:wt、showDefaults
http://localhost:8983/api/cores/mycore/schema/dynamicfields?wt=xml
獲取字段類別
GET /collection/schema/fieldtypes GET /collection/schema/fieldtypes/name
可用請求參數:wt、showDefaults
http://localhost:8983/api/cores/mycore/schema/fieldtypes?wt=xml
獲取複製字段
GET /collection/schema/copyfields
可用請求參數:wt、 source.fl、 dest.fl
獲取其餘信息
GET /collection/schema/name 獲取schema的name GET /collection/schema/version 獲取schema的版本 GET /collection/schema/uniquekey 獲取惟一鍵字段 GET /collection/schema/similarity 獲取全局相關性計算類
可用請求參數:wt