无法在Elasticsearch中使用新建索引中的query或update _by_query搜索数据

j91ykkif  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(2)|浏览(202)

使用下面的方法在Elasticsearch中创建了一个索引tr_logintracker。我们使用的是Elasticsearch版本7.17。我们甚至尝试用整数数据类型代替索引

{
  "mappings": {
    "properties": {
      "logintime": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "logouttime": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "logout": {
        "type": "long"
      },
      "vehicleid": {
        "type": "long"
      },
      "driverid": {
        "type": "long"
      },
      "vehicleownerid": {
        "type": "long"
      }
    }
  }
}

已创建索引

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "tr_logintracker"
}

文档被插入到索引中。使用

{
    "query": {
        "match_all" : {}
    }
}

回应

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "tr_logintracker",
                "_type": "_doc",
                "_id": "6pJHe4QBPiDyvh1VwkiC",
                "_score": 1.0,
                "_source": {
                    "data": {
                        "vehicleownerid": 17,
                        "driverid": 21,
                        "vehicleid": 20,
                        "logintime": "2022-11-15 18:03:29",
                        "logout": 0
                    }
                }
            }
        ]
    }
}

但查询相同内容时,获取的结果为空查询

{
  "query": {
    "bool": {
      "must": [
         { "match": { "driverid" : 21 }}
      ]
    }
  }
}

回应

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

当选中/tr_logintracker/_mapping时,可以看到下面的内容。这看起来不正确。当我们将文档插入索引时,会出现第二组条目。

{
    "tr_logintracker": {
        "mappings": {
            "properties": {
                "data": {
                    "properties": {
                        "driverid": {
                            "type": "long"
                        },
                        "logintime": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "logout": {
                            "type": "long"
                        },
                        "vehicleid": {
                            "type": "long"
                        },
                        "vehicleownerid": {
                            "type": "long"
                        }
                    }
                },
                "driverid": {
                    "type": "long"
                },
                "logintime": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss"
                },
                "logout": {
                    "type": "long"
                },
                "logouttime": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss"
                },
                "vehicleid": {
                    "type": "long"
                },
                "vehicleownerid": {
                    "type": "long"
                }
            }
        }
    }
}

我们还尝试了动态Map的选项-获取由插入的程序创建的索引。即使在这种情况下,查询也不会获取结果。

jucafojl

jucafojl1#

Tldr;

您似乎有两个driverid条目,

  • data.driverid
  • driverid

在第一次显示的文档中,您有data.driverid: 21,但您似乎查询的是driverid

溶液

这应该可以

{
  "query": {
    "bool": {
      "must": [
         { "match": { "data.driverid" : 21 }}
      ]
    }
  }
}

根本原因

最有可能的是,你必须发送一个类似下面的文档到Elasticsearch

{
  "data": {
    "vehicleownerid": 17,
    "driverid": 21,
    "vehicleid": 20,
    "logintime": "2022-11-15 18:03:29",
    "logout": 0
  }
}

你应该像这样寄到哪里

{
  "vehicleownerid": 17,
  "driverid": 21,
  "vehicleid": 20,
  "logintime": "2022-11-15 18:03:29",
  "logout": 0
}
idv4meu8

idv4meu82#

match_all结果中,我可以看到您要查询的内容位于data对象中,因此,它应该是data.driverid,而不是匹配driverid
您的查询如下所示:

{
  "query": {
    "bool": {
      "must": [
         { "match": { "data.driverid" : 21 }}
      ]
    }
  }
}

相关问题