日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Solr索引和基本数据操作

發(fā)布時(shí)間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Solr索引和基本数据操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)自: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框架,處理二進(jìn)制或結(jié)構(gòu)化文件如Office, Word, PDF 和其他專有格式。
  • 通過HTTP請求上傳XML文件
  • 使用SolrJ寫一個(gè)Java應(yīng)用程序。這可能是最好的選擇。

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插件導(dǎo)入富文檔,或使用Data Import Handler導(dǎo)入結(jié)構(gòu)化數(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操作可以被顯示的提交一個(gè)消息,也可以由solrconfig.xml中的參數(shù)觸發(fā)。
    參數(shù):
    waitSearcher
    expungeDeletes

操作請求Solr合并內(nèi)部數(shù)據(jù),以獲得更好的搜索效果。對于大的搜索需要花費(fèi)一些時(shí)間。
參數(shù):
waitSearcher
maxSegments

  • 刪除(Delete)操作
    兩種方式:“Delete by ID” (UniqueID) 或 “Delete by Query”

可包含多個(gè)刪除操作:

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

  • 轉(zhuǎn)換和索引自定義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來支持上傳二進(jìn)制文件,如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包含相關(guān)Jar:

然后在solrconfig.xml配置:


last_modified
ignored_

/my/path/to/tika.config

yyyy-MM-dd

5. 使用Data Import Handler上傳結(jié)構(gòu)化存儲數(shù)據(jù)

配置solrconfig.xml:


/path/to/my/DIHconfigfile.xml

配置DIHconfigfile.xml:
參考:example/example-DIH/solr/db/conf/db-data-config.xml

6. 部分更新文檔

對于只有部分改變的文檔,Solr支持兩種方法更新進(jìn)行更新:

  • atomic updates, 允許只修改一個(gè)或多個(gè)字段,而不需要重新索引整個(gè)文檔。
  • 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”]
}

應(yīng)用更新命令:
{
“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的一個(gè)特性,用于客戶端程序來確定其正在更新的文檔沒有同時(shí)被其他客戶端修改。
此功能需要每個(gè)索引文檔有一個(gè)_version_字段,并且與更新命令中指定的_version_比較。Solr的schema.xml默認(rèn)有_version_字段。

通常Optimistic Concurrency的工作流如下:
(1) client讀取一個(gè)文檔。/get 確保有最近的版本。
(2) client在本地修改文檔。
(3) client向solr提交修改的文檔,/update
(4) 如果版本沖突(HTTP error 409), client重復(fù)處理步驟。

更新規(guī)則:

  • 如果_version_值大于1,那么文檔中的_version_必須匹配索引中的_version_.
  • 如果_version_值等于1,那么文檔必須存在,且不會(huì)進(jìn)行版本匹配;如果文檔不存在,更新會(huì)被拒絕。
  • 如果_version_值小于0,那么文檔必須不存在;如果文檔存在,更新會(huì)被拒絕。
  • 如果_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. 刪除重復(fù)數(shù)據(jù)(De-duplication)

Solr通過類原生支持去重技術(shù),并且容易添加新的hash/signature實(shí)現(xiàn)。
一個(gè)Signature可以通過幾種方式實(shí)現(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



dedupe ...
  • schema.xml

8. 索引時(shí)語言檢測

使用langid UpdateRequestProcessor可以在索引時(shí)檢測語言并映射文本到語言相關(guān)的字段。Solr支持兩種實(shí)現(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/
title,subject,text,keywords language_s title,subject,text,keywords language_s

9. Content Stream

基于URL地址訪問SolrRequestHandlers時(shí),包含請求參數(shù)的SolrQueryRequest對象,也可以包含一個(gè)ContentStreams列表, 含有用于請求的bulk data.

10. 整合UIMA

Apache Unstructured Information Management Architecture (UIMA)

總結(jié)

以上是生活随笔為你收集整理的Solr索引和基本数据操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。