示例#1
0
// for testing
func main() {
	flag.Parse()
	log.SetFlags(log.Ltime | log.Lshortfile)
	api.Domain = *eshost
	response, _ := core.Index(true, "twitter", "tweet", "1", NewTweet("kimchy", "Search is cool"))
	indices.Flush()
	log.Printf("Index OK: %v", response.Ok)
	searchresponse, err := core.SearchRequest(true, "twitter", "tweet", "{\"query\" : {\"term\" : { \"user\" : \"kimchy\" }}}", "")
	if err != nil {
		log.Println("error during search:" + err.Error())
		log.Fatal(err)
	}
	// try marshalling to tweet type
	var t Tweet
	json.Unmarshal(searchresponse.Hits.Hits[0].Source, t)
	log.Printf("Search Found: %s", t)
	response, _ = core.Get(true, "twitter", "tweet", "1")
	log.Printf("Get: %v", response.Exists)
	response, _ = core.Exists(true, "twitter", "tweet", "1")
	log.Printf("Exists: %v", response.Exists)
	indices.Flush()
	countResponse, _ := core.Count(true, "twitter", "tweet")
	log.Printf("Count: %v", countResponse.Count)
	response, _ = core.Delete(true, "twitter", "tweet", "1", -1, "")
	log.Printf("Delete OK: %v", response.Ok)
	response, _ = core.Get(true, "twitter", "tweet", "1")
	log.Printf("Get: %v", response.Exists)

	healthResponse, _ := cluster.Health(true)
	log.Printf("Health: %v", healthResponse.Status)

	cluster.State("transient", "discovery.zen.minimum_master_nodes", 2)

}
示例#2
0
func TestSearchRequest(t *testing.T) {
	qry := map[string]interface{}{
		"query": map[string]interface{}{
			"wildcard": map[string]string{"actor": "a*"},
		},
	}
	out, err := core.SearchRequest(true, "github", "", qry, "")
	//log.Println(out)
	Assert(&out != nil && err == nil, t, "Should get docs")
	Assert(out.Hits.Total == 588 && out.Hits.Len() == 10, t, "Should have 588 hits but was %v", out.Hits.Total)
}
示例#3
0
文件: search.go 项目: jk-cbfs/cbugg
// powers the bug search
func searchBugs(w http.ResponseWriter, r *http.Request) {

	api.Domain = *esHost
	api.Port = *esPort
	api.Protocol = *esScheme

	from := 0
	var err error
	if r.FormValue("from") != "" {
		from, err = strconv.Atoi(r.FormValue("from"))
		if err != nil {
			log.Printf("Invalid value for from: %v", r.FormValue("from"))
			from = 0
		}
	}

	size := 10
	if r.FormValue("size") != "" {
		size, err = strconv.Atoi(r.FormValue("size"))
		if err != nil {
			log.Printf("Invalid value for size: %v", r.FormValue("size"))
			size = 10
		}
	}

	sortItems := []SortItem{}
	if r.FormValue("sort") != "" {
		sortItems = append(sortItems, buildSortItemFromString(r.FormValue("sort")))

		// if they aren't sorting by +/- _score
		// add that after what they do want to sort by
		// this ensure score is still caclulated
		// and offers secondary sort by the score
		if !strings.HasSuffix(r.FormValue("sort"), "_score") {
			sortItems = append(sortItems, buildSortItemFromString("-_score"))
		}
	}

	filterComponents := getDefaultFilterComponents(r)

	if r.FormValue("status") != "" {
		statusFilter := buildTermsFilter("doc.status", strings.Split(r.FormValue("status"), ","), "")
		filterComponents = append(filterComponents, statusFilter)
	}

	if r.FormValue("tags") != "" {
		tagsFilter := buildTermsFilter("doc.tags", strings.Split(r.FormValue("tags"), ","), "and")
		filterComponents = append(filterComponents, tagsFilter)
	}

	if r.FormValue("modified") != "" {

		now := time.Now()
		var dateRange Range
		switch r.FormValue("modified") {
		case "lt7":
			sevenDaysAgo := now.Add(time.Duration(24) * time.Hour * -7)
			dateRange = buildRange(sevenDaysAgo, nil)
		case "7to30":
			sevenDaysAgo := now.Add(time.Duration(24) * time.Hour * -7)
			thirtyDaysAgo := now.Add(time.Duration(24) * time.Hour * -30)
			dateRange = buildRange(thirtyDaysAgo, sevenDaysAgo)
		case "gt30":
			thirtyDaysAgo := now.Add(time.Duration(24) * time.Hour * -30)
			dateRange = buildRange(nil, thirtyDaysAgo)

		}
		modifiedFilter := buildRangeFilter("doc.modified_at", dateRange)
		filterComponents = append(filterComponents, modifiedFilter)
	}

	filter := buildAndFilter(filterComponents)

	statusFacet := buildTermsFacet("doc.status", filter, 5)
	tagsFacet := buildTermsFacet("doc.tags", filter, 5)
	lastModifiedFacet := buildLastModifiedFacet("doc.modified_at", filter)

	facets := Facets{
		"statuses":      statusFacet,
		"tags":          tagsFacet,
		"last_modified": lastModifiedFacet,
	}

	// all the queries that should be matched
	shouldQueries := []Query{}

	// default to match all query
	insideQuery := buildMatchAllQuery()

	// if they actually provided a query string, run that instead
	if r.FormValue("query") != "" {
		insideQuery = buildQueryStringQuery(r.FormValue("query"))

		// only add these child queries if we actually have a query string
		childTypesToQuery := []string{"comment", "attachment"}
		for _, typ := range childTypesToQuery {
			queryComponent := buildHashChildQuery(typ, insideQuery)
			shouldQueries = append(shouldQueries, queryComponent)
		}
	}

	shouldQueries = append(shouldQueries, insideQuery)

	booleanQuery := buildBoolQuery(nil, shouldQueries, nil, 1)

	query := buildTopLevelQuery(booleanQuery, filter, facets, sortItems, from, size)

	if *debugEs {
		queryJson, err := json.Marshal(query)
		if err == nil {
			log.Printf("Elasticsearch query: %v", string(queryJson))
		}
	}

	searchresponse, err := core.SearchRequest(false, *esIndex, "", query, "")
	if err != nil {
		showError(w, r, err.Error(), 500)
		return
	}

	if *debugEs {
		searchresponseJson, err := json.Marshal(searchresponse)
		if err == nil {
			log.Printf("Elasticsearch response: %v", string(searchresponseJson))
		}
	}

	ourresponse := convertElasticSearchResponse(searchresponse)

	jres, err := json.Marshal(ourresponse)
	if err != nil {
		showError(w, r, err.Error(), 500)
		return
	}
	w.Write(jres)
}
示例#4
0
文件: search.go 项目: jk-cbfs/cbugg
// powers the bug similarity feature when entering new bugs
func findSimilarBugs(w http.ResponseWriter, r *http.Request) {

	api.Domain = *esHost
	api.Port = *esPort
	api.Protocol = *esScheme

	if r.FormValue("query") != "" {

		filterComponents := getDefaultFilterComponents(r)
		matchFilter := buildAndFilter(filterComponents)

		activeBugsFilter := buildTermsFilter("doc.status", []string{"inbox", "new", "open"}, "")
		inactiveBugsFilter := buildTermsFilter("doc.status", []string{"resolved", "closed"}, "")

		boostFilters := Filters{
			map[string]interface{}{
				"filter": activeBugsFilter,
				"boost":  10,
			},
			map[string]interface{}{
				"filter": inactiveBugsFilter,
				"boost":  1,
			},
		}

		queryStringQuery := buildQueryStringQuery(r.FormValue("query"))
		customScoreQuery := buildCustomFiltersScoreQuery(queryStringQuery, boostFilters, "first")
		query := buildTopLevelQuery(customScoreQuery, matchFilter, nil, nil, 0, 10)

		if *debugEs {
			queryJson, err := json.Marshal(query)
			if err == nil {
				log.Printf("Elasticsearch query: %v", string(queryJson))
			}
		}

		searchresponse, err := core.SearchRequest(false, *esIndex, "", query, "")
		if err != nil {
			showError(w, r, err.Error(), 500)
			return
		}

		if *debugEs {
			searchresponseJson, err := json.Marshal(searchresponse)
			if err == nil {
				log.Printf("Elasticsearch response: %v", string(searchresponseJson))
			}
		}

		ourresponse := convertElasticSearchResponse(searchresponse)

		jres, err := json.Marshal(ourresponse)
		if err != nil {
			showError(w, r, err.Error(), 500)
			return
		}
		w.Write(jres)
		return
	}

	showError(w, r, "Search query cannot be empty", 500)

}