W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
如果您想要索引 JSON 文檔而不將其轉(zhuǎn)換為 Solr 的結(jié)構(gòu),則可以通過包含一些帶有更新請求的參數(shù)將它們添加到 Solr。這些參數(shù)提供了如何將單個(gè) JSON 文件拆分為多個(gè) Solr 文檔以及如何將字段映射到 Solr 的架構(gòu)的信息。一個(gè)或多個(gè)有效的 JSON 文檔可以通過配置參數(shù)發(fā)送到 /update/json/docs 路徑。
這些參數(shù)允許您定義如何為多個(gè) Solr 文檔讀取 JSON 文件。
定義將輸入 JSON 拆分為多個(gè) Solr 文檔的路徑,如果您在單個(gè) JSON 文件中有多個(gè)文檔,則需要該路徑。如果整個(gè) JSON 生成一個(gè) solr 文檔,路徑必須是“/
”??梢酝ㄟ^用管道(|)
分隔它們來傳遞多個(gè)分割路徑,示例:split=/|/foo|/foo/bar
。如果一個(gè)路徑是另一個(gè)路徑的子路徑,則它們自動成為子文檔
多值映射參數(shù)。參數(shù)的格式是target-field-name:json-path;
json-path
是必需的;target-field-name
是
Solr 文檔字段名稱,并且是可選的。如果未指定,則從輸入 JSON 自動派生。默認(rèn)的目標(biāo)字段名稱是字段的完全限定名稱。
這里可以使用通配符,請參閱下面的使用通配符字段名稱獲取更多信息。
(boolean 值)當(dāng)輸入 JSON 中的字段在模式中不可用且無架構(gòu)模式未啟用時(shí),此參數(shù)特別方便。這會將所有字段編入默認(rèn)搜索字段(使用下面的 df 參數(shù)),只將uniqueKey
字段映射到模式中相應(yīng)的字段。如果輸入的 JSON 沒有該uniqueKey
字段的值,則會為此生成一個(gè) UUID。
如果使用該mapUniqueKeyOnly
標(biāo)志,則更新處理程序需要一個(gè)數(shù)據(jù)應(yīng)該被索引到的字段。這是其他處理程序用作默認(rèn)搜索字段的相同字段。
這是 JSON 源將存儲到的字段的名稱。這只能用于split=/
(即,您希望您的 JSON 輸入文件索引為一個(gè)單一的 Solr 文檔)。請注意,原子更新將導(dǎo)致該字段與文檔不同步。
這僅用于調(diào)試目的。將其設(shè)置為true
如果您希望將文檔作為響應(yīng)返回。什么都不會被索引。
例如,如果我們有一個(gè)包含兩個(gè)文檔的 JSON 文件,我們可以像這樣定義一個(gè)更新請求:
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",
"last": "Doe",
"grade": 8,
"exams": [
{
"subject": "Maths",
"test" : "term1",
"marks" : 90},
{
"subject": "Biology",
"test" : "term1",
"marks" : 86}
]
}'
您可以通過使用請求參數(shù)來存儲和重用參數(shù)。
curl http://localhost:8983/solr/my_collection/config/params -H 'Content-type:application/json' -d '{
"set": {
"my_params": {
"split": "/exams",
"f": ["first:/first","last:/last","grade:/grade","subject:/exams/subject","test:/exams/test"]
}}}'
并使用它,如下所示:
curl 'http://localhost:8983/solr/my_collection/update/json/docs?useParams=my_params' -H 'Content-type:application/json' -d '{
"first": "John",
"last": "Doe",
"grade": 8,
"exams": [
{
"subject": "Maths",
"test" : "term1",
"marks" : 90},
{
"subject": "Biology",
"test" : "term1",
"marks" : 86}
]
}'
有了這個(gè)要求,我們已經(jīng)定義“exams”包含多個(gè)文件。另外,我們已經(jīng)將輸入文檔中的幾個(gè)字段映射到 Solr 字段。
更新請求完成后,以下兩個(gè)文件將被添加到索引中:
{
"first":"John",
"last":"Doe",
"marks":90,
"test":"term1",
"subject":"Maths",
"grade":8
}
{
"first":"John",
"last":"Doe",
"marks":86,
"test":"term1",
"subject":"Biology",
"grade":8
}
在之前的例子中,我們想要在 Solr 中使用的所有字段與在輸入 JSON 中使用的名稱相同。如果是這種情況,我們可以簡化請求,如下所示:
curl 'http://localhost:8983/solr/my_collection/update/json/docs'\
'?split=/exams'\
'&f=/first'\
'&f=/last'\
'&f=/grade'\
'&f=/exams/subject'\
'&f=/exams/test'\
'&f=/exams/marks'\
-H 'Content-type:application/json' -d '
{
"first": "John",
"last": "Doe",
"grade": 8,
"exams": [
{
"subject": "Maths",
"test" : "term1",
"marks" : 90},
{
"subject": "Biology",
"test" : "term1",
"marks" : 86}
]
}'
在這個(gè)例子中,我們簡單地命名字段路徑(如 /exams/test)。Solr 將自動嘗試將字段的內(nèi)容從 JSON 輸入添加到具有相同名稱的字段中的索引。
Note:如果索引之前的架構(gòu)中不存在文檔,文檔將被拒絕。所以,如果您不使用無架構(gòu)模式,請預(yù)先創(chuàng)建這些字段。如果您在無架構(gòu)模式下工作,則不存在的字段將以 Solr 對字段類型的最佳猜測而創(chuàng)建。
可以指定通配符來自動映射字段, 而不是顯式指定所有字段名稱。
但是這有兩個(gè)限制:通配符只能在 json 路徑的末尾使用;分割路徑不能使用通配符。
一個(gè)星號 * 只映射到直接的子級,雙星號 \*\* 遞歸地映射到所有的后代。以下是通配符路徑映射示例:
使用通配符我們可以進(jìn)一步簡化前面的例子,如下所示:
curl 'http://localhost:8983/solr/my_collection/update/json/docs'\
'?split=/exams'\
'&f=/**'\
-H 'Content-type:application/json' -d '
{
"first": "John",
"last": "Doe",
"grade": 8,
"exams": [
{
"subject": "Maths",
"test" : "term1",
"marks" : 90},
{
"subject": "Biology",
"test" : "term1",
"marks" : 86}
]
}'
因?yàn)槲覀兿M?JSON 輸入中找到字段時(shí)使用字段名進(jìn)行索引,所以雙重通配符 f=/** 將把所有字段及其后代映射到 Solr 中的相同字段。
也可以將所有值發(fā)送到單個(gè)字段,然后對其進(jìn)行全文搜索。這是一個(gè)很好的選擇,可以盲目索引和查詢 JSON 文檔,而不必?fù)?dān)心字段和架構(gòu)。
curl 'http://localhost:8983/solr/my_collection/update/json/docs'\
'?split=/'\
'&f=txt:/**'\
-H 'Content-type:application/json' -d '
{
"first": "John",
"last": "Doe",
"grade": 8,
"exams": [
{
"subject": "Maths",
"test" : "term1",
"marks" : 90},
{
"subject": "Biology",
"test" : "term1",
"marks" : 86}
]
}'
在上面的例子中,我們已經(jīng)說過所有的字段都應(yīng)該添加到名為 “txt” 的 Solr 中的一個(gè)字段中。這會將多個(gè)字段添加到單個(gè)字段,因此您選擇的任何字段都應(yīng)該是多值的。
默認(rèn)行為是使用節(jié)點(diǎn)的完全限定名稱(FQN)。所以,如果我們不定義任何字段映射,像這樣:
curl 'http://localhost:8983/solr/my_collection/update/json/docs?split=/exams'\
-H 'Content-type:application/json' -d '
{
"first": "John",
"last": "Doe",
"grade": 8,
"exams": [
{
"subject": "Maths",
"test" : "term1",
"marks" : 90},
{
"subject": "Biology",
"test" : "term1",
"marks" : 86}
]
}'
索引文檔將被添加到索引中,其字段如下所示:
{
"first":"John",
"last":"Doe",
"grade":8,
"exams.subject":"Maths",
"exams.test":"term1",
"exams.marks":90},
{
"first":"John",
"last":"Doe",
"grade":8,
"exams.subject":"Biology",
"exams.test":"term1",
"exams.marks":86}
此功能支持 JSON 行格式(.jsonl)中的文檔,每行指定一個(gè)文檔。
例如:
curl 'http://localhost:8983/solr/my_collection/update/json/docs' -H 'Content-type:application/json' -d '
{ "first":"Steve", "last":"Jobs", "grade":1, "subject": "Social Science", "test" : "term1", "marks" : 90}
{ "first":"Steve", "last":"Woz", "grade":1, "subject": "Political Science", "test" : "term1", "marks" : 86}'
甚至還有一些文檔,如下例所示:
curl 'http://localhost:8983/solr/my_collection/update/json/docs' -H 'Content-type:application/json' -d '[
{ "first":"Steve", "last":"Jobs", "grade":1, "subject": "Computer Science", "test" : "term1", "marks" : 90},
{ "first":"Steve", "last":"Woz", "grade":1, "subject": "Calculus", "test" : "term1", "marks" : 86}]'
以下是索引嵌套文檔的示例:
curl 'http://localhost:8983/solr/my_collection/update/json/docs?split=/|/orgs'\
-H 'Content-type:application/json' -d '{
"name": "Joe Smith",
"phone": 876876687,
"orgs": [
{
"name": "Microsoft",
"city": "Seattle",
"zip": 98052
},
{
"name": "Apple",
"city": "Cupertino",
"zip": 95014
}
]
}'
在這個(gè)例子中,索引的文檔如下所示:
{
"name":"Joe Smith",
"phone":876876687,
"_childDocuments_":[
{
"name":"Microsoft",
"city":"Seattle",
"zip":98052},
{
"name":"Apple",
"city":"Cupertino",
"zip":95014}]}
可以將任何 json 發(fā)送到 /update/json/docs 端點(diǎn),組件的默認(rèn)配置如下:
<initParams path="/update/json/docs">
<lst name="defaults">
<!-- this ensures that the entire json doc will be stored verbatim into one field -->
<str name="srcField">_src_</str>
<!-- This means a the uniqueKeyField will be extracted from the fields and
all fields go into the 'df' field. In this config df is already configured to be 'text'
-->
<str name="mapUniqueKeyOnly">true</str>
<!-- The default search field where all the values are indexed to -->
<str name="df">text</str>
</lst>
</initParams>
所以,如果沒有參數(shù)傳遞,整個(gè) json 文件將被索引到該 _src_ 字段,并且輸入 JSON 中的所有值都將轉(zhuǎn)到名為 text 的字段。如果存在 uniqueKey 的值,則存儲該值,如果不能從輸入的 JSON 獲取值,則創(chuàng)建 UUID 并將其用作 uniqueKey 字段值。
或者,使用“請求參數(shù)”功能設(shè)置這些參數(shù):
curl http://localhost:8983/solr/my_collection/config/params -H 'Content-type:application/json' -d '{
"set": {
"full_txt": {
"srcField": "_src_",
"mapUniqueKeyOnly" : true,
"df": "text"
}}}'
使用每個(gè)請求發(fā)送參數(shù) useParams = full_txt。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: