如何将完全匹配查询与文本搜索查询结合起来?

问题描述

关于字段状态,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_Stringquery_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
          ]
        }
      }
    ]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...