[英]Elasticsearch query with an array as search input
I'm trying to query some indexed data with an array of strings as search input. 我正在尝试使用字符串数组作为搜索输入来查询一些索引数据。
The indexed data looks like this: 索引数据如下所示:
{
"pubMedID": "21528671",
"title": "Basic fibroblast [...] melanoma cells.",
"abstract": "Human malignant [...] cell growth."
}
I would like to search within the 'title' and 'abstract' fields for multiple strings. 我想在“标题”和“抽象”字段中搜索多个字符串。 For example:
例如:
queryString=['melanoma', 'dysplastic nevus syndrome']
I already tried with the following code: 我已经尝试使用以下代码:
queryString=['melanoma', 'dysplastic nevus syndrome']
payload={
"query": {
"bool": {
"should": [
{
"query_string": {
"query": queryString,
"fields": [
"title",
"abstract"
]
}
}
]
}
}
}
payload_json = (json.dumps(payload))
res = esclient.search(index='medicine',body=payload_json)
But I get the following error when running this: 但是运行此程序时出现以下错误:
RequestError: RequestError(400, 'parsing_exception', '[query_string] query does not support [query]')
The query does work fine if I just put in a simple string value. 如果我只输入一个简单的字符串值,该查询就可以正常工作。 Can someone tell me how I should do this kind of queries where you give as an input an array?
有人可以告诉我在输入数组时我应该如何进行这种查询吗? Thank you in advance!
先感谢您!
EDIT: 编辑:
I was a bit unfamiliar with the query_string query, but it turns out you can do something like this with it too: 我对query_string查询有点不熟悉,但是事实证明,您也可以使用它执行以下操作:
qs = ''
for q in queryStrings:
if qs:
qs += ' OR '
qs += q
payload={
"query": {
"bool": {
"should": [
{
"query_string": {
"query": qs,
"fields": [
"title",
"abstract"
]
}
}
]
}
}
}
the result will be a query similar to the multiple clause one's outlined below. 结果将是类似于下面概述的多重子句的查询。
docs here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html 此处的文档: https : //www.elastic.co/guide/zh-CN/elasticsearch/reference/current/query-dsl-query-string-query.html
ORIGINAL: 原版的:
this can be achieved with multiple clauses like so: 这可以通过多个子句来实现,如下所示:
queryString=['melanoma', 'dysplastic nevus syndrome']
payload={
"query": {
"bool": {
"should": [
{
"query_string": {
"query": queryString[0],
"fields": [
"title",
"abstract"
]
}
},
{
"query_string": {
"query": queryString[1],
"fields": [
"title",
"abstract"
]
}
}
]
}
}
}
If you have a variable number of queries, then you just need to dynamically build your "should" clauses like: 如果查询数量可变,则只需要动态构建“应该”子句,例如:
shoulds = []
for q in queryStrings:
shoulds.append({
"query_string": {
"query": q,
"fields": [
"title",
"abstract"
]
}
})
payload={
"query": {
"bool": {
"should": shoulds
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.