1

私はelasticsearchにかなり慣れていないので、この並べ替えを機能させようとして頭を悩ませています。一般的な考え方は、ネストされたメッセージとネストされた参加者を含む電子メール メッセージ スレッドを検索することです。目標は、スレッド レベルで検索結果を表示し、検索を行っている参加者と、参加者がいるメールボックスに応じて last_received_at または last_sent_at 列のいずれかで並べ替えることです。

私の理解では、ネストされた多くの子の中から 1 つの子の値で並べ替えることはできません。したがって、これを行うために、スクリプトで custom_score を使用し、スコアでソートするためのいくつかの提案を見ました。私の計画は、並べ替え列を動的に変更し、ネストされた custom_score クエリを実行して、参加者の 1 人の日付をスコアとして返すことです。スコア形式が奇妙である (例: 末尾に常に 4 つのゼロがある) ことと、期待していた日付が返されない可能性があることの両方の問題に気付きました。

以下は、問題のインデックスとクエリの簡略化されたバージョンです。誰か提案があれば、私はとても感謝しています。(参考までに、私はelasticsearchバージョン0.20.6を使用しています。)

索引:

mappings: {
    message_thread: {
        properties: {
            id: {
                type: long
            }
            subject: {
                dynamic: true
                properties: {
                    id: {
                        type: long
                    }
                    name: {
                        type: string
                    }
                }
            }
            participants: {
                dynamic: true
                properties: {
                    id: {
                        type: long
                    }
                    name: {
                        type: string
                    }
                    last_sent_at: {
                        format: dateOptionalTime
                        type: date
                    }
                    last_received_at: {
                        format: dateOptionalTime
                        type: date
                    }
                }
            }
            messages: {
                dynamic: true
                properties: {
                    sender: {
                        dynamic: true
                        properties: {
                            id: {
                                type: long
                            }
                        }
                    }
                    id: {
                        type: long
                    }
                    body: {
                        type: string
                    }
                    created_at: {
                        format: dateOptionalTime
                        type: date
                    }
                    recipient: {
                        dynamic: true
                        properties: {
                            id: {
                                type: long
                            }
                        }
                    }
                }
            }
            version: {
                type: long
            }
        }
    }
}

クエリ:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": { "participants.id": 3785 }
        },
        {
          "custom_score": {
            "query": {
              "filtered": {
                "query": { "match_all": {} },
                "filter": {
                  "term": { "participants.id": 3785 }
                }
              }
            },
            "params": { "sort_column": "participants.last_received_at" },
            "script": "doc[sort_column].value"
          }
        }
      ]
    }
  },
  "filter": {
    "bool": {
      "must": [
        {
          "term": { "messages.recipient.id": 3785 }
        }
      ]
    }
  },
  "sort": [ "_score" ]
}

解決:

@imotov のおかげで、これが最終結果です。参加者がインデックスで適切にネストされていませんでした (メッセージはそうである必要はありませんでした)。さらに、クエリを簡素化するために参加者に include_in_root が使用されました (参加者は小さなレコードであり、実際のサイズの問題ではありませんが、@imotov はこれを使用しない例も提供しています)。次に、dis_max クエリを使用するように JSON リクエストを再構築しました。

