func TestSearchSortOrder(t *testing.T) {
	c := NewTestConn()

	// ok, now lets try sorting by repository watchers descending
	qry := Search("github").Pretty().Query(
		Query().All(),
	).Sort(
		Sort("repository.watchers").Desc(),
	)
	out, _ := qry.Result(c)

	// how many different docs used the word "add", during entire time range
	expectedDocs := 10
	expectedTotal := 8085
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedTotal, fmt.Sprintf("Should have %v total= %v", expectedTotal, out.Hits.Total))
	b, err := out.Hits.Hits[0].Source.MarshalJSON()
	assert.T(t, err == nil, fmt.Sprintf("Should not have returned an error: %v", err))
	h1 := gou.NewJsonHelper(b)
	assert.T(t, h1.Int("repository.watchers") == 41377,
		fmt.Sprintf("Should have 41377 watchers= %v", h1.Int("repository.watchers")))

	// ascending
	out, _ = Search("github").Pretty().Query(
		Query().All(),
	).Sort(
		Sort("repository.watchers"),
	).Result(c)
	// how many different docs used the word "add", during entire time range
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedTotal, fmt.Sprintf("Should have %v total got %v", expectedTotal, out.Hits.Total))
	b, err = out.Hits.Hits[0].Source.MarshalJSON()
	assert.T(t, err == nil, fmt.Sprintf("Should not have returned an error: %v", err))
	h2 := gou.NewJsonHelper(b)
	assert.T(t, h2.Int("repository.watchers") == 0,
		fmt.Sprintf("Should have 0 watchers= %v", h2.Int("repository.watchers")))

	// sort descending with search
	out, _ = Search("github").Pretty().Size("5").Query(
		Query().Search("python"),
	).Sort(
		Sort("repository.watchers").Desc(),
	).Result(c)
	// how many different docs used the word "add", during entire time range
	expectedDocs = 5
	expectedTotal = 734

	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedTotal, fmt.Sprintf("Should have %v total got %v", expectedTotal, out.Hits.Total))

	b, err = out.Hits.Hits[0].Source.MarshalJSON()
	assert.T(t, err == nil, fmt.Sprintf("Should not have returned an error: %v", err))
	h3 := gou.NewJsonHelper(b)
	watchers := 8659
	assert.T(t, h3.Int("repository.watchers") == watchers,
		fmt.Sprintf("Should have %v watchers, got %v", watchers, h3.Int("repository.watchers")))

}
func TestSearchFacetRange(t *testing.T) {
	c := NewTestConn()

	// ok, now lets try facet but on actor field with a range
	qry := Search("github").Pretty().Facet(
		Facet().Fields("actor").Size("500"),
	).Query(
		Query().Search("add"),
	)
	out, err := qry.Result(c)
	assert.T(t, out != nil && err == nil, t, "Should have output")

	if out == nil {
		t.Fail()
		return
	}
	//log.Println(string(out.Facets))
	h := gou.NewJsonHelper(out.Facets)
	expectedActorTotal := 521
	// how many different docs used the word "add", during entire time range
	assert.T(t, h.Int("actor.total") == expectedActorTotal, fmt.Sprintf("Should have %v results %v", expectedActorTotal, h.Int("actor.total")))
	// make sure size worked
	expectedTerms := 366
	assert.T(t, len(h.List("actor.terms")) == expectedTerms,
		fmt.Sprintf("Should have %v unique userids, %v", expectedTerms, len(h.List("actor.terms"))))

	// ok, repeat but with a range showing different results
	qry = Search("github").Pretty().Facet(
		Facet().Fields("actor").Size("500"),
	).Query(
		Query().Range(
			Range().Field("created_at").From("2012-12-10T15:00:00-08:00").To("2012-12-10T15:10:00-08:00"),
		).Search("add"),
	)
	out, err = qry.Result(c)
	assert.T(t, out != nil && err == nil, t, "Should have output")

	if out == nil {
		t.Fail()
		return
	}
	//log.Println(string(out.Facets))
	h = gou.NewJsonHelper(out.Facets)
	// how many different events used the word "add", during time range?
	expectedActorTotal = 97
	expectedTerms = 71
	assert.T(t, h.Int("actor.total") == expectedActorTotal, fmt.Sprintf("Should have %v results %v", expectedActorTotal, h.Int("actor.total")))
	// make sure size worked
	assert.T(t, len(h.List("actor.terms")) == expectedTerms,
		fmt.Sprintf("Should have %v event types, %v", expectedTerms, len(h.List("actor.terms"))))

}
func TestFacetRegex(t *testing.T) {
	c := NewTestConn()

	// This is a possible solution for auto-complete
	out, _ := Search("github").Size("0").Facet(
		Facet().Regex("repository.name", "no.*").Size("8"),
	).Result(c)
	if out == nil || &out.Hits == nil {
		t.Fail()
		return
	}
	//Debug(string(out.Facets))
	fh := gou.NewJsonHelper([]byte(out.Facets))
	facets := fh.Helpers("/repository.name/terms")
	assert.T(t, len(facets) == 8, fmt.Sprintf("Should have 8? but was %v", len(facets)))
	// for _, f := range facets {
	// 	Debug(f)
	// }
}
func TestSearchFacetOne(t *testing.T) {
	/*
		A faceted search for what "type" of events there are
		- since we are not specifying an elasticsearch type it searches all ()

		{
		    "terms" : {
		      "_type" : "terms",
		      "missing" : 0,
		      "total" : 7561,
		      "other" : 0,
		      "terms" : [ {
		        "term" : "pushevent",
		        "count" : 4185
		      }, {
		        "term" : "createevent",
		        "count" : 786
		      }.....]
		    }
		 }

	*/
	c := NewTestConn()

	qry := Search("github").Pretty().Facet(
		Facet().Fields("type").Size("25"),
	).Query(
		Query().All(),
	).Size("1")
	out, err := qry.Result(c)
	//log.Println(string(out.Facets))
	//gou.Debug(out)
	assert.T(t, out != nil && err == nil, "Should have output")
	if out == nil {
		t.Fail()
		return
	}
	h := gou.NewJsonHelper(out.Facets)
	expectedTotal := 8084
	expectedTerms := 16
	assert.T(t, h.Int("type.total") == expectedTotal, fmt.Sprintf("Should have %v results %v", expectedTotal, h.Int("type.total")))
	assert.T(t, len(h.List("type.terms")) == expectedTerms, fmt.Sprintf("Should have %v event types, %v", expectedTerms, len(h.List("type.terms"))))

	// Now, lets try changing size to 10
	qry.FacetVal.Size("10")
	out, err = qry.Result(c)
	h = gou.NewJsonHelper(out.Facets)

	// still same doc count
	assert.T(t, h.Int("type.total") == expectedTotal, fmt.Sprintf("Should have %v results %v", expectedTotal, h.Int("type.total")))
	// make sure size worked
	expectedTerms = 10
	assert.T(t, len(h.List("type.terms")) == expectedTerms, fmt.Sprintf("Should have %v event types, got %v", expectedTerms, len(h.List("type.terms"))))

	// now, lets add a type (out of the 16)
	out, _ = Search("github").Type("IssueCommentEvent").Pretty().Facet(
		Facet().Fields("type").Size("25"),
	).Query(
		Query().All(),
	).Result(c)
	h = gou.NewJsonHelper(out.Facets)
	//log.Println(string(out.Facets))
	// still same doc count
	expectedTotal = 685
	assert.T(t, h.Int("type.total") == expectedTotal, fmt.Sprintf("Should have %v results %v", expectedTotal, h.Int("type.total")))
	// we should only have one facettype because we limited to one type
	assert.T(t, len(h.List("type.terms")) == 1, fmt.Sprintf("Should have 1 event types, %v", len(h.List("type.terms"))))

	// now, add a second type (chained)
	out, _ = Search("github").Type("IssueCommentEvent").Type("PushEvent").Pretty().Facet(
		Facet().Fields("type").Size("25"),
	).Query(
		Query().All(),
	).Result(c)
	h = gou.NewJsonHelper(out.Facets)
	// still same doc count
	expectedTotal = 4941
	expectedTerms = 2
	assert.T(t, h.Int("type.total") == expectedTotal, fmt.Sprintf("Should have %v results %v", expectedTotal, h.Int("type.total")))
	// make sure we now have 2 types
	assert.T(t, len(h.List("type.terms")) == expectedTerms, fmt.Sprintf("Should have %v event types, %v", expectedTerms, len(h.List("type.terms"))))

	//and instead of faceting on type, facet on userid
	// now, add a second type (chained)
	out, _ = Search("github").Type("IssueCommentEvent,PushEvent").Pretty().Facet(
		Facet().Fields("actor").Size("500"),
	).Query(
		Query().All(),
	).Result(c)
	h = gou.NewJsonHelper(out.Facets)
	// still same doc count
	expectedTotal = 5168
	expectedTerms = 500
	assert.T(t, h.Int("actor.total") == expectedTotal, t, fmt.Sprintf("Should have %v results %v", expectedTotal, h.Int("actor.total")))
	// make sure size worked
	assert.T(t, len(h.List("actor.terms")) == expectedTerms, t, fmt.Sprintf("Should have %v users, %v", expectedTerms, len(h.List("actor.terms"))))

}