【问题标题】:ElasticSearch 2.x : more_like_this query and nested objectsElasticSearch 2.x:more_like_this 查询和嵌套对象
【发布时间】:2016-10-10 09:25:54
【问题描述】:

我刚刚发现了"more_like_this" query type 并尝试将它与我的嵌套对象一起使用。 不幸的是,这个查询似乎无法在嵌套对象中搜索。这是我的地图:

"Presentation": {
    "properties": {
      "id": {
        "include_in_all": false,
        "type": "string"
      },
      "title": {
        "include_in_all": true,
        "type": "string"
      },
      "description": {
        "include_in_all": true,
        "type": "string"
      },
      "categories": {
        "properties": {
          "id": {
            "include_in_all": false,
            "type": "string"
          },
          "category": {
            "include_in_all": true,
            "type": "string"
          },
          "category_suggest": {
            "properties": {
              "input": {
                "type": "string"
              },
              "payload": {
                "properties": {
                  "id": {
                    "type": "long"
                  }
                }
              }
            }
          }
        },
        "type": "nested"
      }
    }
  }

我的目标是找到所有与 id "96" 相关的演示文稿,并提升与 "96" 具有相同类别的演示文稿。 但是,在执行下面的查询时,Elasticsearch 只计算“title”和“description”字段的分数(而不是查看“category”)。

{
  "size": 4,
  "query": {
    "more_like_this": {
      "like": [
        {
          "_index": "client",
          "_type": "Presentation",
          "_id": "96"
        }
      ],
      "min_term_freq": 1,
      "max_query_terms": 35,
      "min_word_length": 3,
      "minimum_should_match": "1%"
    }
  }
} 

我也尝试在嵌套字段上强制查询,但它也不起作用:

{
  "size": 4,
  "query": {
    "bool": {
      "should": [
        {
          "more_like_this": {
            "like": [
              {
                "_index": "client",
                "_type": "Presentation",
                "_id": "96"
              }
            ],
            "min_term_freq": 1,
            "max_query_terms": 35,
            "min_word_length": 3,
            "minimum_should_match": "1%"                   
          }
        },
        {
            "nested" : {
                "path":"categories",
                "query" : {
                    "more_like_this": {
                        "like": [
                          {
                            "_index": "client",
                            "_type": "Presentation",
                            "_id": "96"
                          }
                        ],
                        "min_term_freq": 1,
                        "max_query_terms": 35,
                        "min_word_length": 3,
                        "minimum_should_match": "1%"
                    }
                }
            }
        }
      ]
    }
  }
}

我发现这个人也有同样的问题,但使用的是旧版本的 elasticsearch:ElasticSearch More_Like_This API and Nested Object Properties 而且,不幸的是,没有给出可以与 ES 2.x 一起使用的答案(除了展平整个索引,这是我做不到的)。

你们中有人对这个(奇怪的)问题有任何想法吗? 谢谢:)

【问题讨论】:

    标签: elasticsearch elasticsearch-2.0


    【解决方案1】:

    我相信您可以指定要搜索的字段。您可以尝试直接指向嵌套变量。像这样的

    {
      "size": 4,
      "query": {
        "more_like_this": {
          "fields": ["id", "title", "description", "categories.id","categories.description", etc...]
          "like": [
            {
              "_index": "client",
              "_type": "Presentation",
              "_id": "96"
            }
          ],
          "min_term_freq": 1,
          "max_query_terms": 35,
          "min_word_length": 3,
          "minimum_should_match": "1%"
        }
      }
    }
    

    【讨论】:

    • 不幸的是,如果我只保留 categories.id (或任何其他类别字段),它也不起作用:"hits": {"total": 0,"max_score": null,"hits": [ ]}
    【解决方案2】:

    我在 ES 5.3 上遇到同样的问题(我希望从文档和嵌套文档中计算 MLT)。

    您的 bool should 解决方案非常有帮助 - 我试图在一个 MLT 查询中进行连接,但不知道该怎么做。

    通过在嵌套的 MLT 查询中指定 fields,我能够让它工作(或者至少看起来工作正常)。因此,对于您的情况,您将添加:

    "fields": ["categories.*"]
    

    到嵌套的 MLT 查询。不确定这是否适用于 2.x,但认为会提及。

    【讨论】:

    • 不幸的是,它不适用于我的版本。如果我尝试同时放置“标题”和“类别”,结果是一样的。只有“类别。*”,我没有收到任何结果。
    【解决方案3】:

    尝试将"term_vector": "yes" 属性放入映射中。

    根据documentation

    执行 MLT 的字段必须被索引并且类型为字符串。 此外,当对文档使用 like 时,_source 必须是 启用或必须存储字段或存储 term_vector。为了 加快分析速度,有助于在索引时存储术语向量。

    【讨论】:

    • 好主意,但仍然无法正常工作,即使使用最简单的查询:{"query": {"nested": { "path": "categories", "query": { "more_like_this": { "fields": [ "categories.category" ], "like": [ { "_index": "client", "_type": "Presentation", "_id": "40"}], "min_term_freq": 1, "max_query_terms": 20 } } } } } 此处建议:stackoverflow.com/questions/39124066/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 2015-01-13
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多