阅读 183

es - elasticsearch search - collapse

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :collapse有什么特点?
答 :
在这里插入图片描述
问 :collapse如何使用?
答 :

# collapse
PUT /collapse_test
{
  "mappings" : {
    "properties" : {
      "name" : {"type" : "text"},
      "user" : {
        "properties": { 
          "id"  : {"type" : "integer"},
          "age" : {"type" : "integer"}
        }
      }
    }
  }
}

# 索引
POST /collapse_test/_doc/1
{
  "name" : "hello",
  "user" : {
    "id"  : 1,
    "age" : 18
  }
}

# 索引
POST /collapse_test/_doc/2
{
  "name" : "hello good",
  "user" : {
    "id"  : 2,
    "age" : 17
  }
}

# 索引
POST /collapse_test/_doc/3
{
  "name" : "hello good",
  "user" : {
    "id"  : 3,
    "age" : 17
  }
}

# 索引
POST /collapse_test/_doc/4
{
  "name" : "hello good",
  "user" : {
    "id"  : 4,
    "age" : 17
  }
}

# 索引
POST /collapse_test/_doc/5
{
  "name" : "hello good me",
  "user" : {
    "id"  : 5,
    "age" : 19
  }
}

# 搜索
GET /collapse_test/_search
{
  "query" : {
    "match" : {
      "name" : "hello"
    }
  },
  "collapse" : {
    "field" : "user.id"
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "collapse_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.10938574,
        "_source" : {
          "name" : "hello",
          "user" : {
            "id" : 1,
            "age" : 18
          }
        },
        "fields" : {
          "user.id" : [
            1
          ]
        }
      },
      {
        "_index" : "collapse_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.08701137,
        "_source" : {
          "name" : "hello good",
          "user" : {
            "id" : 2,
            "age" : 17
          }
        },
        "fields" : {
          "user.id" : [
            2
          ]
        }
      },
      {
        "_index" : "collapse_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.08701137,
        "_source" : {
          "name" : "hello good",
          "user" : {
            "id" : 3,
            "age" : 17
          }
        },
        "fields" : {
          "user.id" : [
            3
          ]
        }
      },
      {
        "_index" : "collapse_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.08701137,
        "_source" : {
          "name" : "hello good",
          "user" : {
            "id" : 4,
            "age" : 17
          }
        },
        "fields" : {
          "user.id" : [
            4
          ]
        }
      },
      {
        "_index" : "collapse_test",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 0.07223585,
        "_source" : {
          "name" : "hello good me",
          "user" : {
            "id" : 5,
            "age" : 19
          }
        },
        "fields" : {
          "user.id" : [
            5
          ]
        }
      }
    ]
  }
}


