首页 > 解决方案 > Solr 中是否存在允许空值的数字多值字段类型?

问题描述

问题

有没有办法在 solr 中有一个允许空值并且是多值的数字字段类型?

例如"my_number_field": [1, 3, null, 4]

背景

我正在尝试将一些从 XML 展平到 JSON 中的数据索引,其中一个字段是数字(当前pdoubles)并且可以有空值。

    {
        ...,
        "sector_code": [
            "52010",
            "P13"
        ],
        "sector_vocabulary": [
            "",
            "99"
        ],
        "sector_percentage": [
            "",
            "100"
        ]
    }

managed-schema

<field name="sector_percentage" type="pdoubles" multiValued="true" indexed="true" required="false" stored="true"/>

<fieldType name="pdoubles" class="solr.DoublePointField" docValues="true" multiValued="true"/>

当我尝试将此索引到 Solr 中时,出现以下错误:

Error adding field 'sector_percentage'='[,100]' msg=empty String]

将值设置为 0 是不合适的,因为源数据中缺少值可能意味着基于上下文的不同事物。

标签: solrsolrcloud

解决方案


答案是使用SolrNaN中不存在的pdoubles值。

"my_number_field": [1, 3, "NaN", 4]

索引字段的示例查询响应:

{
  "responseHeader":{
    "zkConnected":true,
    "status":0,
    "QTime":0,
    "params":{
      "q":"id:zz1234",
      "indent":"true",
      "fl":"sector_percentage",
      "q.op":"OR",
      "_":"1635517103316"}},
  "response":{"numFound":1,"start":0,"numFoundExact":true,"docs":[
      {
        "sector_percentage":[50.0,
          "NaN",
          100.0]}]
  }}

推荐阅读