curl -XDELETE "localhost:9200/test-idx"
curl -XPUT "localhost:9200/test-idx" -d '{
  "mappings": {
    "message_thread": {
      "properties": {
        "id": {
          "type": "long"
        },
        "messages": {
          "properties": {
            "body": {
              "type": "string",
              "analyzer": "standard"
            },
            "created_at": {
              "type": "date",
              "format": "yyyy-MM-dd'\''T'\''HH:mm:ss'\''Z'\''"
            },
            "id": {
              "type": "long"
            },
            "recipient": {
              "dynamic": "true",
              "properties": {
                "id": {
                  "type": "long"
                }
              }
            },
            "sender": {
              "dynamic": "true",
              "properties": {
                "id": {
                  "type": "long"
                }
              }
            }
          }
        },
        "messages_count": {
          "type": "long"
        },
        "participants": {
          "type": "nested",
          "include_in_root": true,
          "properties": {
            "id": {
              "type": "long"
            },
            "last_received_at": {
              "type": "date",
              "format": "yyyy-MM-dd'\''T'\''HH:mm:ss'\''Z'\''"
            },
            "last_sent_at": {
              "type": "date",
              "format": "yyyy-MM-dd'\''T'\''HH:mm:ss'\''Z'\''"
            },
            "name": {
              "type": "string",
              "analyzer": "standard"
            }
          }
        },
        "subject": {
          "properties": {
            "id": {
              "type": "long"
            },
            "name": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}'
curl -XPUT "localhost:9200/test-idx/message_thread/1" -d '{
  "id" : 1,
  "subject" : {"name": "Test Thread"},
  "participants" : [
    {"id" : 87793, "name" : "John Smith", "last_received_at" : null, "last_sent_at" : "2010-10-27T17:26:58Z"},
    {"id" : 3785, "name" : "David Jones", "last_received_at" : "2010-10-27T17:26:58Z", "last_sent_at" : null}
  ],
  "messages" : [{
    "id" : 1,
    "body" : "This is a test.",
    "sender" : { "id" : 87793 },
    "recipient" : { "id" : 3785},
    "created_at" : "2010-10-27T17:26:58Z"
  }]
}'
curl -XPUT "localhost:9200/test-idx/message_thread/2" -d '{
  "id" : 2,
  "subject" : {"name": "Elastic"},
  "participants" : [
    {"id" : 57834, "name" : "Paul Johnson", "last_received_at" : "2010-11-25T17:26:58Z", "last_sent_at" : "2010-10-25T17:26:58Z"},
    {"id" : 3785, "name" : "David Jones", "last_received_at" : "2010-10-25T17:26:58Z", "last_sent_at" : "2010-11-25T17:26:58Z"}
  ],
  "messages" : [{
    "id" : 2,
    "body" : "More testing of elasticsearch.",
    "sender" : { "id" : 57834 },
    "recipient" : { "id" : 3785},
    "created_at" : "2010-10-25T17:26:58Z"
  },{
    "id" : 3,
    "body" : "Reply message.",
    "sender" : { "id" : 3785 },
    "recipient" : { "id" : 57834},
    "created_at" : "2010-11-25T17:26:58Z"
  }]
}'
curl -XPOST localhost:9200/test-idx/_refresh
echo
# Using include in root
curl "localhost:9200/test-idx/message_thread/_search?pretty=true" -d '{
  "query": {
    "filtered": {
      "query": {
        "nested": {
          "path": "participants",
          "score_mode": "max",
          "query": {
            "custom_score": {
              "query": {
                "filtered": {
                  "query": {
                    "match_all": {}
                  },
                  "filter": {
                    "term": {
                      "participants.id": 3785
                    }
                  }
                }
              },
              "params": {
                "sort_column": "participants.last_received_at"
              },
              "script": "doc[sort_column].value"
            }
          }
        }
      },
      "filter": {
        "query": {
          "multi_match": {
            "query": "test",
            "fields": ["subject.name", "participants.name", "messages.body"],
            "operator": "and",
            "use_dis_max": true
          }
        }
      }
    }
  },
  "sort": ["_score"],
  "fields": []
}
'

# Not using include in root
curl "localhost:9200/test-idx/message_thread/_search?pretty=true" -d '{
  "query": {
    "filtered": {
      "query": {
        "nested": {
          "path": "participants",
          "score_mode": "max",
          "query": {
            "custom_score": {
              "query": {
                "filtered": {
                  "query": {
                    "match_all": {}
                  },
                  "filter": {
                    "term": {
                      "participants.id": 3785
                    }
                  }
                }
              },
              "params": {
                "sort_column": "participants.last_received_at"
              },
              "script": "doc[sort_column].value"
            }
          }
        }
      },
      "filter": {
        "query": {
          "bool": {
            "should": [{
              "match": {
                "subject.name":"test"
              }
            }, {
              "nested" : {
                "path": "participants",
                "query": {
                  "match": {
                    "name":"test"
                  }
                }
              }
            }, {
              "match": {
                "messages.body":"test"
              }
            }
            ]
          }
        }
      }
    }
  },
  "sort": ["_score"],
  "fields": []
}
'
4

1 に答える 1