Elasticsearch -包含时间范围的查询字符串

gijlo24d  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(2)|浏览(255)

实际上我想找到“变量ABC包含'apple'并且只计算1天前的文档”的文档。
这将返回ABC为“apple”的所有文档:

'{ 
    "query": {
        "query_string": {
            "query": "apple",
             "default_field": "ABC"
            }
        }
    }'

不幸的是,这并不起作用:

'{ 
    "query": {
        "query_string": {
            "query": "apple",
             "default_field": "ABC"
            }
        },
        "filter": {
            "range": {"nest.nest1.nest2.timestamps.start": {"gte": "now-1d"}}
            }
        }
    }'

我要查找的示例文档,高度缩写,感谢您的帮助:

{
    "hits": [
      {
        "_index": "indexabc",
        "_id": "IDabc",
        "_score": "15"
        "_source": {
             "ABC": "applebottom",
             "nest": {
                 "nest1": {
                    "nest2": {
                        "timestamps": {
                            "start": "2022-09-01T08:00:10+00:00",
                            "send": "2022-09-01T08:16:20+00:00"
                        }
                    }
                 }
              }
           }
        }
     ]
dzhpxtsq

dzhpxtsq1#

您需要这样做:

{
  "query": {
   "bool": {
    "must": [
      {
        "query_string": {
          "query": "apple",
          "default_field": "ABC"
        }
      }
    ],
    "filter": [
      {
        "range": {
          "nest.nest1.nest2.timestamps.start": {
            "gte": "now-1d"
          }
        }
      }
    ]
   }
  }
}
dfddblmv

dfddblmv2#

有多种方法可以解决此问题

1.万用字符

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "*apple*", --> wildcard
            "default_field": "ABC"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "nest.nest1.nest2.timestamps.start": {
              "gte": "now-1y"
            }
          }
        }
      ]
    }
  }
}

通配符的速度很慢。如果您面临性能问题,那么还有其他方法可以解决这个问题。

**2.Prefix**请输入您的密码

如果搜索文本位于单词开头,则可以使用简单前缀查询

{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "ABC": "apple"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "nest.nest1.nest2.timestamps.start": {
              "gte": "now-1y"
            }
          }
        }
      ]
    }
  }
}

第三章

PUT index24
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "ngram",
          "min_gram": 5,
          "max_gram": 5
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "ABC": {
        "type": "text",
        "analyzer": "my_analyzer"
      }
    }
  }
} 

GET index24/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "ABC",
            "query": "apple"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "nest.nest1.nest2.timestamps.start": {
              "gte": "now-1y"
            }
          }
        }
      ]
    }
  }
}

对于“applebottom”,将生成以下标记[“apple”、“ppleb”、“plebo”、“lebot”、“ebott”、“bottom”]。您可以使用“min_gram”调整生成的标记:5和“最大_克”:5.
Ngram允许您在文本之间的任何位置进行搜索,并给予更好的搜索性能,因为标记是在索引时创建的。

相关问题