必须匹配查询不能在弹性搜索中工作 [英] Must match query is not working in elastic search

查看:123
本文介绍了必须匹配查询不能在弹性搜索中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找名为The Shining
的所有视频,但也可以使用parent_id = 189,而parent_type =folder



我的查询接头连接所有匹配语句与OR而不是AND



我做错了什么?

  {
fields:[name,parent_id,parent_type],
query:{

和:{
必须:[
{
match:{
name:Shining
}
},
{
match:{
parent_id:189
}
},
{
:{
parent_type:folder
}

}
]
}
}
}

映射:

 <$ c $ {
{
{
video:{
properties:{
homepage_tags:{
键入:嵌套,
property:{
id:{
type:integer
},
metaType:{
type:string
},
tag_category_name:{
type:string
},
tag_category_order:{
type integer
},
tag_name:{
type:string
}
}
},
id:{
type:integer
},
name:{
type:string
},
parent_id:{
type:integer
},
parent_type:{
type:string
},
provider:{
type:string
},
发布:{
type:string
} ,
query:{
properties:{
bool:{
pr操作:{
必须:{
properties:{
match:{
properties:{
name b $ btype:string
},
parent_id:{
type:long
}
}
}
}
}
}
}
}
},
source_id:{
type:字符串
},
subtitles:{
type:nested,
include_in_root:true,
properties $ bcontent:{
type:string,
store:true,
analyzer:no_stopwords
},
end_time:{
type:float
},
id:{
type:integer
},
parent_type:{
type:string
},
start_time:{
type:float
},
uid:{
type:integer
}
video_id:{
type:string
},
video_parent_id:{
type:integer
},
video_parent_type:{
type:string
}
}
},
tags:{
type:nested,
properties:{
content:{
type:string
},
end_time :{
type:string
},
id:{
type:integer
},
metaType:{
type:字符串
},
parent_type:{
type:string
},
start_time:{
type :string
}
}
},
uid:{
type:integer
},
vid_url:{
type:string
}
}
}
}}
/ pre>

解决方案

我可以通过阅读上面的Volodymryrs答案来解决问题。



由于匹配the,因此发现了其他匹配项。我试图添加操作符参数,但是这不是不幸的。我所做的是使用match_phrase,并将我的另外两个匹配字段切换为term - 请参阅下面的答案 -

  [
'query'=> [
'bool'=> [
'must'=> [
[
'match_phrase'=> [
'name'=> $ searchTerm
]
],
[
'term'=> [
'parent_id'=> intVal($ parent_id)
]
],
[
'term'=> [
'parent_type'=> strtolower($ parent_type)
]
]

]
]
]
];


I am tying to find all Videos with the name "The Shining" but also with a parent_id = 189, and parent_type = "folder"

My query seams to connect all of the match statements with "OR" instead of "AND"

What am I doing wrong?

{
  "fields": ["name","parent_id","parent_type"],
  "query": {

    "and": {
      "must":[
        {
          "match": {
            "name": "The Shining"
          }
        },
        {
        "match": {
          "parent_id": 189
        }
      },
        {
          "match": {
            "parent_type": "folder"
          }

        }
      ]
    }
}
}

Mapping:

{"video" : {
"mappings" : {
  "video" : {
    "properties" : {
      "homepage_tags" : {
        "type" : "nested",
        "properties" : {
          "id" : {
            "type" : "integer"
          },
          "metaType" : {
            "type" : "string"
          },
          "tag_category_name" : {
            "type" : "string"
          },
          "tag_category_order" : {
            "type" : "integer"
          },
          "tag_name" : {
            "type" : "string"
          }
        }
      },
      "id" : {
        "type" : "integer"
      },
      "name" : {
        "type" : "string"
      },
      "parent_id" : {
        "type" : "integer"
      },
      "parent_type" : {
        "type" : "string"
      },
      "provider" : {
        "type" : "string"
      },
      "publish" : {
        "type" : "string"
      },
      "query" : {
        "properties" : {
          "bool" : {
            "properties" : {
              "must" : {
                "properties" : {
                  "match" : {
                    "properties" : {
                      "name" : {
                        "type" : "string"
                      },
                      "parent_id" : {
                        "type" : "long"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "source_id" : {
        "type" : "string"
      },
      "subtitles" : {
        "type" : "nested",
        "include_in_root" : true,
        "properties" : {
          "content" : {
            "type" : "string",
            "store" : true,
            "analyzer" : "no_stopwords"
          },
          "end_time" : {
            "type" : "float"
          },
          "id" : {
            "type" : "integer"
          },
          "parent_type" : {
            "type" : "string"
          },
          "start_time" : {
            "type" : "float"
          },
          "uid" : {
            "type" : "integer"
          },
          "video_id" : {
            "type" : "string"
          },
          "video_parent_id" : {
            "type" : "integer"
          },
          "video_parent_type" : {
            "type" : "string"
          }
        }
      },
      "tags" : {
        "type" : "nested",
        "properties" : {
          "content" : {
            "type" : "string"
          },
          "end_time" : {
            "type" : "string"
          },
          "id" : {
            "type" : "integer"
          },
          "metaType" : {
            "type" : "string"
          },
          "parent_type" : {
            "type" : "string"
          },
          "start_time" : {
            "type" : "string"
          }
        }
      },
      "uid" : {
        "type" : "integer"
      },
      "vid_url" : {
        "type" : "string"
      }
    }
  }
}}}

解决方案

I was able to solve the problem by reading Volodymryrs answer above.

Aditional matches were being found because they were matching "the". I tried to add the operator argument, but that did not work unfortunately. What I did instead was to use "match_phrase" and also switched my two other match fields to "term" - see my answer below –

[
            'query' => [
                'bool' => [
                    'must' => [
                        [
                            'match_phrase' => [
                                'name' => $searchTerm
                            ]
                        ],
                        [
                            'term' => [
                                'parent_id' => intVal($parent_id)
                            ]
                        ],
                        [
                            'term' => [
                                'parent_type' => strtolower($parent_type)
                            ]
                        ]

                    ]
                ]
            ]
        ];

这篇关于必须匹配查询不能在弹性搜索中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