# 搜索
GET /collapse_test/_search
{
  "collapse" : {
    "field"      : "user.age",
    "inner_hits" : [
      {
      "name" : "most_recent",
      "size" : 4,
      "sort" : [{"user.id" : "desc"}]
     },
     {
       "name" : "largest_responses",
       "size" : 4,
       "sort" : [{"user.id" : "asc"}]
     }
    ],
    "max_concurrent_group_searches" : 4
   },
   "sort" : [{"user.id" : "desc"}]
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "collapse_test",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "name" : "hello good me",
          "user" : {
            "id" : 5,
            "age" : 19
          }
        },
        "fields" : {
          "user.age" : [
            19
          ]
        },
        "sort" : [
          5
        ],
        "inner_hits" : {
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "5",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good me",
                    "user" : {
                      "id" : 5,
                      "age" : 19
                    }
                  },
                  "sort" : [
                    5
                  ]
                }
              ]
            }
          },
          "largest_responses" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "5",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good me",
                    "user" : {
                      "id" : 5,
                      "age" : 19
                    }
                  },
                  "sort" : [
                    5
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "collapse_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : null,
        "_source" : {
          "name" : "hello good",
          "user" : {
            "id" : 4,
            "age" : 17
          }
        },
        "fields" : {
          "user.age" : [
            17
          ]
        },
        "sort" : [
          4
        ],
        "inner_hits" : {
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 3,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "4",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good",
                    "user" : {
                      "id" : 4,
                      "age" : 17
                    }
                  },
                  "sort" : [
                    4
                  ]
                },
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "3",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good",
                    "user" : {
                      "id" : 3,
                      "age" : 17
                    }
                  },
                  "sort" : [
                    3
                  ]
                },
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "2",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good",
                    "user" : {
                      "id" : 2,
                      "age" : 17
                    }
                  },
                  "sort" : [
                    2
                  ]
                }
              ]
            }
          },
          "largest_responses" : {
            "hits" : {
              "total" : {
                "value" : 3,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "2",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good",
                    "user" : {
                      "id" : 2,
                      "age" : 17
                    }
                  },
                  "sort" : [
                    2
                  ]
                },
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "3",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good",
                    "user" : {
                      "id" : 3,
                      "age" : 17
                    }
                  },
                  "sort" : [
                    3
                  ]
                },
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "4",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good",
                    "user" : {
                      "id" : 4,
                      "age" : 17
                    }
                  },
                  "sort" : [
                    4
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "collapse_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "name" : "hello",
          "user" : {
            "id" : 1,
            "age" : 18
          }
        },
        "fields" : {
          "user.age" : [
            18
          ]
        },
        "sort" : [
          1
        ],
        "inner_hits" : {
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello",
                    "user" : {
                      "id" : 1,
                      "age" : 18
                    }
                  },
                  "sort" : [
                    1
                  ]
                }
              ]
            }
          },
          "largest_responses" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello",
                    "user" : {
                      "id" : 1,
                      "age" : 18
                    }
                  },
                  "sort" : [
                    1
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}

# 搜索,二级折叠
GET /collapse_test/_search
{
  "collapse" : {
    "field"      : "user.age",
    "inner_hits" : {
      "name"     : "most_recent",
      "size"     : 4,
      "sort"     : [{"user.id" : "desc"}],
      "collapse" : {
        "field" : "user.id"
      }
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "collapse_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "hello",
          "user" : {
            "id" : 1,
            "age" : 18
          }
        },
        "fields" : {
          "user.age" : [
            18
          ]
        },
        "inner_hits" : {
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello",
                    "user" : {
                      "id" : 1,
                      "age" : 18
                    }
                  },
                  "fields" : {
                    "user.id" : [
                      1
                    ]
                  },
                  "sort" : [
                    1
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "collapse_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "hello good",
          "user" : {
            "id" : 2,
            "age" : 17
          }
        },
        "fields" : {
          "user.age" : [
            17
          ]
        },
        "inner_hits" : {
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 3,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "4",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good",
                    "user" : {
                      "id" : 4,
                      "age" : 17
                    }
                  },
                  "fields" : {
                    "user.id" : [
                      4
                    ]
                  },
                  "sort" : [
                    4
                  ]
                },
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "3",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good",
                    "user" : {
                      "id" : 3,
                      "age" : 17
                    }
                  },
                  "fields" : {
                    "user.id" : [
                      3
                    ]
                  },
                  "sort" : [
                    3
                  ]
                },
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "2",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good",
                    "user" : {
                      "id" : 2,
                      "age" : 17
                    }
                  },
                  "fields" : {
                    "user.id" : [
                      2
                    ]
                  },
                  "sort" : [
                    2
                  ]
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "collapse_test",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "name" : "hello good me",
          "user" : {
            "id" : 5,
            "age" : 19
          }
        },
        "fields" : {
          "user.age" : [
            19
          ]
        },
        "inner_hits" : {
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "collapse_test",
                  "_type" : "_doc",
                  "_id" : "5",
                  "_score" : null,
                  "_source" : {
                    "name" : "hello good me",
                    "user" : {
                      "id" : 5,
                      "age" : 19
                    }
                  },
                  "fields" : {
                    "user.id" : [
                      5
                    ]
                  },
                  "sort" : [
                    5
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}
复制代码
文章分类
后端
文章标签