dongyi2425
2017-03-18 14:33
浏览 98
已采纳

使用bool查询的ElasticSearch全文搜索

I am trying to make full-text search in whole document using

"query" => [
    "query_string" => [
        "fields" => ["_all"],
        "query" => "fooA AND fooB"
    ]
]

It works pretty good. In the same query I need to run bool query

"query" => [
    "bool" => [
        "must" => [
            "term" => [
                "name" => "My_name"
            ]
        ],
        "should" => [
            ....
        ]
    ]
]

Is it possible to combine these two queries ? is this proper way to make full-text search ?

In result I need all documents that contains "fooA AND fooB" in any field and special field name equals "My_name".

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用

在整个文档中进行全文搜索 “query”=>  [
“query_string”=>  [
“fields”=>  [“_ all”],
“查询”=>  “fooA AND fooB”
] 
] 
   
 
 

它的效果非常好。 在同一个查询中我需要运行bool查询

 “query”=>  [
“bool”=>  [
“必须”=>  [
“term”=>  [
“name”=>  “My_name”
] 
],
“应该”=>  [
 .... 
] 
] 
] 
   
 
 

是否可以合并这两个查询? 这是进行全文搜索的正确方法吗?

结果我需要在任何字段中包含“fooA AND fooB”的所有文档,特殊字段名称等于“My_name”。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drny60365 2017-03-18 17:05
    已采纳

    I found solution for my question thanks to this post.

    "query" => [
        "bool" => [
            "must" => [
                [
                    "term" => [
                        "name" => "My_name"
                    ]
                ],
                [
                    "query_string" => [
                        "fields" => ["_all"],
                        "query" => "fooA AND fooB"
                    ]
                ]
            ]
        ]
    ]
    

    This combination works for me

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doucan1996 2017-03-18 15:59

    Yes, you can club these together as shown below :

    GET _search
    {
      "from": 0,
      "size": 20,
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "name": "MY_NAME"
              }
            },
            {
              "query_string": {
                "query": "fooA AND fooB",
                "fields": [
                  "_all"
                ]
              }
            }
          ]
        }
      }
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题