2017-10-17 86 views
0

我有一个类型的定义如下 “taggeable”:弹性搜索更多类似这样的查询与过滤器将结果

{ 
"mappings": { 
    "taggeable" : { 
     "_all" : {"enabled" : false}, 
     "properties" : { 
      "category" : { 
       "type" : "string" 
      }, 
      "tags" : { 
       "type" : "string", 
       "term_vector" : "yes" 
      } 
     } 
    } 
} 

}

我也有这个5号文件:

Document1 (tags: "t1 t2", category: "cat1") 
Document2 (tags: "t1" , category: "cat1") 
Document3 (tags: "t1 t3", category: "cat1") 
Document4 (tags: "t4" , category: "cat1") 
Document5 (tags: "t4" , category: "cat2") 

以下查询:

{ 
"query": { 
    "more_like_this" : { 
     "fields" : ["tags"], 
     "like" : ["t1", "t2"], 
     "min_term_freq" : 1, 
     "min_doc_freq": 1 
     } 
    } 
} 

将返回:

Document1 (tags: "t1 t2", category: "cat1") 
Document2 ("t1", category: "cat1") 
Document3 ("t1 t3", category: "cat1") 

这是正确的,但此查询:

{ 
"query": { 
    "filtered": { 
    "query": { 
     "more_like_this" : { 
     "fields" : ["tags"], 
     "like" : ["t1", "t2"], 
     "min_term_freq" : 1, 
     "min_doc_freq": 1 
    }, 
    "filter": { 
     "bool": { 
       "must": [        
        {"match": { "category": "cat1"}} 
       ] 
     } 
    } 
} 

} }

是返回:

Document1 (tags: "t1 t2", category: "cat1") 
Document4 (tags: "t4" , category: "cat1") 
Document2 (tags: "t1" , category: "cat1") 
Document3 (tags: "t1 t3", category: "cat1") 

这,Document4现在是也被检索出来并且它的分数与Documen1相似,这是一个完美的匹配,即使Document4没有包含在“t1 t2”中的任何单词。

任何人都知道发生了什么?我使用的是弹性搜寻提前

回答

1

2.4.6

感谢这就是为什么缩进一致是很重要的一个很好的例子。在这里,我已经修改你发布什么一致的缩进,问题更加明显(JSONLint是一个方便的工具,如果你不使用的编辑器有助于这一点):

{ 
    "query": { 
    "filtered": { 
     "query": { 
     "more_like_this": { 
      "fields": ["tags"], 
      "like": ["t1", "t2"], 
      "min_term_freq": 1, 
      "min_doc_freq": 1 
     }, 
     "filter": { 
      "bool": { 
      "must": [{ 
       "match": { 
       "category": "cat1" 
       } 
      }] 
      } 
     } 
     } 
    } 
    } 

你的过滤器是“查询”的孩子,而不是“过滤”的孩子。

真的,你不应该使用过滤,它已被否决,see here。你应该改变它为一个布尔,就像那里指定的那样。

+0

我希望查询失败,如果有一个额外的孩子或其他错误。谢谢。 – italktothewind