Пример #1
0
func TestClusterSearch(t *testing.T) {
	expected := es.SearchResponse{Took: 134}
	server := newServer(func(e *json.Encoder) {
		e.Encode(expected)
	})
	defer server.Close()

	cluster := newCluster(clusterSpec(server.URL, "cluster1", "on"))
	defer cluster.Shutdown()

	got, err := cluster.Search(es.SearchRequest{
		es.SearchParams{},
		es.MatchAllQuery(),
	})

	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(expected, got) {
		t.Fatal("got != epxected")
	}
}
Пример #2
0
func TestMultiClusterSearchDarkMode(t *testing.T) {
	expected := es.SearchResponse{Took: 134}

	cluster1 := newServer(func(e *json.Encoder) {
		time.Sleep(10 * time.Millisecond)
		e.Encode(expected)
	})
	defer cluster1.Close()
	cluster2 := newServer(func(e *json.Encoder) {
		e.Encode(es.SearchResponse{Took: 1340})
	})
	defer cluster2.Close()

	mc := esmc.MultiCluster{
		newCluster(clusterSpec(cluster1.URL, "cluster1", "on")),
		newCluster(clusterSpec(cluster2.URL, "cluster2", "dark")),
	}
	defer func() {
		for _, c := range mc {
			c.Shutdown()
		}
	}()

	got, err := mc.Search(es.SearchRequest{
		es.SearchParams{},
		es.MatchAllQuery(),
	})

	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(expected, got) {
		t.Fatal("got != epxected")
	}
}
Пример #3
0
func TestMultiSearch(t *testing.T) {
	indices := []string{"index1", "index2"}
	c := newCluster(t, indices, map[string]interface{}{
		"/index1/foo/1": map[string]string{
			"user":        "******",
			"description": "index=index1 type=foo id=1 user=alice",
		},
		"/index2/bar/2": map[string]string{
			"user":        "******",
			"description": "index=index2 type=bar id=2 user=bob",
		},
	})
	defer c.Shutdown()
	defer deleteIndices(t, indices) // comment out to leave data after test

	q1 := es.QueryWrapper(es.TermQuery(es.TermQueryParams{
		Query: &es.Wrapper{
			Name:    "user",
			Wrapped: "alice",
		},
	}))
	q2 := es.QueryWrapper(es.TermQuery(es.TermQueryParams{
		Query: &es.Wrapper{
			Name:    "user",
			Wrapped: "bob",
		},
	}))
	q3 := es.QueryWrapper(es.MatchAllQuery())

	request := es.MultiSearchRequest{
		Requests: []es.SearchRequest{
			es.SearchRequest{
				es.SearchParams{
					Indices: []string{"index1"},
					Types:   []string{"foo"},
				},
				q1,
			},
			es.SearchRequest{
				es.SearchParams{
					Indices: []string{"index2"},
					Types:   []string{"bar"},
				},
				q2,
			},
			es.SearchRequest{
				es.SearchParams{
					Indices: []string{}, // "index1", "index2" is not supported (!)
					Types:   []string{}, // "type1", "type2" is not supported (!)
				},
				q3,
			},
		},
	}

	response, err := c.MultiSearch(request)
	if err != nil {
		t.Fatal(err)
	}

	if expected, got := 3, len(response.Responses); expected != got {
		t.Fatalf("expected %d response(s), got %d", expected, got)
	}

	r1 := response.Responses[0]
	if r1.Error != "" {
		t.Fatalf("response 1: %s", r1.Error)
	}
	if expected, got := 1, r1.HitsWrapper.Total; expected != got {
		t.Fatalf("response 1: expected %d hit(s), got %d", expected, got)
	}
	buf, _ := json.Marshal(r1)
	t.Logf("response 1 OK: %s", buf)

	r2 := response.Responses[1]
	if r2.Error != "" {
		t.Fatalf("response 2: %s", r1.Error)
	}
	if expected, got := 1, r2.HitsWrapper.Total; expected != got {
		t.Fatalf("response 2: expected %d hit(s), got %d", expected, got)
	}
	buf, _ = json.Marshal(r2)
	t.Logf("response 2 OK: %s", buf)

	r3 := response.Responses[2]
	if r3.Error != "" {
		t.Fatalf("response 3: %s", r1.Error)
	}
	if expected, got := 2, r3.HitsWrapper.Total; expected != got {
		t.Fatalf("response 3: expected %d hit(s), got %d", expected, got)
	}
	buf, _ = json.Marshal(r3)
	t.Logf("response 3 OK: %s", buf)
}