ElasticSearch 获取字段,即使它们为空或 null

ElasticSearch get fields even if they are empty or null

我有以下用于示例数据集的 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 但由于我是这方面的初学者,所以我无法理解如何处理它。 所以预期的结果是我希望看到空字段 accidentsaccident_date 显示为 null 或空白,如果它们有没有值,在上面的例子中,car_registration 没有 61123 和 61125 有空值,因此我想检索所有字段,而不考虑空白值。

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

You can use terms query that returns documents that contain one or more exact terms in a provided field.

添加带有搜索查询和搜索结果的工作示例(使用问题中提供的相同示例索引数据)

搜索查询:(版本 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.*,请阅读 terms query 上的文档以了解其语法。

您想要的是能够查看文档的 NULL 值,这在默认情况下是不可能的,因为 NULL 值未编入索引且不可搜索,请参阅 official NULL_VALUES doc 了解更多信息

A null value cannot be indexed or searched. When a field is set to null, (or an empty array or an array of null values) it is treated as though that field has no values.

但是要实现您的 use-case,您需要在映射中提供 null_value 参数,并且在索引文档时,如果没有收到任何值,则传递 null_value 参数值在您的文档中,如以下完整示例所示

索引映射

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