【问题标题】:ElasticSearch DSL query not working on a specific fieldElasticSearch DSL 查询不适用于特定字段
【发布时间】:2021-10-28 08:41:18
【问题描述】:

我正在查询一个 ElasticSearch 数据库。我的文件结构如下:

    {
      "uuid": "5035732b-9ba8-4cf7-96a7-6d1c7e179fec",
      "name": "LTK_1604d0",
      "recording_datetime": "2021-10-10T08:08:13",
      "stopped_recording_datetime": "2021-10-11T06:53:12",
      "created": "2021-10-11T17:25:16.754278",
      "source": "U",
      "file": "ISO-XML_LTK_15f407_LTK_15f9a0_LTK_15fc07_LTK_15fed7_LTK_15ffcb (1).zip",
      "data_format": "p",
      "data_type": "ISOBUS",
      "data_set_rois": [],
      "dataset_measurements": [
        {
            "id": 1995250,
            "index": 2,
            "name": "iso_013A",
            "unit": "%",
            "device": "49700062",
            "code": "iso_013A",
            "count": 8067,
            "std": 21.233523112224,
            "min": 0.0,
            "max": 55.0,
            "mean": 18.4815544812198,
            "percentiles": [
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                40.0,
                40.5,
                40.7,
                41.0,
                41.5,
                42.2,
                43.3,
                45.8
            ],
            "prio": 1
        },
        {
            "id": 1995248,
            "index": 0,
            "name": "iso_0054",
            "unit": "t/ha",
            "device": "49700062",
            "code": "iso_0054",
            "count": 8067,
            "std": 37.4462960389206,
            "min": 0.0,
            "max": 412.41,
            "mean": 26.6444713028387,
            "percentiles": [
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                21.62,
                47.329,
                54.652,
                58.21,
                61.188,
                64.041,
                68.034,
                79.57
            ],
            "prio": 1
        },
        {
            "id": 1995253,
            "index": 5,
            "name": "iso_E107+107",
            "unit": "U/min",
            "device": "49700062",
            "code": "iso_E107+107",
            "count": 8067,
            "std": 617.86811828088,
            "min": 0.0,
            "max": 2129.0,
            "mean": 1406.03780835503,
            "percentiles": [
                0.0,
                0.0,
                998.0,
                999.0,
                999.0,
                1002.0,
                1284.1,
                1500.4,
                1598.0,
                1659.0,
                1701.0,
                1729.0,
                1762.0,
                1810.0,
                1856.0,
                1931.0,
                1991.0,
                2030.0,
                2049.0
            ],
            "prio": 2
        },
        {
            "id": 1995252,
            "index": 4,
            "name": "iso_E104+107",
            "unit": " ",
            "device": "49700062",
            "code": "iso_E104+107",
            "count": 8067,
            "std": 0.0,
            "min": 52.0,
            "max": 52.0,
            "mean": 52.0,
            "percentiles": [
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0,
                52.0
            ],
            "prio": 2
        },
        {
            "id": 1995251,
            "index": 3,
            "name": "iso_018D",
            "unit": "km/h",
            "device": "49700062",
            "code": "iso_018D",
            "count": 8067,
            "std": 9.64427850775535,
            "min": 0.0,
            "max": 45.07,
            "mean": 6.29758894260568,
            "percentiles": [
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.22,
                2.62,
                3.66,
                3.87,
                3.98,
                4.09,
                4.2,
                4.33,
                4.49,
                4.79,
                6.69400000000001,
                9.74299999999998,
                18.872,
                32.234
            ],
            "prio": 2
        },
        {
            "id": 1995249,
            "index": 1,
            "name": "iso_0057",
            "unit": "t/h",
            "device": "49700062",
            "code": "iso_0057",
            "count": 8067,
            "std": 81.4727887924538,
            "min": 0.0,
            "max": 277.1,
            "mean": 66.4214788645097,
            "percentiles": [
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                0.0,
                64.5519999999999,
                128.509,
                153.67,
                164.72,
                171.156,
                176.18,
                181.524,
                188.78
            ],
            "prio": 2
        }
      ],
      "user": "test@test.nl",
      "centroid": "POINT (54.423692227112667 7.45637103322727)",
      "mappings": null
}

我想获取data_type = ISOBUScreated starts with 2021 所在的所有文档文档。但是,当我尝试查询 created 字段时,无论我如何编写查询,它总是返回 0 个命中。我还必须在查询中添加关键字,否则服务器只会返回内部服务器错误。为什么查询中某些字段名要加关键字?

1.

{
    "query": {
        "prefix": { "created.keyword":"2021" }
    }
}

2.

{
    "query": {
        "bool": {
            "must": [{
                "prefix": { "created.keyword":"2021" }
            }]
        }
    }
}

3.

{
    "query": {
        "wildcard": { 
            "created.keyword": {"value": "2021*"}
        }
    }
}

4.

{
    "query": {
        "bool": {
            "must": [
                { "term": { "data_type.keyword": "ISOBUS"} },
                { "prefix": { "created.keyword":"2021" } }]
        }
    }
}

【问题讨论】:

    标签: elasticsearch dsl


    【解决方案1】:

    这实际上取决于您的映射。最好的办法是为您的created 字段使用date 类型:

    {
        "mappings": {
            "properties": {
                "data_type": {
                    "type": "keyword"
                },
                "created": {
                    "type": "date"
                }
            }
        }
    }
    

    然后你可以索引你的文档:

    POST index/_doc
    
    {
        "created": "2021-10-11T17:25:16.754278",
        "data_type": "ISOBUS"
    }
    

    然后就可以查询了:

    {
        "query": {
            "bool": {
                "must": [
                    {"term": {"data_type": "ISOBUS"}},
                    {"range": {
                        "created": {
                            "gte": "2021",
                            "lt": "2022"
                        }
                    }}
                ]
            }
        }
    }
    

    这个想法是您要搜索 2021 年的确切年份,因此您可以得到 >= 2021 和

    【讨论】:

      猜你喜欢
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 2021-02-11
      相关资源
      最近更新 更多