You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

search.go 1.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package routers
  2. import (
  3. "code.gitea.io/gitea/modules/context"
  4. "code.gitea.io/gitea/modules/log"
  5. "github.com/olivere/elastic/v7"
  6. )
  7. type Table struct {
  8. TableName string
  9. SpecifyField string
  10. SortBy string
  11. Where string
  12. }
  13. type SearchOptions struct {
  14. Page int64
  15. PageSize int64
  16. Key string
  17. SearchObj []Table
  18. }
  19. var client *elastic.Client
  20. var host = "http://192.168.207.94:9200"
  21. func Search(ctx *context.Context) {
  22. TableName := ctx.Query("TableName")
  23. Key := ctx.Query("Key")
  24. //SortBy := ctx.Query("SortBy")
  25. //Page := ctx.QueryInt64("Page")
  26. //PageSize := ctx.QueryInt64("PageSize")
  27. //ESSearchUrl := setting.RecommentRepoAddr
  28. var err error
  29. //这个地方有个小坑 不加上elastic.SetSniff(false) 会连接不上
  30. client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host))
  31. if err != nil {
  32. panic(err)
  33. }
  34. if Key != "" {
  35. boolQ := elastic.NewBoolQuery()
  36. nameQuery := elastic.NewMatchQuery("name", Key).Boost(2)
  37. descriptionQuery := elastic.NewMatchQuery("description", Key).Boost(1)
  38. boolQ.Should(nameQuery, descriptionQuery)
  39. res, err := client.Search(TableName+"-es-index").Query(boolQ).Sort("updated_unix", false).Do(ctx.Req.Context())
  40. if err == nil {
  41. ctx.JSON(200, res)
  42. }
  43. } else {
  44. log.Info("query all content.")
  45. res, err := client.Search(TableName+"-es-index").Sort("updated_unix", false).Do(ctx.Req.Context())
  46. if err == nil {
  47. ctx.JSON(200, res)
  48. }
  49. }
  50. }