【问题标题】:Returning all documents when query string is empty查询字符串为空时返回所有文档
【发布时间】:2019-12-15 16:47:12
【问题描述】:

假设我有以下映射:

{
    'properties': {
        {'title': {'type': 'text'},
        {'created': {'type': 'text'}}
    }
}

用户有时会通过created 进行查询,有时会通过titlecreated 进行查询。在这两种情况下,我都希望查询 JSON 尽可能相似。当用户不使用title 进行查询时,创建仅按created 过滤的查询有什么好方法?

我尝试了类似的方法:

    {
        bool: {
            must: [
                {range: {created: {gte: '2010-01-01'}}}, 
                {query: {match_all: {}}}
            ] 
        }
    }

但这没有用。编写此查询的最佳方式是什么?

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    您的查询不起作用,因为 created 的类型为 text 而不是 date,字符串日期的范围查询将无法按预期工作,您应该将映射从 text 类型更改为 date并重新索引您的数据。

    按照this 一步一步地重新索引您的数据(使用新映射)。

    现在,如果我理解正确,您想使用通用查询,根据用户输入过滤title 或/和created

    在这种情况下,我的建议是使用Query String

    一个例子(版本 7.4.x):

    映射

    PUT my_index
    {
      "mappings": {
        "properties": {
          "title": {
            "type": "text"
          },
          "created": {  -------> change type to date instead of text
            "type": "date"
          }
        }
      }
    }
    

    索引一些文档

    PUT my_index/_doc/1
    {
      "title":"test1",
      "created": "2010-01-01"
    }
    
    PUT my_index/_doc/2
    {
      "title":"test2",
      "created": "2010-02-01"
    }
    
    PUT my_index/_doc/3
    {
      "title":"test3",
      "created": "2010-03-01"
    }
    

    搜索查询(已创建)

    GET my_index/_search
    {
     "query": {
       "query_string": {
         "query": "created:>=2010-02-01",
         "fields"  : ["created"]
       }
     } 
    }
    

    结果

    "hits" : [
          {
            "_index" : "my_index",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 1.0,
            "_source" : {
              "title" : "test2",
              "created" : "2010-02-01"
            }
          },
          {
            "_index" : "my_index",
            "_type" : "_doc",
            "_id" : "3",
            "_score" : 1.0,
            "_source" : {
              "title" : "test3",
              "created" : "2010-03-01"
            }
          }]
    

    搜索查询(标题)

    GET my_index/_search
    {
     "query": {
       "query_string": {
         "query": "test2",
         "fields"  : ["title"]
       }
     } 
    }
    

    结果

    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.9808292,
        "_source" : {
          "title" : "test2",
          "created" : "2010-02-01"
        }
      }
    ]
    

    搜索查询(标题和创建)

    GET my_index/_search
    {
     "query": {
       "query_string": {
         "query": "(created:>=2010-02-01) AND test3"
       }
     } 
    }
    

    结果

    "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.9808292,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.9808292,
        "_source" : {
          "title" : "test3",
          "created" : "2010-03-01"
        }
      }
    ]
    

    查询字符串中的字段 - 您可以同时提及这两个字段。如果您删除 fields,则查询将应用于映射中的所有字段。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-14
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      • 2014-07-10
      • 2018-12-08
      • 1970-01-01
      相关资源
      最近更新 更多