Elasticsearch 构建动态查询 - python
Elasticsearch build dynamic query - python
关于 elasticsearch-py 的新手问题 api。我建立了姓名、地址、phone 号码等记录的索引,我可以使用 python 客户端进行查询
例如
`elasticsearch.search(index = "index_name", q= 'first_name:"JOHN"')`
并得到合适的结果
但是,我想将查询作为字符串参数
first_name = "JOHN"
qu = "first_name:".join(first_name)
elasticsearch.search(index = 'index_name', q = qu)
查询失败。有没有更好的方法来进行这些类型的动态查询?
你试过像这样解压字典吗:
mydict={"firstname ":名字}
elasticsearch.search(index = 'index_name ", **mydict)
甚至:
mydict={"index": "index_name ","firstname ":名字}
elasticsearch.search( **mydict)
mydict 可以有任何你需要的键值对
值得一试
我通常会将搜索查询构建为请求正文搜索。
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html
示例如下:
searchdata = essearch.search(index=indexname, body= {
"query": {
"filtered": {
"query": {
"query_string": {
"query": "name:fred"
}
},
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "fieldname"
}
},
{
"exists": {
"field": "secondfieldname"
}
}
]
}
}
}
},
"size": 5000,
"sort": [
{
"loggeddate": {
"order": "desc"
}
}
]
}
)
关于 elasticsearch-py 的新手问题 api。我建立了姓名、地址、phone 号码等记录的索引,我可以使用 python 客户端进行查询 例如
`elasticsearch.search(index = "index_name", q= 'first_name:"JOHN"')`
并得到合适的结果 但是,我想将查询作为字符串参数
first_name = "JOHN"
qu = "first_name:".join(first_name)
elasticsearch.search(index = 'index_name', q = qu)
查询失败。有没有更好的方法来进行这些类型的动态查询?
你试过像这样解压字典吗:
mydict={"firstname ":名字} elasticsearch.search(index = 'index_name ", **mydict)
甚至:
mydict={"index": "index_name ","firstname ":名字} elasticsearch.search( **mydict)
mydict 可以有任何你需要的键值对
值得一试
我通常会将搜索查询构建为请求正文搜索。 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html
示例如下:
searchdata = essearch.search(index=indexname, body= {
"query": {
"filtered": {
"query": {
"query_string": {
"query": "name:fred"
}
},
"filter": {
"bool": {
"must": [
{
"exists": {
"field": "fieldname"
}
},
{
"exists": {
"field": "secondfieldname"
}
}
]
}
}
}
},
"size": 5000,
"sort": [
{
"loggeddate": {
"order": "desc"
}
}
]
}
)