Example #1
0
func TestSuggest(t *testing.T) {
	c := NewTestConn()
	mappingOpts := MappingOptions{Properties: hash{
		"completion": hash{
			"type": "completion",
		},
	}}
	err := c.PutMapping("github", "SuggestTest", SuggestTest{}, mappingOpts)
	ok(t, err)

	_, err = c.UpdateWithPartialDoc("github", "SuggestTest", "1", nil, SuggestTest{"foobar"}, true)
	ok(t, err)

	query := hash{"completion_completion": hash{
		"text": "foo",
		"completion": hash{
			"size":  10,
			"field": "completion",
		},
	}}

	_, err = c.Refresh("github")
	ok(t, err)

	res, err := c.Suggest("github", nil, query)
	ok(t, err)

	opts, err := res.Result("completion_completion")
	ok(t, err)

	first := opts[0]
	assert.T(t, len(first.Options) > 0, "Length of first.Options was 0.")
	text := first.Options[0].Text
	assert.T(t, text == "foobar", fmt.Sprintf("Expected foobar, got: %s", text))
}
Example #2
0
func TestSearchRequestQueryString(t *testing.T) {
	c := NewTestConn()

	out, err := c.SearchUri("github", "", map[string]interface{}{"q": "actor:a*"})
	expectedHits := 621
	assert.T(t, &out != nil && err == nil, "Should get docs")
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v hits but was %v", expectedHits, out.Hits.Total))
}
Example #3
0
// currently broken in drone.io
func XXXTestBulkUpdate(t *testing.T) {
	var (
		buffers        = make([]*bytes.Buffer, 0)
		totalBytesSent int
		messageSets    int
	)

	InitTests(true)
	c := NewTestConn()
	c.Port = "9200"
	indexer := c.NewBulkIndexer(3)
	indexer.Sender = func(buf *bytes.Buffer) error {
		messageSets += 1
		totalBytesSent += buf.Len()
		buffers = append(buffers, buf)
		return indexer.Send(buf)
	}
	indexer.Start()

	date := time.Unix(1257894000, 0)
	user := map[string]interface{}{
		"name": "smurfs", "age": 22, "date": date, "count": 1,
	}

	// Lets make sure the data is in the index ...
	_, err := c.Index("users", "user", "5", nil, user)

	// script and params
	data := map[string]interface{}{
		"script": "ctx._source.count += 2",
	}
	err = indexer.Update("users", "user", "5", "", &date, data, true)
	// So here's the deal. Flushing does seem to work, you just have to give the
	// channel a moment to recieve the message ...
	//	<- time.After(time.Millisecond * 20)
	//	indexer.Flush()

	waitFor(func() bool {
		return len(buffers) > 0
	}, 5)

	indexer.Stop()

	assert.T(t, indexer.NumErrors() == 0 && err == nil, fmt.Sprintf("Should not have any errors, bulkErrorCt:%v, err:%v", indexer.NumErrors(), err))

	response, err := c.Get("users", "user", "5", nil)
	assert.T(t, err == nil, fmt.Sprintf("Should not have any errors  %v", err))
	m := make(map[string]interface{})
	json.Unmarshal([]byte(*response.Source), &m)
	newCount := m["count"]
	assert.T(t, newCount.(float64) == 3,
		fmt.Sprintf("Should have update count: %#v ... %#v", m["count"], response))
}
Example #4
0
func TestSearchRange(t *testing.T) {
	c := NewTestConn()

	// now lets filter by a subset of the total time
	out, _ := Search("github").Size("25").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"),
	).Result(c)
	assert.T(t, out != nil && &out.Hits != nil, "Must not have nil results, or hits")
	assert.T(t, out.Hits.Len() == 25, fmt.Sprintf("Should have 25 docs %v", out.Hits.Len()))
	assert.T(t, out.Hits.Total == 92, fmt.Sprintf("Should have total=92 but was %v", out.Hits.Total))
}
Example #5
0
func TestSearchFields(t *testing.T) {
	c := NewTestConn()

	// same as terms, search using fields:
	//    how many different docs have jasmine in repository.name?
	qry := Search("github").Query(
		Query().Fields("repository.name", "jasmine", "", ""),
	)
	out, _ := qry.Result(c)
	expectedDocs := 4
	expectedHits := 4
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total= %v", expectedHits, out.Hits.Total))
}
Example #6
0
func TestSearchTerm(t *testing.T) {
	c := NewTestConn()

	// ok, now lets try searching with term query (specific field/term)
	qry := Search("github").Query(
		Query().Term("repository.name", "jasmine"),
	)
	out, _ := qry.Result(c)
	// how many different docs have jasmine in repository.name?
	expectedDocs := 4
	expectedHits := 4
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total= %v", expectedHits, out.Hits.Total))
}
Example #7
0
func XXXTestBulkErrors(t *testing.T) {
	// lets set a bad port, and hope we get a conn refused error?
	c := NewTestConn()
	c.Port = "27845"
	defer func() {
		c.Port = "9200"
	}()
	indexer := c.NewBulkIndexerErrors(10, 1)
	indexer.Start()
	errorCt := 0
	go func() {
		for i := 0; i < 20; i++ {
			date := time.Unix(1257894000, 0)
			data := map[string]interface{}{"name": "smurfs", "age": 22, "date": date}
			indexer.Index("users", "user", strconv.Itoa(i), "", &date, data, true)
		}
	}()
	var errBuf *ErrorBuffer
	for errBuf = range indexer.ErrorChannel {
		errorCt++
		break
	}
	if errBuf.Buf.Len() > 0 {
		gou.Debug(errBuf.Err)
	}
	assert.T(t, errorCt > 0, fmt.Sprintf("ErrorCt should be > 0 %d", errorCt))
	indexer.Stop()
}
Example #8
0
func TestBulkDelete(t *testing.T) {
	InitTests(true)

	c := NewTestConn()
	indexer := c.NewBulkIndexer(1)
	sentBytes := []byte{}

	indexer.Sender = func(buf *bytes.Buffer) error {
		sentBytes = append(sentBytes, buf.Bytes()...)
		return nil
	}

	indexer.Start()

	indexer.Delete("fake", "fake_type", "1", true)

	indexer.Flush()
	indexer.Stop()

	sent := string(sentBytes)

	expected := `{"delete":{"_index":"fake","_type":"fake_type","_id":"1","refresh":true}}
`
	asExpected := sent == expected
	assert.T(t, asExpected, fmt.Sprintf("Should have sent '%s' but actually sent '%s'", expected, sent))
}
Example #9
0
func TestBulkSmallBatch(t *testing.T) {
	var (
		messageSets int
	)

	InitTests(true)
	c := NewTestConn()

	date := time.Unix(1257894000, 0)
	data := map[string]interface{}{"name": "smurfs", "age": 22, "date": date}

	// Now tests small batches
	indexer := c.NewBulkIndexer(1)
	indexer.BufferDelayMax = 100 * time.Millisecond
	indexer.BulkMaxDocs = 2
	messageSets = 0
	indexer.Sender = func(buf *bytes.Buffer) error {
		messageSets += 1
		return indexer.Send(buf)
	}
	indexer.Start()
	<-time.After(time.Millisecond * 20)

	indexer.Index("users", "user", "2", "", &date, data, true)
	indexer.Index("users", "user", "3", "", &date, data, true)
	indexer.Index("users", "user", "4", "", &date, data, true)
	<-time.After(time.Millisecond * 200)
	//	indexer.Flush()
	indexer.Stop()
	assert.T(t, messageSets == 2, fmt.Sprintf("Should have sent 2 message sets %d", messageSets))

}
Example #10
0
func TestSearchRequest(t *testing.T) {
	c := NewTestConn()

	qry := map[string]interface{}{
		"query": map[string]interface{}{
			"wildcard": map[string]string{"actor": "a*"},
		},
	}
	out, err := c.Search("github", "", nil, qry)
	//log.Println(out)
	assert.T(t, &out != nil && err == nil, t, "Should get docs")
	expectedDocs := 10
	expectedHits := 621
	assert.T(t, out.Hits.Len() == expectedDocs, t, fmt.Sprintf("Should have %v docs but was %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, t, fmt.Sprintf("Should have %v hits but was %v", expectedHits, out.Hits.Total))
}
Example #11
0
func TestFilterRange(t *testing.T) {
	c := NewTestConn()

	// now lets filter range for repositories with more than 100 forks
	out, _ := Search("github").Size("25").Filter(
		Range().Field("repository.forks").From("100"),
	).Result(c)
	if out == nil || &out.Hits == nil {
		t.Fail()
		return
	}
	expectedDocs := 25
	expectedHits := 725

	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have total %v got %v", expectedHits, out.Hits.Total))
}
Example #12
0
func TestSearchFilterQuery(t *testing.T) {
	c := NewTestConn()

	// compound query + filter with query being wildcard
	out, _ := Search("github").Size("25").Query(
		Query().Fields("repository.name", "jas*", "", ""),
	).Filter(
		Filter().Terms("repository.has_wiki", true),
	).Result(c)
	if out == nil || &out.Hits == nil {
		t.Fail()
		return
	}

	expectedDocs := 7
	expectedTotal := 7
	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))
}
Example #13
0
func TestSearchSimple(t *testing.T) {
	c := NewTestConn()

	// searching without faceting
	qry := Search("github").Pretty().Query(
		Query().Search("add"),
	)
	out, _ := qry.Result(c)
	// how many different docs used the word "add"
	expectedDocs := 10
	expectedHits := 494
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total= %v", expectedHits, out.Hits.Total))

	// now the same result from a "Simple" search
	out, _ = Search("github").Search("add").Result(c)
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total= %v", expectedHits, out.Hits.Total))
}
Example #14
0
func TestBulkIndexerBasic(t *testing.T) {
	testIndex := "users"
	var (
		buffers        = make([]*bytes.Buffer, 0)
		totalBytesSent int
		messageSets    int
	)

	InitTests(true)
	c := NewTestConn()

	c.DeleteIndex(testIndex)

	indexer := c.NewBulkIndexer(3)
	indexer.Sender = func(buf *bytes.Buffer) error {
		messageSets += 1
		totalBytesSent += buf.Len()
		buffers = append(buffers, buf)
		// log.Printf("buffer:%s", string(buf.Bytes()))
		return indexer.Send(buf)
	}
	indexer.Start()

	date := time.Unix(1257894000, 0)
	data := map[string]interface{}{
		"name": "smurfs",
		"age":  22,
		"date": "yesterday",
	}

	err := indexer.Index(testIndex, "user", "1", "", &date, data, true)

	waitFor(func() bool {
		return len(buffers) > 0
	}, 5)
	// part of request is url, so lets factor that in
	//totalBytesSent = totalBytesSent - len(*eshost)
	assert.T(t, len(buffers) == 1, fmt.Sprintf("Should have sent one operation but was %d", len(buffers)))
	assert.T(t, indexer.NumErrors() == 0 && err == nil, fmt.Sprintf("Should not have any errors. NumErrors: %v, err: %v", indexer.NumErrors(), err))
	expectedBytes := 144
	assert.T(t, totalBytesSent == expectedBytes, fmt.Sprintf("Should have sent %v bytes but was %v", expectedBytes, totalBytesSent))

	err = indexer.Index(testIndex, "user", "2", "", nil, data, true)
	<-time.After(time.Millisecond * 10) // we need to wait for doc to hit send channel
	// this will test to ensure that Flush actually catches a doc
	indexer.Flush()
	totalBytesSent = totalBytesSent - len(*eshost)
	assert.T(t, err == nil, fmt.Sprintf("Should have nil error  =%v", err))
	assert.T(t, len(buffers) == 2, fmt.Sprintf("Should have another buffer ct=%d", len(buffers)))

	assert.T(t, indexer.NumErrors() == 0, fmt.Sprintf("Should not have any errors %d", indexer.NumErrors()))
	expectedBytes = 250 // with refresh
	assert.T(t, closeInt(totalBytesSent, expectedBytes), fmt.Sprintf("Should have sent %v bytes but was %v", expectedBytes, totalBytesSent))

	indexer.Stop()
}
Example #15
0
func TestSearchMissingExists(t *testing.T) {
	c := NewTestConn()

	// search for docs that are missing repository.name
	qry := Search("github").Filter(
		Filter().Exists("repository.name"),
	)
	out, _ := qry.Result(c)
	expectedDocs := 10
	expectedTotal := 7695
	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))

	qry = Search("github").Filter(
		Filter().Missing("repository.name"),
	)
	out, _ = qry.Result(c)
	expectedDocs = 10
	expectedTotal = 390
	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))
}
Example #16
0
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"))))

}
Example #17
0
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)
	// }
}
Example #18
0
func TestQueryString(t *testing.T) {
	// Test nil argument
	s, err := Escape(nil)
	assert.T(t, s == "" && err == nil, fmt.Sprintf("Nil should not fail and yield empty string"))

	// Test single string argument
	s, err = Escape(map[string]interface{}{"foo": "bar"})
	exp := "foo=bar"
	assert.T(t, s == exp && err == nil, fmt.Sprintf("Expected %s, got: %s", exp, s))

	// Test single int argument
	s, err = Escape(map[string]interface{}{"foo": 1})
	exp = "foo=1"
	assert.T(t, s == exp && err == nil, fmt.Sprintf("Expected %s, got: %s", exp, s))

	// Test single float argument
	s, err = Escape(map[string]interface{}{"foo": 3.141592})
	exp = "foo=3.141592"
	assert.T(t, s == exp && err == nil, fmt.Sprintf("Expected %s, got: %s", exp, s))

	// Test single []string argument
	s, err = Escape(map[string]interface{}{"foo": []string{"bar", "baz"}})
	exp = "foo=bar%2Cbaz"
	assert.T(t, s == exp && err == nil, fmt.Sprintf("Expected %s, got: %s", exp, s))

	// Test combination of all arguments
	s, err = Escape(map[string]interface{}{
		"foo":  "bar",
		"bar":  1,
		"baz":  3.141592,
		"test": []string{"a", "b"},
	})
	// url.Values also orders arguments alphabetically.
	exp = "bar=1&baz=3.141592&foo=bar&test=a%2Cb"
	assert.T(t, s == exp && err == nil, fmt.Sprintf("Expected %s, got: %s", exp, s))

	// Test invalid datatype
	s, err = Escape(map[string]interface{}{"foo": []int{}})
	assert.T(t, err != nil, fmt.Sprintf("Expected err to not be nil"))
}
Example #19
0
func TestFilters(t *testing.T) {
	c := NewTestConn()

	// search for docs that are missing repository.name
	qry := Search("github").Filter(
		Filter().Exists("repository.name"),
	)
	out, err := qry.Result(c)
	assert.T(t, err == nil, t, "should not have error")
	expectedDocs := 10
	expectedHits := 7695
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))
	qry = Search("github").Filter(
		Filter().Missing("repository.name"),
	)
	expectedHits = 390
	out, _ = qry.Result(c)
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))

	//actor_attributes: {type: "User",
	qry = Search("github").Filter(
		Filter().Terms("actor_attributes.location", "portland"),
	)
	out, _ = qry.Result(c)
	expectedDocs = 10
	expectedHits = 71
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))

	/*
		Should this be an AND by default?
	*/
	qry = Search("github").Filter(
		Filter().Terms("actor_attributes.location", "portland"),
		Filter().Terms("repository.has_wiki", true),
	)
	out, err = qry.Result(c)
	expectedDocs = 10
	expectedHits = 44
	assert.T(t, err == nil, t, "should not have error")
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))

	// NOW, lets try with two query calls instead of one
	qry = Search("github").Filter(
		Filter().Terms("actor_attributes.location", "portland"),
	)
	qry.Filter(
		Filter().Terms("repository.has_wiki", true),
	)
	out, err = qry.Result(c)
	//gou.Debug(out)
	assert.T(t, err == nil, t, "should not have error")
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))

	qry = Search("github").Filter(
		"or",
		Filter().Terms("actor_attributes.location", "portland"),
		Filter().Terms("repository.has_wiki", true),
	)
	out, err = qry.Result(c)
	expectedHits = 6676
	assert.T(t, err == nil, t, "should not have error")
	assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
	assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))
}
Example #20
0
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"))))

}
func TestGetAll(t *testing.T) {
	InitTests(true)
	c := NewTestConn()
	nodesInfo, err := c.AllNodesInfo()
	assert.T(t, err == nil, fmt.Sprintf("should not have gotten error, received: %v", err))
	assert.T(t, nodesInfo.ClusterName != "", fmt.Sprintf("clustername should have been not empty. received: %q", nodesInfo.ClusterName))
	for _, node := range nodesInfo.Nodes {
		assert.T(t, node.Settings != nil, fmt.Sprintf("Settings should not have been null"))
		assert.T(t, node.OS != nil, fmt.Sprintf("OS should not have been null"))
		assert.T(t, node.Process != nil, fmt.Sprintf("Process should not have been null"))
		assert.T(t, node.JVM != nil, fmt.Sprintf("JVM should not have been null"))
		assert.T(t, node.ThreadPool != nil, fmt.Sprintf("ThreadPool should not have been null"))
		assert.T(t, node.Network != nil, fmt.Sprintf("Network should not have been null"))
		assert.T(t, node.Transport != nil, fmt.Sprintf("Transport should not have been null"))
		assert.T(t, node.Http != nil, fmt.Sprintf("Http should not have been null"))
		assert.T(t, node.Plugins != nil, fmt.Sprintf("Plugins should not have been null"))
	}
}
Example #22
0
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")))

}