是否可以在嵌套字段上使用更像这样的查询?

时间:2016-08-24 13:00:54

标签: elasticsearch nested morelikethis

我有一个基于(嵌套)新闻文章的“事件”类型,包括标题和文本,两者都有多个字段。

我试过了:

{
  "query":{
    "nested":{
      "path":"article",
      "query":{
        "mlt":{
          "fields":["article.title.search","article.text.search"],
          "max_query_terms": 20,
          "min_term_freq": 1,
          "include": "false",
          "like":[{
              "_index":"myindex",
              "_type":"event",
              "doc":{
                "article":{
                  "title":"this is the title",
                  "text":"this is the body of the article"
              }
          }]
        }
      }
    }
  }
}

但它总是返回0次点击

1 个答案:

答案 0 :(得分:1)

{
"query": {
    "nested":{
        "path":"articles",
        "query":{
            "more_like_this" : {
                "fields" : ["articles.brand", "articles.category", "articles.material"],
                "like" : [
                {
                    "_index" : "$index",
                    "_type" : "$type",
                    "_id" : "$id"
                }
                ],
                "min_term_freq" : 1,
                "max_query_terms" : 20
            }
        }
    }
}

这对我有用,考虑到您使用的嵌套字段的映射必须定义为术语向量。

"brand": {
            "type": "string",
            "index": "not_analyzed",
            "term_vector": "yes"
          }

请参阅:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html