func TestElasticMoreLikeThis(t *testing.T) {

	e, err := NewElastic("localhost", "thedistributedbay_test")
	if err != nil {
		t.Fatal(err)
	}

	desc := randSeq(40)
	t1 := &core.Torrent{
		Hash:        randSeq(40),
		Name:        randSeq(40),
		Description: desc,
		CategoryID:  1,
		Seeders:     core.NewRange(100),
		Leechers:    core.NewRange(10),
	}
	t2 := &core.Torrent{
		Hash:        randSeq(40),
		Name:        randSeq(40),
		Description: desc,
		CategoryID:  2,
		Seeders:     core.NewRange(10),
		Leechers:    core.NewRange(100),
	}
	e.NewTorrent(t1)
	e.NewTorrent(t2)

	_, err = e.MoreLikeThis(t1.Hash)
	if err != nil {
		t.Fatal(err)
	}
	// TODO: Look at results and figure out why nothing is being returned.
}
func (db *TestDB) Get(hash string) (*core.Torrent, error) {
	if hash == "bad hash" {
		return nil, errors.New("invalid hash")
	}
	return &core.Torrent{
		Hash:        "test hash",
		Name:        "test hash",
		Description: "test description",
		CategoryID:  1,
		CreatedAt:   time.Unix(0, 0),
		Files:       100,
		Size:        100,
		Seeders:     core.NewRange(100),
		Leechers:    core.NewRange(100),
		Completed:   core.NewRange(100),
	}, nil
}
func TestElasticSearch(t *testing.T) {

	e, err := NewElastic("localhost", "thedistributedbay_test")
	if err != nil {
		t.Fatal(err)
	}

	desc := randSeq(40)
	t1 := &core.Torrent{
		Hash:        randSeq(40),
		Name:        randSeq(40),
		Description: desc,
		CategoryID:  1,
		Seeders:     core.NewRange(100),
		Leechers:    core.NewRange(10),
	}
	t2 := &core.Torrent{
		Hash:        randSeq(40),
		Name:        randSeq(40),
		Description: desc,
		CategoryID:  2,
		Seeders:     core.NewRange(10),
		Leechers:    core.NewRange(100),
	}
	e.NewTorrent(t1)
	e.NewTorrent(t2)

	// Search with seeders desc
	results, err := e.Search(desc, 0, 10, []uint8{}, "Seeders.Min:desc")
	if err != nil {
		t.Fatal(err)
	}
	if len(results.Hits) != 2 {
		t.Fatal("Wrong number of results")
	}
	if results.Hits[0].Id != t1.Hash {
		t.Fatal("Sorting by seeders failed.")
	}

	// Search with seeders asc
	results, err = e.Search(desc, 0, 10, []uint8{}, "Seeders.Min:asc")
	if err != nil {
		t.Fatal(err)
	}
	if len(results.Hits) != 2 {
		t.Fatal("Wrong number of results")
	}
	if results.Hits[0].Id != t2.Hash {
		t.Fatal("Sorting by seeders failed.")
	}

	// Search with category
	results, err = e.Search(desc, 0, 10, []uint8{1, 8}, "Seeders.Min:desc")
	if err != nil {
		t.Fatal(err)
	}
	if len(results.Hits) != 1 {
		t.Fatal("Wrong number of results")
	}
	if results.Hits[0].Id != t1.Hash {
		t.Fatal("Returned result from wrong category")
	}

	// Search with from statement
	results, err = e.Search(desc, 1, 10, []uint8{}, "Seeders.Min:desc")
	if err != nil {
		t.Fatal(err)
	}
	if len(results.Hits) != 1 {
		t.Fatal("Wrong number of results")
	}
	if results.Hits[0].Id != t2.Hash {
		t.Fatal("Returned wrong result")
	}

	// Search with no sort statement
	results, err = e.Search(desc, 0, 10, []uint8{}, "")
	if err != nil {
		t.Fatal(err)
	}
	if len(results.Hits) != 2 {
		t.Fatal("Wrong number of results")
	}

	// Search with no query
	results, err = e.Search("", 0, 10, []uint8{}, "Seeders.Min:desc")
	if err != nil {
		t.Fatal(err)
	}
	if len(results.Hits) < 2 {
		t.Fatal("Wrong number of results")
	}
}