99re热视频这里只精品,久久久天堂国产精品女人,国产av一区二区三区,久久久精品成人免费看片,99久久精品免费看国产一区二区三区

Solr轉換和索引自定義JSON

2018-11-17 15:07 更新

如果您想要索引 JSON 文檔而不將其轉換為 Solr 的結構,則可以通過包含一些帶有更新請求的參數(shù)將它們添加到 Solr。這些參數(shù)提供了如何將單個 JSON 文件拆分為多個 Solr 文檔以及如何將字段映射到 Solr 的架構的信息。一個或多個有效的 JSON 文檔可以通過配置參數(shù)發(fā)送到 /update/json/docs 路徑。

映射參數(shù)

這些參數(shù)允許您定義如何為多個 Solr 文檔讀取 JSON 文件。

split

定義將輸入 JSON 拆分為多個 Solr 文檔的路徑,如果您在單個 JSON 文件中有多個文檔,則需要該路徑。如果整個 JSON 生成一個 solr 文檔,路徑必須是“/”??梢酝ㄟ^用管道(|)分隔它們來傳遞多個分割路徑,示例:split=/|/foo|/foo/bar。如果一個路徑是另一個路徑的子路徑,則它們自動成為子文檔

f

多值映射參數(shù)。參數(shù)的格式是target-field-name:json-path;json-path是必需的;target-field-name是 Solr 文檔字段名稱,并且是可選的。如果未指定,則從輸入 JSON 自動派生。默認的目標字段名稱是字段的完全限定名稱。

這里可以使用通配符,請參閱下面的使用通配符字段名稱獲取更多信息。

mapUniqueKeyOnly

(boolean 值)當輸入 JSON 中的字段在模式中不可用且無架構模式未啟用時,此參數(shù)特別方便。這會將所有字段編入默認搜索字段(使用下面的 df 參數(shù)),只將uniqueKey字段映射到模式中相應的字段。如果輸入的 JSON 沒有該uniqueKey字段的值,則會為此生成一個 UUID。

df

如果使用該mapUniqueKeyOnly標志,則更新處理程序需要一個數(shù)據(jù)應該被索引到的字段。這是其他處理程序用作默認搜索字段的相同字段。

srcField

這是 JSON 源將存儲到的字段的名稱。這只能用于split=/(即,您希望您的 JSON 輸入文件索引為一個單一的 Solr 文檔)。請注意,原子更新將導致該字段與文檔不同步。

echo

這僅用于調試目的。將其設置為true如果您希望將文檔作為響應返回。什么都不會被索引。

例如,如果我們有一個包含兩個文檔的 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",
  "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}
]
}'

有了這個要求,我們已經定義“exams”包含多個文件。另外,我們已經將輸入文檔中的幾個字段映射到 Solr 字段。

更新請求完成后,以下兩個文件將被添加到索引中:

{
  "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}
  ]
}'

在這個例子中,我們簡單地命名字段路徑(如 /exams/test)。Solr 將自動嘗試將字段的內容從 JSON 輸入添加到具有相同名稱的字段中的索引。

Note:如果索引之前的架構中不存在文檔,文檔將被拒絕。所以,如果您不使用無架構模式,請預先創(chuàng)建這些字段。如果您在無架構模式下工作,則不存在的字段將以 Solr 對字段類型的最佳猜測而創(chuàng)建。

對字段名使用通配符

可以指定通配符來自動映射字段, 而不是顯式指定所有字段名稱。

但是這有兩個限制:通配符只能在 json 路徑的末尾使用;分割路徑不能使用通配符。

一個星號 * 只映射到直接的子級,雙星號 \*\* 遞歸地映射到所有的后代。以下是通配符路徑映射示例:

  • f=$FQN:/**:將所有字段映射到 JSON 字段的完全限定名 ($FQN)。完全限定名是通過連接層次結構中的所有關鍵字(以句點(.))作為分隔符獲得的。如果未指定 f 路徑映射,則這是默認行為。
  • f=/docs/*:將文檔中的所有字段以 json 中給出的名稱映射。
  • f=/docs/**:將文檔中的所有字段及其子節(jié)點映射為 json 中給出的名稱。
  • f=searchField:/docs/* :將 /docs 下的所有字段映射到一個名為 “searchField” 的字段。
  • f=searchField:/docs/** :將 /docs 及其子項下的所有字段映射到 searchField。

使用通配符我們可以進一步簡化前面的例子,如下所示:

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}
  ]
}'

因為我們希望在 JSON 輸入中找到字段時使用字段名進行索引,所以雙重通配符 f=/** 將把所有字段及其后代映射到 Solr 中的相同字段。

也可以將所有值發(fā)送到單個字段,然后對其進行全文搜索。這是一個很好的選擇,可以盲目索引和查詢 JSON 文檔,而不必擔心字段和架構。

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}
  ]
}'

在上面的例子中,我們已經說過所有的字段都應該添加到名為 “txt” 的 Solr 中的一個字段中。這會將多個字段添加到單個字段,因此您選擇的任何字段都應該是多值的。

默認行為是使用節(jié)點的完全限定名稱(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)中的文檔,每行指定一個文檔。

例如:

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
    }
  ]
}'

在這個例子中,索引的文檔如下所示:

{
  "name":"Joe Smith",
  "phone":876876687,
  "_childDocuments_":[
    {
      "name":"Microsoft",
      "city":"Seattle",
      "zip":98052},
    {
      "name":"Apple",
      "city":"Cupertino",
      "zip":95014}]}

自定義 JSON 索引的技巧

  1. 無架構模式:自動處理字段創(chuàng)建。字段猜測可能不完全按照您的預期,但它的工作原理。最好的辦法是以無架構模式設置本地服務器,索引一些示例文檔,并在索引之前使用適當?shù)淖侄晤愋驮趯嶋H設置中創(chuàng)建這些字段。
  2. 預先創(chuàng)建的架構:將文檔發(fā)布到 /update/json/docs 端點,并且 echo=true。這將為您提供需要創(chuàng)建的字段名稱的列表。在實際編制索引之前創(chuàng)建這些字段
  3. 沒有架構,只有全文搜索:所有你需要做的就是在您的 JSON 上進行全文搜索。按照 “設置 JSON 默認值”部分中的設置配置。

可以將任何 json 發(fā)送到 /update/json/docs 端點,組件的默認配置如下:

<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ù)傳遞,整個 json 文件將被索引到該 _src_ 字段,并且輸入 JSON 中的所有值都將轉到名為 text 的字段。如果存在 uniqueKey 的值,則存儲該值,如果不能從輸入的 JSON 獲取值,則創(chuàng)建 UUID 并將其用作 uniqueKey 字段值。

或者,使用“請求參數(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"
 }}}'

使用每個請求發(fā)送參數(shù) useParams = full_txt。

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號