一、基本操作
1.列表元素之一查询,如 "terminalType": ["pc", "mobile"]
正确用法:
GET
http://0.0.0.0:8200/amis_action_data/data/_search?size=10000
查询条件:
Ps:数组查询要用关键字terms(如下面的
terminalType字段)
,而不是term,否则会报错"
[term] query does not support array of values"
{
"query": {
"bool": {
"must": [
{
"terms": {
"terminalType": ["pc", "mobile"]
}
},
{
"range": {
"time": {
"lte": 1631774498,
"gte": 1631030400
}
}
},
{
"term": {
"tab": "find"
}
},
{
"term": {
"series": "other"
}
}
]
}
}
}
2、根据条件删除ES中的数据
import requests
import json
# 注意替换ES地址
url = "http://10.xxx.xx.xx:8000/user_knowledge/data/_delete_by_query"
payload = json.dumps({
"query": {
"bool": {
"must": [
{
"term": {
"userId": "zhangshan01"
}
},
{
"term": {
"_id": "123seevfff"
}
}
]
}
}
})
# 注意鉴权信息
headers = {
'Authorization': 'Basic c3VwZXddddddddddd=',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
二、问题
1、根据某个字段的值查询不到数据(macth 可以,term不行)
分析:
通过term查询不到数据,但是根据match可以查到,后来发现是因为es字段类型设置的问题,因为设置了该字段类型为text类型,导致es对存储的数据建立了分词索引,大写字母都转成小写了,把类型改为keyword就可以了,见:参考
在ES查询的时候,发现一个奇怪的现象:命名库里面某个字段有数据,按照查询条件却查询不出来,比如"scene"字段有两个可能的值:WEEKLY_REPORT、service,使用 term大写查不到数据,小写可以查到数据,使用 macth大小写均可以,具体现象如下:
- 使用 term,"scene": "service"作为查询条件可以查到数据
- 使用 term,"scene": "WEEKLY_REPORT"(大写)作为查询条件查不到数据
- 使用 term,"scene": "weekly_report"(小写)作为查询条件可以查到数据
- 使用macth时,无论大小写都可以查到数据