elasticsearch获取字段,即使这些字段为空或null

js4nwp54  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(2)|浏览(441)

我为一个示例数据集提供了以下elasticsearch术语过滤器。

"filter":{
   "type":"and",
   "and":[
      {
         "type":"terms",
         "terms":{
            "field":"car_registration_no.raw",
            "terms":[
               "61123",
               "61124",
               "61125"
            ]
         }
      }
   ]
}

下面是示例结果:

{
    "result": [
      {
        "totalHits": 3,
        "hits": [
          {
            "schemaid": "adef89sesdceasjkmn5dlky6djj7kk189lkdqsc",
            "localVersion": 2,
            "row": {
              "primaryKey": {
                "car_registration_no": "61123"
              },
              "columns": {
                "model": "Nissan",
                "submodel": "Saloon",
                "date_used": 1597017600000
              },
              "editsVersion": 0
            },
            "highlight": {}
          },
          {
            "schemaid": "adef89sesdceasjkmn5dlky6djj7kk189lkdqsc",
            "localVersion": 2,
            "row": {
              "primaryKey": {
                "car_registration_no": "61124"
              },
              "columns": {
                "model": "Nissan",
                "submodel": "Saloon",
                "accidents": "1",
                "accident_date": "12/12/2019",
                "date_used": 1597017600000
              },
              "editsVersion": 0
            },
            "highlight": {}
          },
          {
            "schemaid": "adef89sesdceasjkmn5dlky6djj7kk189lkdqsc",
            "localVersion": 2,
            "row": {
              "primaryKey": {
                "car_registration_no": "61125"
              },
              "columns": {
                "model": "Nissan",
                "submodel": "Saloon",
                "date_used": 1597017600000
              },
              "editsVersion": 0
            },
            "highlight": {}
          }
        ],
        "counts": {},
        "nextToken": null
      }
    ]
  }

只有一辆车(61124号车)有事故记录,我如何为其他车检索空值或空值?
我曾经提到过elasticsearch,但由于我是一个初学者,我不知道如何处理它。因此,预期的结果是,如果没有值,我希望看到空白字段“事故”和“事故日期”显示为null或空白,在上面的示例中,车辆注册号61123和61125具有null值,因此我希望检索所有字段,而不考虑空值。

{
    "result": [
      {
        "totalHits": 3,
        "hits": [
          {
            "schemaid": "adef89sesdceasjkmn5dlky6djj7kk189lkdqsc",
            "localVersion": 2,
            "row": {
              "primaryKey": {
                "car_registration_no": "61123"
              },
              "columns": {
                "model": "Nissan",
                "submodel": "Saloon",
                "accidents": "null",
                "accident_date": "null",
                "date_used": 1597017600000
              },
              "editsVersion": 0
            },
            "highlight": {}
          },
          {
            "schemaid": "adef89sesdceasjkmn5dlky6djj7kk189lkdqsc",
            "localVersion": 2,
            "row": {
              "primaryKey": {
                "car_registration_no": "61124"
              },
              "columns": {
                "model": "Nissan",
                "submodel": "Saloon",
                "accidents": "1",
                "accident_date": "12/12/2019",
                "date_used": 1597017600000
              },
              "editsVersion": 0
            },
            "highlight": {}
          },
          {
            "schemaid": "adef89sesdceasjkmn5dlky6djj7kk189lkdqsc",
            "localVersion": 2,
            "row": {
              "primaryKey": {
                "car_registration_no": "61125"
              },
              "columns": {
                "model": "Nissan",
                "submodel": "Saloon",
                "accidents": "null",
                "accident_date": "null",
                "date_used": 1597017600000
              },
              "editsVersion": 0
            },
            "highlight": {}
          }
        ],
        "counts": {},
        "nextToken": null
      }
    ]
  }
ljsrvy3e

ljsrvy3e1#

你想要的是能够看到 NULL 文档的值,这在默认情况下是不可能的 NULL 数值不可索引和搜索,更多信息请参考官方的空值文档
无法索引或搜索空值。当一个字段设置为null(或空数组或空值数组)时,它被视为该字段没有值。
但是为了实现您的用例,您需要提供 null_value 在Map和索引文档时,如果没有收到值,则传递 null_value 文件中的param值,如下面的完整示例所示
索引Map

{
    "mappings": {
        "properties": {
            "accidents": {
                "type": "keyword",
                "null_value": "NULL" --> note this
            },
            "accidents_date": {
                "type": "keyword",
                "null_value": "NULL"
            },
            "car_registration_no" :{
                "type" : "keyword"
            }
        }
    }
}

索引示例文档

{
    "accidents" : "1",
    "accidents_date": "12/12/2019",
    "car_registration_no" : "61124"
}
{
    "accidents" : "null",
    "accidents_date": "null",
    "car_registration_no" : "61125"
}
{
    "accidents" : "null",
    "accidents_date": "null",
    "car_registration_no" : "61123"
}

搜索查询

{
    "query": {
        "terms": {
            "car_registration_no": [
                "61124",
                "61123",
                "61125"
            ],
            "boost": 1.0
        }
    }
}

和预期的搜索结果

"hits": [
            {
                "_index": "carindex",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "accidents": "null", --> note this
                    "accidents_date": "NULL",
                    "car_registration_no": "61123"
                }
            },
            {
                "_index": "carindex",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "accidents": "1",
                    "accidents_date": "12/12/2019",
                    "car_registration_no": "61124"
                }
            },
            {
                "_index": "carindex",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "car_registration_no": "61125"
                }
            }
        ]
d4so4syb

d4so4syb2#

可以使用术语查询返回在提供的字段中包含一个或多个精确术语的文档。
添加带有搜索查询和搜索结果的工作示例(使用与问题中提供的相同的示例索引数据)
搜索查询:(版本7.*)

{
  "query": {
    "terms": {
      "row.primaryKey.car_registration_no": [ "61125","61123", "61124" ]
    }
  }
}

搜索结果:

{
  "took": 16,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "stof_64398592",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "schemaid": "adef89sesdceasjkmn5dlky6djj7kk189lkdqsc",
          "localVersion": 2,
          "row": {
            "primaryKey": {
              "car_registration_no": "61123"
            },
            "columns": {
              "model": "Nissan",
              "submodel": "Saloon",
              "accidents": "null",
              "accident_date": "null",         <-- note this
              "date_used": 1597017600000
            },
            "editsVersion": 0
          },
          "highlight": {}
        }
      },
      {
        "_index": "stof_64398592",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "schemaid": "adef89sesdceasjkmn5dlky6djj7kk189lkdqsc",
          "localVersion": 2,
          "row": {
            "primaryKey": {
              "car_registration_no": "61124"
            },
            "columns": {
              "model": "Nissan",
              "submodel": "Saloon",
              "accidents": "1",
              "accident_date": "12/12/2019",
              "date_used": 1597017600000            <-- note this
            },
            "editsVersion": 0
          },
          "highlight": {}
        }
      },
      {
        "_index": "stof_64398592",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.0,
        "_source": {
          "schemaid": "adef89sesdceasjkmn5dlky6djj7kk189lkdqsc",
          "localVersion": 2,
          "row": {
            "primaryKey": {
              "car_registration_no": "61125"
            },
            "columns": {
              "model": "Nissan",
              "submodel": "Saloon",
              "accidents": "null",
              "accident_date": "null",           <-- note this
              "date_used": 1597017600000
            },
            "editsVersion": 0
          },
          "highlight": {}
        }
      }
    ]
  }
}

如果您使用的是版本5.*,那么请阅读有关术语查询的文档,了解其语法。

相关问题