Is it possible to use a more-like-this query on ne

2019-06-10 09:42发布

问题:

I have an "event" type based on a (nested) press article, including the title, and the text, which both have multifields.

I've tried :

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

But it always returns 0 hits

回答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
            }
        }
    }
}

This Works for me, Taking in consideration that the mapping of the nested fields you are using must be defined as term vectors.

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

Refer to: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html