簡體   English   中英

ElasticSearch 2.x:more_like_this查詢和嵌套對象

[英]ElasticSearch 2.x : more_like_this query and nested objects

我剛剛發現了“more_like_this”查詢類型並試圖將它與我的嵌套對象一起使用。 不幸的是,這個查詢似乎無法在嵌套對象中搜索。 這是我的映射:

"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只計算“標題”和“描述”字段的分數(而不是查看“類別”)。

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

我發現這個人有同樣的問題,但有一個舊版本的elasticsearchElasticSearch More_Like_This API和嵌套對象屬性不幸的是,沒有給出可以與ES 2.x一起使用的答案(除了壓平整個索引,我不能這樣做。

你們中的任何一個人對這個(奇怪的)問題有什么看法嗎? 謝謝 :)

我相信您可以指定要搜索的字段。 您可以嘗試直接指向嵌套變量。 像這樣的東西

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

我在ES 5.3上有同樣的問題(我希望從文檔和嵌套文檔計算MLT)。

你的bool should解決方案是非常有幫助的 - 我試圖在一個MLT查詢內部加入,並且無法弄清楚如何這樣做。

通過在嵌套的MLT查詢中指定fields ,我能夠使這個工作(或者至少看起來工作得很好)。 所以對於你的情況,你會添加:

"fields": ["categories.*"]

到嵌套的MLT查詢。 不確定這是否適用於2.x,但認為會提到。

嘗試在映射中添加"term_vector": "yes"屬性。

根據文檔

必須對要執行MLT的字段建立索引並使用字符串類型。 此外,在文檔中使用like時,必須啟用_source或必須存儲字段或存儲term_vector。 為了加速分析,它可以幫助在索引時存儲術語向量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM