gpt4 book ai didi

elasticsearch - Elasticsearch术语与匹配

转载 作者:行者123 更新时间:2023-12-02 22:26:38 26 4
gpt4 key购买 nike

我必须在2个条件下编写搜索查询。

  • 时间戳
  • 目录

  • 当我在搜索查询中使用匹配项时,如下所示
    {
    "query":{
    "bool":{
    "must":{
    "match":{
    "directory":"/user/ayush/test/error/"
    }
    },
    "filter":{
    "range":{
    "@timestamp":{
    "gte":"2020-08-25 01:00:00",
    "lte":"2020-08-25 01:30:00",
    "format":"yyyy-MM-dd HH:mm:ss"
    }
    }
    }
    }
    }
    }
    在筛选结果中,我正在获取目录的记录
  • /user/ayush/test/error/
  • /user/hive/
  • /user/

  • 但是当我使用下面的术语时
    {
    "query":{
    "bool":{
    "must":{
    "term":{
    "directory":"/user/ayush/test/error/"
    }
    },
    "filter":{
    "range":{
    "@timestamp":{
    "gte":"2020-08-25 01:00:00",
    "lte":"2020-08-25 01:30:00",
    "format":"yyyy-MM-dd HH:mm:ss"
    }
    }
    }
    }
    }
    }
    即使使用目录值 /user/ayush/test/error/ ,我也没有得到任何结果

    最佳答案

    The match query analyzes the input string and constructs more basicqueries from that.

    The term query matches exact terms.


    请参阅这些博客以获取详细信息:
    SO question on Term vs Match query
    https://discuss.elastic.co/t/term-query-vs-match-query/14455
    elasticsearch match vs term query
    字段值 /user/ayush/test/error/的分析如下:
    POST/_analyze
    {
    "analyzer" : "standard",
    "text" : "/user/ayush/test/error/"
    }
    生成的 token 为:
    {
    "tokens": [
    {
    "token": "user",
    "start_offset": 1,
    "end_offset": 5,
    "type": "<ALPHANUM>",
    "position": 0
    },
    {
    "token": "ayush",
    "start_offset": 6,
    "end_offset": 11,
    "type": "<ALPHANUM>",
    "position": 1
    },
    {
    "token": "test",
    "start_offset": 12,
    "end_offset": 16,
    "type": "<ALPHANUM>",
    "position": 2
    },
    {
    "token": "error",
    "start_offset": 17,
    "end_offset": 22,
    "type": "<ALPHANUM>",
    "position": 3
    }
    ]
    }
    索引数据:
    { "directory":"/user/ayush/test/error/" }
    { "directory":"/user/ayush/" }
    { "directory":"/user" }
    使用术语查询的搜索查询:
    字词查询不会将任何分析器应用于搜索字词,因此只会在倒排索引中查找该确切字词。因此,要搜索确切的术语,您需要使用 directory.keyword或更改字段的映射。
    {
    "query": {
    "term": {
    "directory.keyword": {
    "value": "/user/ayush/test/error/",
    "boost": 1.0
    }
    }
    }
    }
    字词查询的搜索结果:
    "hits": [
    {
    "_index": "my_index",
    "_type": "_doc",
    "_id": "1",
    "_score": 0.9808291,
    "_source": {
    "directory": "/user/ayush/test/error/"
    }
    }
    ]

    关于elasticsearch - Elasticsearch术语与匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63690769/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com