0

好吧,伙计们,我希望你们在这个流行病时期做得很好,我在忽略 elasticsearch 查询中的特殊字符时遇到了麻烦:这是我想做的:

Select * from table where ext like %6500% and start_time like %-01-% 

这是我所做的:

   "query": {
       "bool": {
           "must": [
               {
                   "query_string": {
                       "ext": "*6500*",
                       "fields": [
                           "extension"
                       ],
                       "analyze_wildcard": true
                   }
               },
               {
                   "query_string": {
                       "query": "*\\-01\\-*",
                       "fields": [
                           "start_time"
                       ],
                       "analyze_wildcard": true
                   }
               }
           ]
       }
   }

第一个有效,但第二个没有给出我想要的。顺便说一句,字段 start_time 是这样的,例如: 2020-01-03 15:03:45 它是一个关键字类型(我发现它是这样的)。

4

2 回答 2

0

如果您被迫keywordstart_time.start_time

GET karim/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "-01-",
            "fields": [
              "start_time"
            ]
          }
        }
      ]
    }
  }
}

但是,建议在使用date日期(时间)时使用。所以像这样设置你的索引:

PUT karim
{
  "mappings": {
    "properties": {
      "start_time": {
        "type": "date",
        "format": "YYYY-MM-DD HH:mm:ss"
      }
    }
  }
}

像这样查询

GET karim/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "start_time": {
              "gte": "01",
              "lt": "02",
              "format": "MM"
            }
          }
        }
      ]
    }
  }
}

任何给定年份的一月。调整格式以匹配特定年份等。

这种方法保证比通配符文本查询更快,尤其是。当您查询多个范围并且可能打算在未来聚合时。

于 2020-05-24T09:10:35.233 回答
0

您正在使用类型文本和关键字类型的子字段来索引您的字段。文本字段在标记中被破坏,例如“2020-01-12”将存储为 [“2020”、“01”、“12”]。您需要使用“start_time.keyword”对关键字字段运行查询

{
  "query": {
       "bool": {
           "must": [
               {
                   "query_string": {
                       "query": "*-01-*",
                       "fields": [
                           "start_time.keyword" --> note
                       ],
                       "analyze_wildcard": true
                   }
               }
           ]
       }
   }
}

正如@joe 提到的通配符查询性能较差,最好使用日期字段

于 2020-05-26T04:53:13.140 回答