必须匹配查询在弹性搜索中不起作用

时间:2017-02-08 01:29:18

标签: elasticsearch

我想找到所有名为“The Shining”的视频 还有一个parent_id = 189和parent_type =“folder”

我的查询接缝将所有匹配语句与“OR”而不是“AND”连接

我做错了什么?

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

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

        }
      ]
    }
}
}

映射:

{"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"
      }
    }
  }
}}}

2 个答案:

答案 0 :(得分:0)

由于match查询,您将获得 闪亮的匹配,并根据匹配情况获得分数。最简单的修复方法之一是添加运算符and

{
   "match": {
      "name": "The Shining", 
      "operator": "and"
    }
}

但这并不是你所需要的,因为这也会与“闪耀的”或“阳光灿烂”的名字相匹配。

其他选项是,如果您需要在名称上进行完全匹配,则需要将字段名称设为未分析。在ES 5中,您可以将字段类型设置为keyword

此外,我建议您使用bool查询term查询,因为它们会完全匹配。

{
  "fields": ["name","parent_id","parent_type"],
  "query": {
        "bool": {
          "must":[{
              "term": {
                "name": "The Shining"
              }
            },
            {
            "term": {
              "parent_id": 189
            }
          },
            {
              "term": {
                "parent_type": "folder"
              }

            }
          ]
        }
    }
}

答案 1 :(得分:0)

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

正在找到传统的比赛,因为他们匹配“the”。我试图添加运算符参数,但不幸的是,这不起作用。我所做的是使用“match_phrase”并将我的另外两个匹配字段切换为“term” - 请参阅下面的答案 -

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

                    ]
                ]
            ]
        ];