Elasticsearch使用Python进行多重查询/搜索
我是Python和Elasticsearch的新手,我在Elasticsearch中创建了一个包含一些数据的索引,我想使用Python根据从用户收到的一些过滤器(关键字、类别)对它们执行查询 问题是,这个程序只在2个字段已满时返回匹配的数据,但我希望它在1个类似字段的类别为空时也返回数据。当关键字为空时,它与“”类似,且不返回任何内容;当类别为空时,我收到此错误:Elasticsearch使用Python进行多重查询/搜索,python,elasticsearch,kibana,Python,elasticsearch,Kibana,我是Python和Elasticsearch的新手,我在Elasticsearch中创建了一个包含一些数据的索引,我想使用Python根据从用户收到的一些过滤器(关键字、类别)对它们执行查询 问题是,这个程序只在2个字段已满时返回匹配的数据,但我希望它在1个类似字段的类别为空时也返回数据。当关键字为空时,它与“”类似,且不返回任何内容;当类别为空时,我收到此错误: elasticsearch.exceptions.RequestError: RequestError(400, 'x_conten
elasticsearch.exceptions.RequestError: RequestError(400, 'x_content_parse_exception', '[multi_match] unknown token [VALUE_NULL] after [query]')
我做错了什么?如何修复我的搜索过滤器?添加带有索引数据、搜索查询和搜索结果的工作示例 根据您的上述评论,如果
内容
字段不包含关键字
,并且类别
字段包含类别
,则将对类别
字段执行搜索查询。这可以通过使用
索引数据:
{
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "d"
}
{
"content": "a",
"title": "b",
"lead": "c",
"category": "category"
}
{
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "category"
}
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "keyword",
"fields": [
"content",
"title",
"lead"
]
}
},
{
"multi_match": {
"query": "category",
"fields": [
"category"
]
}
}
],
"minimum_should_match":1
}
}
}
"hits": [
{
"_index": "stof_64081587",
"_type": "_doc",
"_id": "3",
"_score": 0.9666445,
"_source": {
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "category"
}
},
{
"_index": "stof_64081587",
"_type": "_doc",
"_id": "2",
"_score": 0.60996956,
"_source": {
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "d"
}
},
{
"_index": "stof_64081587",
"_type": "_doc",
"_id": "1",
"_score": 0.35667494,
"_source": {
"content": "a",
"title": "b",
"lead": "c",
"category": "category"
}
}
]
搜索查询:
{
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "d"
}
{
"content": "a",
"title": "b",
"lead": "c",
"category": "category"
}
{
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "category"
}
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "keyword",
"fields": [
"content",
"title",
"lead"
]
}
},
{
"multi_match": {
"query": "category",
"fields": [
"category"
]
}
}
],
"minimum_should_match":1
}
}
}
"hits": [
{
"_index": "stof_64081587",
"_type": "_doc",
"_id": "3",
"_score": 0.9666445,
"_source": {
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "category"
}
},
{
"_index": "stof_64081587",
"_type": "_doc",
"_id": "2",
"_score": 0.60996956,
"_source": {
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "d"
}
},
{
"_index": "stof_64081587",
"_type": "_doc",
"_id": "1",
"_score": 0.35667494,
"_source": {
"content": "a",
"title": "b",
"lead": "c",
"category": "category"
}
}
]
搜索结果:
{
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "d"
}
{
"content": "a",
"title": "b",
"lead": "c",
"category": "category"
}
{
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "category"
}
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "keyword",
"fields": [
"content",
"title",
"lead"
]
}
},
{
"multi_match": {
"query": "category",
"fields": [
"category"
]
}
}
],
"minimum_should_match":1
}
}
}
"hits": [
{
"_index": "stof_64081587",
"_type": "_doc",
"_id": "3",
"_score": 0.9666445,
"_source": {
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "category"
}
},
{
"_index": "stof_64081587",
"_type": "_doc",
"_id": "2",
"_score": 0.60996956,
"_source": {
"content": "keyword a",
"title": "b",
"lead": "c",
"category": "d"
}
},
{
"_index": "stof_64081587",
"_type": "_doc",
"_id": "1",
"_score": 0.35667494,
"_source": {
"content": "a",
"title": "b",
"lead": "c",
"category": "category"
}
}
]
如果像category这样的一个字段是空的,那么也返回数据,关于这一点,这是否意味着
category
字段包含像'
这样的文档,并且您想查询该空字段值?您是否有机会查看我的答案,期待得到您的反馈:)@BhavyaGupta我的意思是,当类别字段为空时,我只想按关键字搜索。当关键字为空时,我只想按类别搜索,当两者都已满时,我想按类别搜索和筛选数据both@BhavyaGupta比如,有时候用户没有输入关键字,但是输入了一个类别,我只想用这个类别查询数据,请看一下我更新的答案,让我知道这是否是你的问题?现在当我把关键字放空:{'take':0,'timed_out':False,'u shards':{'total':1,'successful':1,'skipped':0,'failed':0},'hits':{'total':{'value':0,'relation':'eq','max_score':无,'hits':[]}当类别为空时:elasticsearch.exceptions.RequestError:RequestError(400,'x_content_parse_exception','[query]'后的[multi_match]未知标记[VALUE_NULL])@bluxixixi关键字
为空,这意味着?您是否在其中一个文档中没有使用关键字
字段?