Solr索引和基本数据操作
轉自:https://blog.csdn.net/lzx1104/article/details/51460987
1. 介紹
Solr索引可以接收不同的數(shù)據(jù)來源,包括XML文件,逗號分隔值(CSV)文件,從數(shù)據(jù)庫提取的數(shù)據(jù),常見的文件格式如MS Word或PDF.
有三種常用的方法加載數(shù)據(jù)到Solr索引:
- 使用Apache Tika的Solr Cell框架,處理二進制或結構化文件如Office, Word, PDF 和其他專有格式。
- 通過HTTP請求上傳XML文件
- 使用SolrJ寫一個Java應用程序。這可能是最好的選擇。
2. Post工具
2.1 索引XML
$ bin/post -h
$ bin/post -c gettingstarted *.xml
$ bin/post -c gettingstarted -p 8984 *.xml
$ bin/post -c gettingstarted -d ‘42’
2.2 索引CSV
$ bin/post -c gettingstarted *.csv
索引tab分隔(tab-separated)文件
$ bin/post -c signals -params “separator=%09” -type text/csv data.tsv
2.3 索引JSON
$ bin/post -c gettingstarted *.json
2.4 索引富文件
$ bin/post -c gettingstarted a.pdf
$ bin/post -c gettingstarted afolder/
$ bin/post -c gettingstarted -filetypes ppt,html afolder/
3. 使用Index Handlers上傳數(shù)據(jù)
Index Handlers是用來添加、刪除和更新索引文檔的請求處理器。
除了使用Tika插件導入富文檔,或使用Data Import Handler導入結構化數(shù)據(jù),Solr原生支持索引XML,CSV,JSON文檔。
3.1 XML格式索引更新
Content-type: application/xml or Content-type: text/xml
(1) 添加文檔
Patrick Eagar
Sports
12.40
Summer of the all-rounder: Test and championship cricket in England 1982
…
(2) XML更新命令
- Commit 和 Optimize
操作將上次commit至今提交的文檔寫入磁盤。commit前,新索引的文檔對Searcher不可見。
Commit操作可以被顯示的提交一個消息,也可以由solrconfig.xml中的參數(shù)觸發(fā)。
參數(shù):
waitSearcher
expungeDeletes
操作請求Solr合并內(nèi)部數(shù)據(jù),以獲得更好的搜索效果。對于大的搜索需要花費一些時間。
參數(shù):
waitSearcher
maxSegments
- 刪除(Delete)操作
兩種方式:“Delete by ID” (UniqueID) 或 “Delete by Query”
可包含多個刪除操作:
0002166313
0031745983
subject:sport
publisher:penguin
-
回滾(Rollback)操作
回滾上次commit后的添加和刪除操作。
-
使用curl命令執(zhí)行更新
curl http://localhost:8983/solr/my_collection/update -H “Content-Type: text/xml”
–data-binary ’
Patrick Eagar
Sports
796.35
0002166313
1982
Collins
’
curl http://localhost:8983/solr/my_collection/update -H “Content-Type: text/xml”
–data-binary @myfile.xml
curl http://localhost:8983/solr/my_collection/update?stream.body=<commit/>
(3) Using XSLT to Transform XML Index Updates
3.2 JSON 格式索引更新
Content-Type: application/json 或 Content-Type: text/json
- 添加文檔
curl -X POST -H ‘Content-Type: application/json’
‘http://localhost:8983/solr/my_collection/update/json/docs’ --data-binary ’
{
“id”: “1”,
“title”: “Doc 1”
}’
curl -X POST -H ‘Content-Type: application/json’
‘http://localhost:8983/solr/my_collection/update’ --data-binary ’
[
{
“id”: “1”,
“title”: “Doc 1”
},
{
“id”: “2”,
“title”: “Doc 2”
}
]’
curl ‘http://localhost:8983/solr/techproducts/update?commit=true’ --data-binary
@example/exampledocs/books.json -H ‘Content-type:application/json’
- 發(fā)送命令
curl -X POST -H ‘Content-Type: application/json’
‘http://localhost:8983/solr/my_collection/update’ --data-binary ’
{
“add”: {
“doc”: {
“id”: “DOC1”,
“my_boosted_field”: { /* use a map with boost/value for a boosted field /
“boost”: 2.3,
“value”: “test”
},
“my_multivalued_field”: [ “aaa”, “bbb” ] / Can use an array for a multi-valued field /
}
},
“add”: {
“commitWithin”: 5000, / commit this document within 5 seconds /
“overwrite”: false, / don’t check for existing documents with the same uniqueKey /
“boost”: 3.45, / a document boost /
“doc”: {
“f1”: “v1”, / Can use repeated keys for a multi-valued field /
“f1”: “v2”
}
},
“commit”: {},
“optimize”: { “waitSearcher”:false },
“delete”: { “id”:“ID” }, / delete by ID /
“delete”: { “query”:“QUERY” } / delete by query */
}’
簡單的delete-by-id:
{ “delete”:“myid” }
{ “delete”:[“id1”,“id2”] }
{
“delete”:“id”:50,
“version”:12345
}
便捷請求路徑:
/update/json
/update/json/docs
- 轉換和索引自定義JSON
curl ‘http://localhost:8983/solr/my_collection/update/json/docs’
‘?split=/exams’
‘&f=first:/first’
‘&f=last:/last’
‘&f=grade:/grade’
‘&f=subject:/exams/subject’
‘&f=test:/exams/test’
‘&f=marks:/exams/marks’
-H ‘Content-type:application/json’ -d ’
{
“first”: “John”,
“l(fā)ast”: “Doe”,
“grade”: 8,
“exams”: [
{
“subject”: “Maths”,
“test” : “term1”,
“marks” : 90},
{
“subject”: “Biology”,
“test” : “term1”,
“marks” : 86}
]
}’
3.3 CSV格式索引更新
curl ‘http://localhost:8983/solr/techproducts/update?commit=true’ --data-binary
@example/exampledocs/books.csv -H ‘Content-type:application/csv’
curl ‘http://localhost:8983/solr/update/csv?commit=true&separator= &escape=\’
–data-binary @/tmp/result.txt
4. 使用Apache Tika的Solr Cell上傳數(shù)據(jù)
ExtractingRequestHandler可以使用Tika來支持上傳二進制文件,如Word,PDF, 用于數(shù)據(jù)抽取和索引。
curl
‘http://localhost:8983/solr/techproducts/update/extract?literal.id=doc1&commit=true’
-F “myfile=@example/exampledocs/solr-word.pdf”
- 配置ExtractingRequestHandler
需要配置solrconfig.xml包含相關Jar:
然后在solrconfig.xml配置:
last_modified
ignored_
/my/path/to/tika.config
yyyy-MM-dd5. 使用Data Import Handler上傳結構化存儲數(shù)據(jù)
配置solrconfig.xml:
/path/to/my/DIHconfigfile.xml
配置DIHconfigfile.xml:
參考:example/example-DIH/solr/db/conf/db-data-config.xml
6. 部分更新文檔
對于只有部分改變的文檔,Solr支持兩種方法更新進行更新:
- atomic updates, 允許只修改一個或多個字段,而不需要重新索引整個文檔。
- optimistic concurrency 或 optimistic locking, 這是很多NoSQL數(shù)據(jù)庫的特性,允許基于版本有條件的更新。
6.1 Atomic Updates
set
add
remove
removeregex
inc
已存在文檔:
{
“id”:“mydoc”,
“price”:10,
“popularity”:42,
“categories”:[“kids”],
“promo_ids”:[“a123x”],
“tags”:[“free_to_try”,“buy_now”,“clearance”,“on_sale”]
}
應用更新命令:
{
“id”:“mydoc”,
“price”:{“set”:99},
“popularity”:{“inc”:20},
“categories”:{“add”:[“toys”,“games”]},
“promo_ids”:{“remove”:“a123x”},
“tags”:{“remove”:[“free_to_try”,“on_sale”]}
}
更新后文檔:
{
“id”:“mydoc”,
“price”:99,
“popularity”:62,
“categories”:[“kids”,“toys”,“games”],
“tags”:[“buy_now”,“clearance”]
}
6.2 Optimistic Concurrency
Optimistic Concurrency是solr的一個特性,用于客戶端程序來確定其正在更新的文檔沒有同時被其他客戶端修改。
此功能需要每個索引文檔有一個_version_字段,并且與更新命令中指定的_version_比較。Solr的schema.xml默認有_version_字段。
通常Optimistic Concurrency的工作流如下:
(1) client讀取一個文檔。/get 確保有最近的版本。
(2) client在本地修改文檔。
(3) client向solr提交修改的文檔,/update
(4) 如果版本沖突(HTTP error 409), client重復處理步驟。
更新規(guī)則:
- 如果_version_值大于1,那么文檔中的_version_必須匹配索引中的_version_.
- 如果_version_值等于1,那么文檔必須存在,且不會進行版本匹配;如果文檔不存在,更新會被拒絕。
- 如果_version_值小于0,那么文檔必須不存在;如果文檔存在,更新會被拒絕。
- 如果_version_值等于0,那么不管文檔是否存在,版本是否匹配。如果文檔存在,將被覆蓋;不存在,將被添加。
$ curl -X POST -H ‘Content-Type: application/json’
‘http://localhost:8983/solr/techproducts/update?versions=true’ --data-binary ’
[ { “id” : “aaa” },
{ “id” : “bbb” } ]’
{“responseHeader”:{“status”:0,“QTime”:6},
“adds”:[“aaa”,1498562471222312960,
“bbb”,1498562471225458688]}
$ curl -X POST -H ‘Content-Type: application/json’
‘http://localhost:8983/solr/techproducts/update?version=999999&versions=true’
–data-binary ’
[{ “id” : “aaa”,
“foo_s” : “update attempt with wrong existing version” }]’
{“responseHeader”:{“status”:409,“QTime”:3},
“error”:{“msg”:“version conflict for aaa expected=999999
actual=1498562471222312960”,
“code”:409}}
$ curl -X POST -H ‘Content-Type: application/json’
‘http://localhost:8983/solr/techproducts/update?version=1498562471222312960&versio
ns=true&commit=true’ --data-binary ’
[{ “id” : “aaa”,
“foo_s” : “update attempt with correct existing version” }]’
{“responseHeader”:{“status”:0,“QTime”:5},
“adds”:[“aaa”,1498562624496861184]}
$ curl ‘http://localhost:8983/solr/techproducts/query?q=:&fl=id,version’
{
“responseHeader”:{
“status”:0,
“QTime”:5,
“params”:{
“fl”:“id,version”,
“q”:":"}},
“response”:{“numFound”:2,“start”:0,“docs”:[
{
“id”:“bbb”,
“version”:1498562471225458688},
{
“id”:“aaa”,
“version”:1498562624496861184}]
}}
7. 刪除重復數(shù)據(jù)(De-duplication)
Solr通過類原生支持去重技術,并且容易添加新的hash/signature實現(xiàn)。
一個Signature可以通過幾種方式實現(xiàn):
MD5Signature 128 bit hash
Lookup3Signature 64 bit hash
TextProfileSignature Fuzzy hashing from nutch
配置:
- solrconfig.xml
true
id
false
name,features,cat
solr.processor.Lookup3Signature
- schema.xml
8. 索引時語言檢測
使用langid UpdateRequestProcessor可以在索引時檢測語言并映射文本到語言相關的字段。Solr支持兩種實現(xiàn):
- Tika’s language detection feature: http://tika.apache.org/0.10/detection.html
- LangDetect language detection: http://code.google.com/p/language-detection/
9. Content Stream
基于URL地址訪問SolrRequestHandlers時,包含請求參數(shù)的SolrQueryRequest對象,也可以包含一個ContentStreams列表, 含有用于請求的bulk data.
10. 整合UIMA
Apache Unstructured Information Management Architecture (UIMA)
總結
以上是生活随笔為你收集整理的Solr索引和基本数据操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海参价格大概多少钱一斤?
- 下一篇: 个性签名心情说说短句