问题描述
关于字段状态,location_id的我的完全匹配查询如下:
{'query': {'constant_score': {'filter': {'bool': {
'must': [{'bool': {'should': [{'terms': {'status': ['Open','not complete']}}]}},{'bool': {'should': [{'terms': {'location_id': [1652]}}]}}],'must_not': []}}}},'sort': [{'id': {'order': 'asc','unmapped_type': 'long'}}]}
假设上面的查询返回了50条记录,现在我要在这50条记录中的某一列中搜索一个单词。例如,列是描述,单词是“测试”。如何将以上查询与此搜索词查询结合使用。 搜索列可以是必须查询(状态/位置)中的一列
简而言之,我希望所有状态为“打开/未完成”且location_id == 1652的记录以及描述字段的单词“ test”在文本中的任何位置
我尝试了simple_query_String和query_string,但遇到错误。不确定在上面的嵌套查询中可以放在哪里。
我在下面的查询中尝试了 match ,但没有写任何记录:
{'query': {'constant_score': {'filter': {'bool': {
'must': [{'bool': {'should': [{'terms': {'status': ['Open',{'bool': {'should': [{'terms': {'location_id': [1652]}}]}},{'bool': {'should': [{'match': {'description': 'test'}}]}}],'unmapped_type': 'long'}}]}
使用 query_string
的工作查询{'query': {'constant_score': {'filter': {'bool': {
'must': [{'bool': {'should': [{'terms': {'status': ['Open',{'query_string': {"default_field": "description","query": "*test*"}}],'unmapped_type': 'long'}}]}
解决方法
您可以在其中使用boolean query
查询匹配匹配其他布尔组合的文档 查询。
添加带有索引数据,搜索查询和搜索结果的工作示例。 以下搜索查询符合您的以下要求:
简而言之,我要所有状态为“打开/未完成”的记录, location_id == 1652,并在任意位置具有单词“ test”的描述字段 在文字中
索引数据:
{
"description":"test","status":["open","not connected"],"location_id":[1652]
}
搜索查询:
{
"query": {
"bool": {
"must": [
{
"match": {
"description": "test"
}
},{
"terms": {
"status": [
"open","not complete"
]
}
},{
"terms": {
"location_id": [
"1652"
]
}
}
]
}
}
}
搜索结果:
"hits": [
{
"_index": "stof_63936644","_type": "_doc","_id": "1","_score": 2.287682,"_source": {
"description": "test","status": [
"open","not connected"
],"location_id": [
1652
]
}
}
]