Beispiel #1
0
func TestSearchSortOrder(t *testing.T) {
	c := NewConn()

	// 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 := 8084
	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")))

}
Beispiel #2
0
func TestSearchFacetRange(t *testing.T) {
	c := NewConn()

	// 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"))))

}
Beispiel #3
0
func TestSearchSortOrder(t *testing.T) {

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

	// how many different docs used the word "add", during entire time range
	assert.T(t, out.Hits.Len() == 10, fmt.Sprintf("Should have 10 docs %v", out.Hits.Len()))
	assert.T(t, out.Hits.Total == 8084, fmt.Sprintf("Should have 8084 total= %v", out.Hits.Total))
	h1 := gou.NewJsonHelper(out.Hits.Hits[0].Source)
	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()
	// how many different docs used the word "add", during entire time range
	assert.T(t, out.Hits.Len() == 10, fmt.Sprintf("Should have 10 docs %v", out.Hits.Len()))
	assert.T(t, out.Hits.Total == 8084, fmt.Sprintf("Should have 8084 total= %v", out.Hits.Total))
	h2 := gou.NewJsonHelper(out.Hits.Hits[0].Source)
	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()
	//log.Println(out)
	//log.Println(err)
	// how many different docs used the word "add", during entire time range
	assert.T(t, out.Hits.Len() == 5, fmt.Sprintf("Should have 5 docs %v", out.Hits.Len()))
	assert.T(t, out.Hits.Total == 734, fmt.Sprintf("Should have 734 total= %v", out.Hits.Total))
	h3 := gou.NewJsonHelper(out.Hits.Hits[0].Source)
	assert.T(t, h3.Int("repository.watchers") == 8659,
		fmt.Sprintf("Should have 8659 watchers= %v", h3.Int("repository.watchers")))

}
Beispiel #4
0
func TestSearchFacetRange(t *testing.T) {
	// 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()
	u.Assert(out != nil && err == nil, t, "Should have output")

	if out == nil {
		t.Fail()
		return
	}
	//log.Println(string(out.Facets))
	h := u.NewJsonHelper(out.Facets)
	// how many different docs used the word "add", during entire time range
	u.Assert(h.Int("actor.total") == 521, t, "Should have 521 results %v", h.Int("actor.total"))
	// make sure size worked
	u.Assert(len(h.List("actor.terms")) == 366, t, "Should have 366 unique userids, %v", 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()
	u.Assert(out != nil && err == nil, t, "Should have output")

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

}
func TestFacetRegex(t *testing.T) {
	// This is a possible solution for auto-complete
	out, _ := Search("github").Size("0").Facet(
		Facet().Regex("repository.name", "no.*").Size("8"),
	).Result()
	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)
	// }
}
Beispiel #6
0
func main() {
	flag.Parse()
	goauthcon = &oauth.OAuthConsumer{
		Service:          "twitter",
		RequestTokenURL:  "https://api.twitter.com/oauth/request_token",
		AccessTokenURL:   "https://api.twitter.com/oauth/access_token",
		AuthorizationURL: "https://api.twitter.com/oauth/authorize",
		ConsumerKey:      *ck,
		ConsumerSecret:   *cs,
		CallBackURL:      "oob",
	}

	s, rt, err := goauthcon.GetRequestAuthorizationURL()
	if err != nil {
		fmt.Println(err)
		return
	}
	var pin string

	fmt.Printf("Open %s In your browser.\n Allow access and then enter the PIN number\n", s)
	fmt.Printf("PIN Number: ")
	fmt.Scanln(&pin)

	at := goauthcon.GetAccessToken(rt.Token, pin)
	fmt.Printf("\n\n\ttoken=%s  secret=%s \n\n\n", at.Token, at.Secret)
	r, err := goauthcon.Get("https://api.twitter.com/1.1/account/verify_credentials.json", nil, at)

	if err != nil {
		fmt.Println(err)
		return
	}
	if r != nil && r.Body != nil && r.StatusCode == 200 {
		userData, _ := ioutil.ReadAll(r.Body)
		if len(userData) > 0 {
			jh := u.NewJsonHelper(userData)
			fmt.Println(string(jh.PrettyJson()))
		}
	} else {
		fmt.Println(r)
		fmt.Println("Twitter verify failed")
	}

}
func TestFacetRegex(t *testing.T) {

	c := NewTestConn()
	PopulateTestDB(t, c)
	defer TearDownTestDB(c)

	Convey("Facted regex query", t, func() {

		// This is a possible solution for auto-complete
		out, err := Search("oilers").Size("0").Facet(
			Facet().Regex("name", "[jk].*").Size("8"),
		).Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)

		// Debug(string(out.Facets))
		fh := gou.NewJsonHelper([]byte(out.Facets))
		facets := fh.Helpers("/name/terms")
		So(err, ShouldBeNil)
		So(facets, ShouldNotBeNil)
		So(len(facets), ShouldEqual, 4)
	})
}
Beispiel #8
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
		      }.....]
		    }
		 }

	*/
	qry := Search("github").Pretty().Facet(
		Facet().Fields("type").Size("25"),
	).Query(
		Query().All(),
	).Size("1")
	out, err := qry.Result()
	//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)
	assert.T(t, h.Int("type.total") == 8084, fmt.Sprintf("Should have 8084 results %v", h.Int("type.total")))
	assert.T(t, len(h.List("type.terms")) == 16, fmt.Sprintf("Should have 16 event types, %v", len(h.List("type.terms"))))

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

	// still same doc count
	assert.T(t, h.Int("type.total") == 8084, fmt.Sprintf("Should have 8084 results %v", h.Int("type.total")))
	// make sure size worked
	assert.T(t, len(h.List("type.terms")) == 10, fmt.Sprintf("Should have 10 event types, %v", 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()
	h = gou.NewJsonHelper(out.Facets)
	//log.Println(string(out.Facets))
	// still same doc count
	assert.T(t, h.Int("type.total") == 685, fmt.Sprintf("Should have 685 results %v", 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()
	h = gou.NewJsonHelper(out.Facets)
	//log.Println(string(out.Facets))
	// still same doc count
	assert.T(t, h.Int("type.total") == 4941, fmt.Sprintf("Should have 4941 results %v", h.Int("type.total")))
	// make sure we now have 2 types
	assert.T(t, len(h.List("type.terms")) == 2, fmt.Sprintf("Should have 2 event types, %v", 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()
	h = gou.NewJsonHelper(out.Facets)
	// still same doc count
	assert.T(t, h.Int("actor.total") == 5158, t, "Should have 5158 results %v", h.Int("actor.total"))
	// make sure size worked
	assert.T(t, len(h.List("actor.terms")) == 500, t, "Should have 500 users, %v", len(h.List("actor.terms")))

}
func TestSearch(t *testing.T) {

	c := NewTestConn()
	PopulateTestDB(t, c)
	defer TearDownTestDB(c)

	Convey("Wildcard request query", t, func() {

		qry := map[string]interface{}{
			"query": map[string]interface{}{
				"wildcard": map[string]string{"name": "*hu*"},
			},
		}
		out, err := c.Search("oilers", "", nil, qry)

		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits, ShouldNotBeNil)
		So(out.Hits.Total, ShouldEqual, 3)
	})

	Convey("Simple search", t, func() {

		// searching without faceting
		qry := Search("oilers").Pretty().Query(
			Query().Search("dave"),
		)

		// how many different docs used the word "dave"
		out, err := qry.Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits, ShouldNotBeNil)
		So(out.Hits.Total, ShouldEqual, 2)

		out, _ = Search("oilers").Search("dave").Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits, ShouldNotBeNil)
		So(out.Hits.Total, ShouldEqual, 2)
	})

	Convey("URL Request query string", t, func() {

		out, err := c.SearchUri("oilers", "", map[string]interface{}{"q": "pos:LW"})

		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits, ShouldNotBeNil)
		So(out.Hits.Total, ShouldEqual, 3)
	})

	//	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
	//	      }.....]
	//	    }
	//	 }

	Convey("Facet search simple", t, func() {

		qry := Search("oilers").Pretty().Facet(
			Facet().Fields("teams").Size("4"),
		).Query(
			Query().All(),
		).Size("1")
		out, err := qry.Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)

		h := gou.NewJsonHelper(out.Facets)
		So(h.Int("teams.total"), ShouldEqual, 37)
		So(h.Int("teams.missing"), ShouldEqual, 0)
		So(len(h.List("teams.terms")), ShouldEqual, 4)

		// change the size
		qry.FacetVal.Size("20")
		out, err = qry.Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)

		h = gou.NewJsonHelper(out.Facets)
		So(h.Int("teams.total"), ShouldEqual, 37)
		So(len(h.List("teams.terms")), ShouldEqual, 11)

	})

	Convey("Facet search with type", t, func() {

		out, err := Search("oilers").Type("heyday").Pretty().Facet(
			Facet().Fields("teams").Size("4"),
		).Query(
			Query().All(),
		).Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)

		h := gou.NewJsonHelper(out.Facets)
		So(h.Int("teams.total"), ShouldEqual, 37)
		So(len(h.List("teams.terms")), ShouldEqual, 4)
	})

	Convey("Facet search with wildcard", t, func() {

		qry := Search("oilers").Pretty().Facet(
			Facet().Fields("teams").Size("20"),
		).Query(
			Query().Search("*w*"),
		)
		out, err := qry.Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)

		h := gou.NewJsonHelper(out.Facets)
		So(h.Int("teams.total"), ShouldEqual, 20)
		So(len(h.List("teams.terms")), ShouldEqual, 7)
	})

	Convey("Facet search with range", t, func() {

		qry := Search("oilers").Pretty().Facet(
			Facet().Fields("teams").Size("20"),
		).Query(
			Query().Range(
				Range().Field("dob").From("19600101").To("19621231"),
			).Search("*w*"),
		)
		out, err := qry.Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)

		h := gou.NewJsonHelper(out.Facets)
		So(h.Int("teams.total"), ShouldEqual, 12)
		So(len(h.List("teams.terms")), ShouldEqual, 5)
	})

	Convey("Search query with terms", t, func() {

		qry := Search("oilers").Query(
			Query().Term("teams", "NYR"),
		)
		out, err := qry.Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits.Len(), ShouldEqual, 4)
		So(out.Hits.Total, ShouldEqual, 4)
	})

	Convey("Search query with fields", t, func() {

		qry := Search("oilers").Query(
			Query().Fields("teams", "NYR", "", ""),
		)
		out, err := qry.Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits.Len(), ShouldEqual, 4)
		So(out.Hits.Total, ShouldEqual, 4)
	})

	Convey("Search query with fields exist and missing", t, func() {

		qry := Search("oilers").Filter(
			Filter().Exists("PIM"),
		)
		out, err := qry.Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits.Len(), ShouldEqual, 2)
		So(out.Hits.Total, ShouldEqual, 2)

		qry = Search("oilers").Filter(
			Filter().Missing("PIM"),
		)
		out, err = qry.Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits.Len(), ShouldEqual, 10)
		So(out.Hits.Total, ShouldEqual, 12)
	})

	Convey("Search with query and filter", t, func() {

		out, err := Search("oilers").Size("25").Query(
			Query().Fields("name", "*d*", "", ""),
		).Filter(
			Filter().Terms("teams", "STL"),
		).Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits.Len(), ShouldEqual, 2)
		So(out.Hits.Total, ShouldEqual, 2)
	})

	Convey("Search with range", t, func() {

		out, err := Search("oilers").Size("25").Query(
			Query().Range(
				Range().Field("dob").From("19600101").To("19621231"),
			).Search("*w*"),
		).Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits.Len(), ShouldEqual, 4)
		So(out.Hits.Total, ShouldEqual, 4)
	})

	Convey("Search with sorting desc", t, func() {

		out, err := Search("oilers").Pretty().Query(
			Query().All(),
		).Sort(
			Sort("dob").Desc(),
		).Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits.Len(), ShouldEqual, 10)
		So(out.Hits.Total, ShouldEqual, 14)

		b, err := out.Hits.Hits[0].Source.MarshalJSON()
		h1 := gou.NewJsonHelper(b)
		So(h1.String("name"), ShouldEqual, "Grant Fuhr")
	})

	Convey("Search with sorting asc", t, func() {

		out, err := Search("oilers").Pretty().Query(
			Query().All(),
		).Sort(
			Sort("dob"),
		).Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits.Len(), ShouldEqual, 10)
		So(out.Hits.Total, ShouldEqual, 14)

		b, err := out.Hits.Hits[0].Source.MarshalJSON()
		h1 := gou.NewJsonHelper(b)
		So(h1.String("name"), ShouldEqual, "Pat Hughes")
	})

	Convey("Search with sorting desc with query", t, func() {

		out, err := Search("oilers").Pretty().Query(
			Query().Search("*w*"),
		).Sort(
			Sort("dob").Desc(),
		).Result(c)
		So(err, ShouldBeNil)
		So(out, ShouldNotBeNil)
		So(out.Hits.Len(), ShouldEqual, 8)
		So(out.Hits.Total, ShouldEqual, 8)

		b, err := out.Hits.Hits[0].Source.MarshalJSON()
		h1 := gou.NewJsonHelper(b)
		So(h1.String("name"), ShouldEqual, "Wayne Gretzky")
	})
}