在ElasticSearch中按日期筛选

a2mppw5e  于 2022-09-20  发布在  ElasticSearch
关注(0)|答案(2)|浏览(189)

我尝试搜索日期字段在某个范围内的所有项目,但搜索失败(不返回任何结果)

查询:

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "range": {
          "last_updated": {
            "from": "2013-01-01 00:00:00"
          }
        }
      }
    }
  }
}

Map:

{
    "my_doctype": {
        "_timestamp": {
            "enabled": "true"
        },
        "properties": {
            "cards": {
                "type": "integer"
            },
            "last_updated": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss"
            }
        }
    }
}

结果是:

{
        took: 1
        timed_out: false
        _shards: {
            total: 1
            successful: 1
            failed: 0
        }
        hits: {
            total: 0
            max_score: null
            hits: [ ]
        }
    }

对于具有数值的整型字段(“卡片”),按范围进行筛选的相同查询也可以正常工作。将日期更改为非常早的开始日期(1900-01-01 00:00:00)也不显示任何结果。

我做错了什么?

顺便说一句,我知道我在Map中启用了_TIMESTAMP,但这不是我要过滤的字段。

qzlgjiam

qzlgjiam1#

在我看来,一切都很顺利:

curl -XPUT localhost:9200/test -d '{
    "settings": {
        "index.number_of_shards": 1,
        "index.number_of_replicas": 0
    },
    "mappings": {
        "doc": {
            "_timestamp": {
                "enabled": "true"
            },
            "properties": {
                "cards": {
                    "type": "integer"
                },
                "last_updated": {
                    "type": "date",
                    "format": "yyyy-MM-dd HH:mm:ss"
                }
            }
        }
    }
}
'
curl -XPOST localhost:9200/test/doc/1 -d '{
    "last_updated": "2012-01-01 12:13:14"
}
'
curl -XPOST localhost:9200/test/doc/2 -d '{
    "last_updated": "2013-02-02 01:02:03"
}
'
curl -X POST 'http://localhost:9200/test/_refresh'
echo
curl -X GET 'http://localhost:9200/test/doc/_search?pretty' -d '{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "range": {
                    "last_updated": {
                        "gte": "2013-01-01 00:00:00"
                    }
                }
            }
        }
    }
}
'
332nm8kg

332nm8kg2#

使用(Python)按日期过滤数据

别忘了安装导入Python Elasticsearch Client

@app.route('/get-data', methods=['GET', 'POST'])
def getFilteredData():
    uri = f'https://localhost:9200/'
    es = Elasticsearch(hosts=uri, basic_auth=(USERNAME, PASSWORD))
    body = {"query" :{"match_all":{}}}

    # if the method POST
    if request.method == 'POST':
        from_date = request.form.get('fromdate')
        to_date = request.form.get('todate')
        body = {
                "query": {
                    "range": {
                        "order_date": {
                            "gte": from_date,
                            "lte": to_date
                        }
                    }
                }
            }
    index_data = client.search(index=index, body=body)
    return render_template('index.html',index_data=index_data)

Git Hub LisaHJung

Relevance of a search

相关问题