Exemple #1
0
// RetrieveContactcollectionViaElastic calls the RetrieveContacts method from Contacts via RPC (Search client). Returns results via jsonapi.
func RetrieveContactCollectionViaElastic(w http.ResponseWriter, r *http.Request) {

	var (
		contactClient = rpcClient(r, "ContactClient")
		args          = models.SearchArgs{}
		reply         = models.SearchReply{}
	)
	args.Search = new(models.Search)
	args.Search.Query = router.Context(r).Param("query")

	err := contactClient.Call("Search.RetrieveContacts", args, &reply)
	if err != nil {
		logs.Error(err)
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	if reply.Contacts == nil {
		reply.Contacts = make([]models.Contact, 0)
	}

	Success(w, r, views.Contacts{Contacts: reply.Contacts}, http.StatusOK)
}
Exemple #2
0
// Implementation of elasticsearch, gets the query terms from the request's url and calls the SearchContacts method from Contacts via RPC (Search client). Returns results via jsonapi.
func SearchContact(w http.ResponseWriter, r *http.Request) {
	var (
		contactClient = rpcClient(r, "ContactClient")
		args          = models.SearchArgs{}
		reply         = models.SearchReply{}
	)

	args.Search = new(models.Search)
	//args.Search.Query = router.Context(r).Param("query")

	if err := Request(args.Search, r); err != nil {
		logs.Debug(err)
		Fail(w, r, map[string]interface{}{"contact": err.Error()}, http.StatusBadRequest)
		return
	}

	// choix de la fonction selon le type de recherche souhaité
	var filtre = ""

	if args.Search.Fields[1] == "address" {
		// recherche sur les addresses
		filtre = "Search.SearchAddressesAggs"

	} else if args.Search.Fields[1] == "geoloc" {
		// recherche par geoloc avec tri par distance
		filtre = "Search.SearchContactsGeoloc"

	} else {
		// recherche sur les contacts
		filtre = "Search.SearchContacts"
	}

	// appel des fonctions
	err := contactClient.Call(filtre, args, &reply)

	if err != nil {
		logs.Error(err)
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	// traitement des résultats selon le type de recherche --------------------------------------------
	if args.Search.Fields[1] == "address" {

		if reply.AddressAggs == nil {
			reply.AddressAggs = make([]models.AddressAggReply, 0)
		}
		Success(w, r, views.AddressAggs{AddressAggs: reply.AddressAggs}, http.StatusOK)

	} else if args.Search.Fields[1] == "geoloc" {

		if reply.Contacts == nil {
			reply.Contacts = make([]models.Contact, 0)
		}

		var cs models.AddressAggReply
		var temp_contact models.Contact
		var compteur int = 0
		var size_nb_address_aggrege int

		if len(args.Search.Fields) == 3 {
			j, err := strconv.Atoi(args.Search.Fields[2])
			if err == nil {
				size_nb_address_aggrege = j
			} else {
				// par défaut
				size_nb_address_aggrege = 50
			}
		} else {
			// par défaut
			size_nb_address_aggrege = 50
		}

		for pos, res_contact := range reply.Contacts {
			//control si nous avons le nombre de fiche résultats suffisants:
			if compteur == size_nb_address_aggrege {
				break
			}
			// si il y'a égalité
			if temp_contact.Address.Location == res_contact.Address.Location { //égalité
				cs.Contacts = append(cs.Contacts, res_contact)
				//si il n'y a pas d'égalité
			} else { //pas d'égalité
				if pos == 0 { //première itération
					cs.Contacts = append(cs.Contacts, res_contact)
				} else { //pas la première itération
					reply.AddressAggs = append(reply.AddressAggs, cs)
					compteur = compteur + 1
					// init de cs
					var cs2 models.AddressAggReply
					cs = cs2
					cs.Contacts = append(cs.Contacts, res_contact)
				}
			}
			temp_contact = res_contact
		}
		reply.AddressAggs = append(reply.AddressAggs, cs)
		Success(w, r, views.AddressAggs{AddressAggs: reply.AddressAggs}, http.StatusOK)

	} else { // pas geoloc, pas address
		if reply.Contacts == nil {
			reply.Contacts = make([]models.Contact, 0)
		}
		Success(w, r, views.Contacts{Contacts: reply.Contacts}, http.StatusOK)
	}

}