【问题标题】:ElasticSearch get fields even if they are empty or nullElasticSearch 获取字段,即使它们为空或 null
【发布时间】:2020-10-17 11:27:22
【问题描述】:

我有以下用于示例数据集的弹性搜索术语过滤器。

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

【问题讨论】:

  • 您的用例不是很清楚。你能解释一下你的预期结果吗?
  • @Bhavya,我已经更新了我的问题以反映我希望如何查看数据,我想查看具有空值的字段以及检索每个 primaryKey 的所有字段的结果
  • 请仔细阅读我的回答,如果这能解决您的问题,请告诉我?
  • @SS_flair 感谢您的支持和接受答案,很高兴我有帮助:)
  • @OpsterElasticsearchNinja 感谢您的快速回复,因为我无法访问数据集的映射,我将不得不查看在输出上动态生成字段

标签: elasticsearch elasticsearch-5


【解决方案1】:

您想要的是能够看到您的文档的 NULL 值,默认情况下这是不可能的,因为 NULL 值没有被索引和搜索,请参考 official NULL_VALUES doc 了解更多信息

无法索引或搜索空值。当字段设置为 null,(或空数组或空值数组)它被视为 尽管该字段没有值。

但是要实现您的用例,您需要在映射中提供 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"
                }
            }
        ]

【讨论】:

    【解决方案2】:

    您可以使用terms query 返回包含一个 或提供的字段中更准确的术语。

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

    搜索查询:(版本 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 上的此文档以了解其语法。

    【讨论】:

    • 术语查询我正在使用过滤器查询进行 POST,我是否将过滤器查询参数替换为查询参数?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多