func indexIdiomFullText(c context.Context, idiom *Idiom, idiomKey *datastore.Key) error {
	index, err := gaesearch.Open("idioms")
	if err != nil {
		return err
	}
	// By using directly the idiom Key as docID,
	// we can leverage faster ID-only search later.
	docID := strconv.Itoa(idiom.Id)
	w, wTitle, wLead := idiom.ExtractIndexableWords()
	doc := &searchableIdiomDoc{
		IdiomKeyString: gaesearch.Atom(idiomKey.Encode()),
		IdiomID:        gaesearch.Atom(strconv.Itoa(idiom.Id)),
		Bulk:           strings.Join(w, " "),
		Langs:          implementedLanguagesConcat(idiom),
		TitleWords:     strings.Join(wTitle, " "),
		LeadWords:      strings.Join(wLead, " "),
	}
	doc.TitleOrLeadWords = doc.TitleWords + " " + doc.LeadWords
	_, err = index.Put(c, docID, doc)
	if err != nil {
		return err
	}

	// Also index each impl individually,
	// so we know what to highlight.
	indexImpl, err := gaesearch.Open("impls")
	if err != nil {
		return err
	}
	for _, impl := range idiom.Implementations {
		implDocID := fmt.Sprintf("%d_%d", idiom.Id, impl.Id)
		w := impl.ExtractIndexableWords()
		implDoc := &searchableImplDoc{
			Lang:    impl.LanguageName,
			IdiomID: gaesearch.Atom(strconv.Itoa(idiom.Id)),
			Bulk:    strings.Join(w, " "),
		}
		// Weird that the search API doesn't have batch queries.
		// TODO: index each impl concurrently?
		// TODO: index only last edited impl?
		_, err = indexImpl.Put(c, implDocID, implDoc)
		if err != nil {
			return err
		}
	}

	return nil
}
Exemplo n.º 2
0
func handleGet(ctx context.Context, res http.ResponseWriter, criteria string) error {
	index, err := search.Open("Movies")
	if err != nil {
		return err
	}
	var iterator *search.Iterator
	if criteria == "" {
		iterator = index.List(ctx, nil)
	} else {
		iterator = index.Search(ctx, criteria, nil)
	}
	// slice of movie instances
	var movies []Movie
	for {
		var movie Movie
		_, err := iterator.Next(&movie)
		if err == search.Done {
			break
		}
		if err != nil {
			return err
		}
		movies = append(movies, movie)
	}
	json.NewEncoder(res).Encode(movies)
}
Exemplo n.º 3
0
func handleSearch(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	query := req.FormValue("q")
	model := &searchModel{
		Query: query,
	}

	index, err := search.Open("movies")
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}

	iterator := index.Search(ctx, query, nil)
	for {
		var movie Movie
		_, err := iterator.Next(&movie)
		if err == search.Done {
			break
		} else if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
		model.Movies = append(model.Movies, movie)
	}

	err = tpl.ExecuteTemplate(res, "search", model)
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
}
Exemplo n.º 4
0
func cleanup(s *testerator.Setup) error {
	c := s.Context
	sniff, ok := c.Value(&ctxKey).(*searchSniffer)
	if !ok {
		return ErrSetupRequired
	}

	indexNames := make(map[string]bool, 0)
	for _, req := range sniff.searchIndexDocumentRequests {
		indexNames[*req.GetParams().GetIndexSpec().Name] = true
	}
	for indexName, _ := range indexNames {
		idx, err := search.Open(indexName)
		if err != nil {
			return err
		}
		iter := idx.List(c, &search.ListOptions{IDsOnly: true})
		for {
			docID, err := iter.Next(nil)
			if err == search.Done {
				break
			} else if err != nil {
				return err
			}
			err = idx.Delete(c, docID)
			if err != nil {
				return err
			}
		}
	}

	sniff.searchIndexDocumentRequests = nil

	return nil
}
func indexIdiomCheatsheets(c context.Context, idiom *Idiom) error {
	index, err := gaesearch.Open("cheatsheets")
	if err != nil {
		return err
	}
	// Index each impl individually.
	for _, impl := range idiom.Implementations {
		docID := fmt.Sprintf("%d_%d", idiom.Id, impl.Id)
		doc := &cheatSheetLineDoc{
			Lang:                 gaesearch.Atom(impl.LanguageName),
			IdiomID:              gaesearch.Atom(strconv.Itoa(idiom.Id)),
			IdiomTitle:           gaesearch.Atom(idiom.Title),
			IdiomLeadParagraph:   gaesearch.Atom(idiom.LeadParagraph),
			ImplID:               gaesearch.Atom(strconv.Itoa(impl.Id)),
			ImplImportsBlock:     gaesearch.Atom(impl.ImportsBlock),
			ImplCodeBlock:        gaesearch.Atom(impl.CodeBlock),
			ImplCodeBlockComment: gaesearch.Atom(impl.AuthorComment),
		}
		// Weird that the search API doesn't have batch queries.
		// TODO: index each impl concurrently?
		_, err = index.Put(c, docID, doc)
		if err != nil {
			return err
		}
	}

	return nil
}
func (a *GaeDatastoreAccessor) unindexAll(c context.Context) error {
	log.Infof(c, "Unindexing everything (from the text search indexes)")

	// Must remove 1 by 1 (Index has no batch methods)
	for _, indexName := range []string{
		"idioms",
		"impls",
		"cheatsheets",
	} {
		log.Infof(c, "Unindexing items of [%v]", indexName)
		index, err := gaesearch.Open(indexName)
		if err != nil {
			return err
		}
		it := index.List(c, &gaesearch.ListOptions{IDsOnly: true})
		for {
			docID, err := it.Next(nil)
			if err == gaesearch.Done {
				break
			}
			if err != nil {
				log.Errorf(c, "Error getting next indexed object to unindex: %v", err)
				return err
			}
			err = index.Delete(c, docID)
			if err != nil {
				return err
			}
		}
	}

	return nil
}
Exemplo n.º 7
0
func handleNewMovie(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	model := &newMovieModel{}

	if req.Method == "POST" {
		title := req.FormValue("title")
		summary := req.FormValue("summary")

		index, err := search.Open("movies")
		if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}

		movie := &Movie{
			Title:   title,
			Summary: summary,
		}

		id, err := index.Put(ctx, "", movie)
		if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
		model.CreatedID = id
	}

	err := tpl.ExecuteTemplate(res, "new-movie", model)
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
}
func (a *GaeDatastoreAccessor) getCheatSheet(c context.Context, lang string, limit int) ([]cheatSheetLineDoc, error) {
	index, err := gaesearch.Open("cheatsheets")
	if err != nil {
		return nil, err
	}
	if limit == 0 {
		// Limit is not optional. 0 means zero result.
		return nil, nil
	}
	cheatLines := make([]cheatSheetLineDoc, 0, 200)
	query := "Lang:" + lang
	it := index.Search(c, query, &gaesearch.SearchOptions{
		Limit: limit,
	})
	for {
		var cheatLine cheatSheetLineDoc
		_, err := it.Next(&cheatLine)
		if err == gaesearch.Done {
			break
		}
		if err != nil {
			return nil, err
		}
		cheatLines = append(cheatLines, cheatLine)
	}
	// Sort by IdiomID asc, ImplID asc
	sort.Sort(cheatSheetLineDocs(cheatLines))
	return cheatLines, err
}
Exemplo n.º 9
0
func (b *InventorySearchBuilder) PutDocument(c context.Context, src *InventorySearch) (string, error) {
	index, err := search.Open(b.IndexName())
	if err != nil {
		return "", err
	}

	docID := ""
	if v, ok := interface{}(src).(smgutils.DocIDer); ok { // TODO can I shorten this cond expression?
		docID, err = v.DocID(c)
		if err != nil {
			return "", err
		}
		src.ID = docID
	}

	log.Debugf(c, "id: %#v, payload: %#v", docID, src)

	docID, err = index.Put(c, docID, src)
	if err != nil {
		return "", err
	}

	src.ID = docID

	return docID, nil
}
Exemplo n.º 10
0
func deleteIndex(c context.Context, id string) error {
	idx, err := search.Open("packages")
	if err != nil {
		return err
	}
	return idx.Delete(c, id)
}
Exemplo n.º 11
0
func handleSearch(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	index, err := search.Open("movies")
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
	var movies []Movie
	q := ""
	switch req.FormValue("mode") {
	case "title":
		q = "Title = " + req.FormValue("q")
	case "description":
		q = "Description = " + req.FormValue("q")
	case "username":
		q = "Username = "******"q")
	}
	iterator := index.Search(ctx, q, nil)
	for {
		var movie Movie
		_, err := iterator.Next(&movie)
		if err == search.Done {
			break
		} else if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
		movies = append(movies, movie)
	}
	renderTemplate(res, req, "layout", PageModel{
		SearchResults: movies,
	})
}
Exemplo n.º 12
0
// Indexes the comment with the specified key for search
func indexCommentForSearch(ctx context.Context, itemID TodoID) *MaybeError {
	index, err := search.Open("tada")
	var result = new(MaybeError)
	if err != nil {
		*result = E(err.Error())
	} else {
		item := readTodoItem(ctx, itemID)
		v := reflect.ValueOf(item)
		switch (*item).(type) {
		case E:
			result = item
		case TodoItem:
			log(fmt.Sprintf("Putting: %s of kind %s and %s", *item, v.Kind(), v.Elem().Kind()))
			titem := (*item).(TodoItem)
			_, err2 := index.Put(ctx, "", &titem)
			if err2 != nil {
				*result = E(err2.Error())
			} else {
				*result = Ok{}
			}
		case TodoID, Matches:
			*result = E("weird response from readTodoItem in indexCommentForSearch")
		}
	}
	return result
}
func (a *GaeDatastoreAccessor) searchImplIDs(c context.Context, words, langs []string) (map[string]bool, error) {
	index, err := gaesearch.Open("impls")
	if err != nil {
		return nil, err
	}
	hits := map[string]bool{}
	query := "Bulk:(~" + strings.Join(words, " AND ~") + ")"
	if len(langs) > 0 {
		query += " AND Lang:(" + strings.Join(langs, " OR ") + ")"
	}
	// Beware of INVALID_REQUEST: The limit 1000000 must be between 1 and 1000
	limit := 1000
	// This is an *IDsOnly* search, where docID == idiomID_implID
	it := index.Search(c, query, &gaesearch.SearchOptions{
		Limit:   limit,
		IDsOnly: true,
	})
	for {
		docID, err := it.Next(nil)
		if err == gaesearch.Done {
			break
		}
		if err != nil {
			return nil, err
		}
		implIDStr := strings.Split(docID, "_")[1]
		hits[implIDStr] = true
	}
	return hits, nil
}
func executeIdiomKeyTextSearchQuery(c context.Context, query string, limit int) (keystrings []string, err error) {
	// log.Infof(c, query)
	index, err := gaesearch.Open("idioms")
	if err != nil {
		return nil, err
	}
	if limit == 0 {
		// Limit is not optional. 0 means zero result.
		return nil, nil
	}
	idiomKeyStrings := make([]string, 0, limit)
	// This is an *IDsOnly* search, where docID == Idiom.Id
	it := index.Search(c, query, &gaesearch.SearchOptions{
		Limit:   limit,
		IDsOnly: true,
	})
	for {
		docID, err := it.Next(nil)
		if err == gaesearch.Done {
			break
		}
		if err != nil {
			return nil, err
		}
		idiomID, err := strconv.Atoi(docID)
		if err != nil {
			return nil, err
		}
		idiomKeyString := newIdiomKey(c, idiomID).Encode()
		idiomKeyStrings = append(idiomKeyStrings, idiomKeyString)
	}
	//log.Debugf(c, "Query [%v] yields %d results.", query, len(idiomKeyStrings))
	return idiomKeyStrings, nil
}
Exemplo n.º 15
0
// DeleteByDocID from Index.
func (b *SampleSearchBuilder) DeleteByDocID(c context.Context, docID string) error {
	index, err := search.Open(b.IndexName())
	if err != nil {
		return err
	}

	return index.Delete(c, docID)
}
Exemplo n.º 16
0
func addToIndex(ctx context.Context, mov *movie) error {
	idx, err := search.Open("movies")
	if err != nil {
		return err
	}

	_, err = idx.Put(ctx, "", mov)
	return err
}
func (a *GaeDatastoreAccessor) unindex(c context.Context, idiomID int) error {
	log.Infof(c, "Unindexing idiom %d", idiomID)

	docID := strconv.Itoa(idiomID)
	index, err := gaesearch.Open("idioms")
	if err != nil {
		return err
	}
	return index.Delete(c, docID)
}
Exemplo n.º 18
0
func handleNewMovie(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	model := &newMovieModel{}

	if req.Method == "POST" {
		title := req.FormValue("title")
		summary := req.FormValue("summary")
		poster, _, err := req.FormFile("poster")
		if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
		defer poster.Close()

		posterID, _ := uuid.NewV4()

		err = putFile(ctx, posterID.String(), poster)
		if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}

		summary, err = renderMarkdown(ctx, summary)
		if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}

		index, err := search.Open("movies")
		if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}

		movie := &Movie{
			Title:    title,
			Summary:  search.HTML(summary),
			PosterID: posterID.String(),
		}

		id, err := index.Put(ctx, "", movie)
		if err != nil {
			http.Error(res, err.Error(), 500)
			return
		}
		model.CreatedID = id
	}

	err := tpl.ExecuteTemplate(res, "new-movie", model)
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
}
Exemplo n.º 19
0
func (s *TaskService) Index(ctx context.Context, t *Task) error {
	span := trace.FromContext(ctx).NewChild("trythings.task.Index")
	defer span.Finish()

	index, err := search.Open("Task")
	if err != nil {
		return err
	}
	_, err = index.Put(ctx, t.ID, t)
	if err != nil {
		return err
	}
	return nil
}
Exemplo n.º 20
0
func handleGet(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	index, err := search.Open("example")
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
	var document Document
	err = index.Get(ctx, "7edf8426-e953-4378-8c1e-b2ad311356bb", &document)
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
	fmt.Fprintf(res, "%v", document)
}
Exemplo n.º 21
0
func handleGet(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	index, err := search.Open("example")
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
	var document Document
	err = index.Get(ctx, "23687ac5-5417-4b13-b9f7-4e674fa5c227", &document)
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}
	fmt.Fprintf(res, "%v", document.Text)
}
Exemplo n.º 22
0
func handlePut(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)

	index, err := search.Open("example")
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}

	var document = &Document{
		Name: "example",
		Text: `It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.
However little known the feelings or views of such a man may be on his first entering a neighbourhood, this truth is so well fixed in the minds of the surrounding families, that he is considered the rightful property of some one or other of their daughters.
"My dear Mr. Bennet," said his lady to him one day, "have you heard that Netherfield Park is let at last?"
Mr. Bennet replied that he had not.
"But it is," returned she; "for Mrs. Long has just been here, and she told me all about it."
Mr. Bennet made no answer.
"Do you not want to know who has taken it?" cried his wife impatiently.
"You want to tell me, and I have no objection to hearing it."
This was invitation enough.
"Why, my dear, you must know, Mrs. Long says that Netherfield is taken by a young man of large fortune from the north of England; that he came down on Monday in a chaise and four to see the place, and was so much delighted with it, that he agreed with Mr. Morris immediately; that he is to take possession before Michaelmas, and some of his servants are to be in the house by the end of next week."
"What is his name?"
"Bingley."
"Is he married or single?"
"Oh! Single, my dear, to be sure! A single man of large fortune; four or five thousand a year. What a fine thing for our girls!"
"How so? How can it affect them?"
"My dear Mr. Bennet," replied his wife, "how can you be so tiresome! You must know that I am thinking of his marrying one of them."
"Is that his design in settling here?"
"Design! Nonsense, how can you talk so! But it is very likely that he may fall in love with one of them, and therefore you must visit him as soon as he comes."
"I see no occasion for that. You and the girls may go, or you may send them by themselves, which perhaps will be still better, for as you are as handsome as any of them, Mr. Bingley may like you the best of the party."
"My dear, you flatter me. I certainly have had my share of beauty, but I do not pretend to be anything extraordinary now. When a woman has five grown-up daughters, she ought to give over thinking of her own beauty."
"In such cases, a woman has not often much beauty to think of."
"But, my dear, you must indeed go and see Mr. Bingley when he comes into the neighbourhood."
"It is more than I engage for, I assure you."
"But consider your daughters. Only think what an establishment it would be for one of them. Sir William and Lady Lucas are determined to go, merely on that account, for in general, you know, they visit no newcomers. Indeed you must go, for it will be impossible for us to visit him if you do not."
"You are over-scrupulous, surely. I dare say Mr. Bingley will be very glad to see you; and I will send a few lines by you to assure him of my hearty consent to his marrying whichever he chooses of the girls; though I must throw in a good word for my little Lizzy."
"I desire you will do no such thing. Lizzy is not a bit better than the others; and I am sure she is not half so handsome as Jane, nor half so good-humoured as Lydia. But you are always giving her the preference."`,
	}

	//	var document = &Document{
	//		Name: "StephenKing",
	//		Text: `Jacobs's electrical workshop was in West Tulsa. I don't know what that part of town is like now, but in 1992 it was a forlorn industrial zone where a lot of the industries seemed to be dead or dying. He pulled into the parking lot of an all-but-destitute strip mall on Olympia Avenue and parked in front of Wilson Auto Body. "It was standing empty for a long time, that's what the Realtor told me," Jacobs said. He was dressed in faded jeans and a blue golf shirt, his hair washed and combed, his eyes sparkling with excitement. Just looking at him made me nervous. "I had to take a year's lease, but it was still dirt cheap. Come on in." "You ought to take down the sign and put up your own," I said. I framed it with hands that were only shaking a little. "'Portraits in Lightning, C. D. Jacobs, Proprietor.' It would look good." "I won't be in Tulsa that long," he said, "and the portraits are really just a way of supporting myself while I conduct my experiments. I've come a long way since my pastoral days, but I've still got a long way to go. You have no idea. Come in, Jamie. Come in." He unlocked a door and led me through an office that was empty of furniture, although I could still see square clean patches on the grimy linoleum, where the legs of a desk had once stood. On the wall was a curling calendar with April 1989 showing. The garage had a corrugated metal roof and I expected it to be baking under the September sun, but it was wonderfully cool. I could hear the whisper of air conditioners. When he flicked a bank of switches—recently modified, judging from the makeshift way the wires stuck out of the uncovered holes where the plates had been—a dozen brilliant lights came on. If not for the oil-darkened concrete and the rectangular caverns where two lifts had once been, you would have thought it was an operating theater. "It must cost a fortune to air-condition this place," I said. "Especially when you've got all those lights blazing." "Dirt cheap. The air conditioners are my own design. They draw very little power, and most of that I generate myself. I could gener- ate all of it, but I wouldn't want Tulsa Power and Light down here, snooping around to fifind out if I was volt-jacking, somehow. As for the lights . . . you could wrap a hand around one of the bulbs with- out burning yourself. Or even heating your skin, for that matter." Our footfalls echoed in all that empty space. So did our voices. It was like being in the company of phantoms. It just feels that way because I'm strung out, I told myself. "Listen, Charlie — you're not messing with anything radioactive, are you?" He grimaced and shook his head. "Nuclear's the last thing I'm interested in. It's energy for idiots. A dead end." "So how do you generate the juice?" "Electricity breeds electricity, if you know what you're doing. Leave it at that. Step over here, Jamie." There were three or four long tables at the end of the room with electrical stuff on them. I recognized an oscilloscope, a spectrom- eter, and a couple of things that looked like Marshall amps but could have been batteries of some kind. There was a control board that looked mostly torn apart, and several stacked consoles with darkened dials. Thick electrical cords snaked every whichway. Some disappeared into closed metal containers that could have been Craftsman tool chests; others just looped back to the dark equip- ment. This could all be a fantasy, I thought. Equipment that only comes alive in his imagination. But the Portraits in Lightning weren't make- believe. I had no idea how he was making those, his explanation had been vague at best, but he was making them. And although I was standing directly beneath one of those brilliant lights, it really did not seem to be throwing any heat. "There doesn't seem to be much here," I said doubtfully. "I expected more." "Flashing lights! Chrome-plated knife-switches sticking out of science fiction control panels! Star Trek telescreens! Possibly a tele- portation chamber, or a hologram of Noah's Ark in a cloud cham- ber!" He laughed cheerily. "Nothing like that," I said, although he had pretty much hit the nail on the head. "It just seems kind of . . . sparse." "It is. I've gone about as far as I can for the time being. I've sold some of my equipment. Other stuff—more controversial stuff— I've dismantled and put in storage. I've done good work in Tulsa, especially considering how little spare time I have. Keeping body and soul together is an annoying business, as I suppose you know." I certainly did. "But yes, I made some progress toward my ultimate goal. Now I need to think, and I don't believe I can do that when I'm turning half a dozen tips a night." "Your ultimate goal being what?" He ignored the question this time, too. "Step over here, Jamie. Would you like a small pick-me-up before we begin?" I wasn't sure I wanted to begin, but I wanted a pick-me-up, all right. Not for the first time, I considered just snatching the little brown bottle and running. Only he'd probably catch me and wrest it away. I was younger, and almost over the flu, but he was still in better shape. He hadn't suffered a shattered hip and leg in a motor- cycle accident, for one thing. He grabbed a paint-spattered wooden chair and set it in front of one of the black boxes that looked like a Marshall amp. "Sit here." But I didn't, not right away. There was a picture on one of the tables, the kind with a little wedge on the back to prop it up. He saw me reach for it and made a move as if to stop me. Then he just stood there. A song on the radio can bring back the past with fierce (if mercifully transitory) immediacy: a first kiss, a good time with your buddies, or an unhappy life-passage. I can never hear Fleetwood Mac's "Go Your Own Way" without thinking of my mother's last painful weeks; that spring it seemed to be on the radio every time I turned it on. A picture can have the same effect. I looked at this one and all at once I was eight again. My sister was helping Morrie set up dominos in Toy Corner while Patsy Jacobs played "Bringing in the Sheaves," swaying on the piano bench, her smooth blond hair shifting from side to side. It was a studio portrait. Patsy was wearing the sort of billowy, shin-length dress that went out of fashion years ago, but it looked good on her. The kid was on her lap, wearing short pants and a sweater vest. A cowlick I remembered well stuck up at the back of his head. Read more: http://www.rollingstone.com/culture/features/stephen-king-exclusive-read-an-excerpt-from-new-book-revival-20141027#ixzz3rGP0EkMZ Follow us: @rollingstone on Twitter | RollingStone on Facebook`,
	//	}

	id, err := index.Put(ctx, "", document)

	fmt.Fprintf(res, "ID: %v, ERROR: %v", id, err)
}
Exemplo n.º 23
0
// Search returns *SampleSearchIterator, It is result from Index.
func (b *SampleSearchBuilder) Search(c context.Context) (*SampleSearchIterator, error) {
	index, err := search.Open(b.IndexName())
	if err != nil {
		return nil, err
	}
	b.index = index

	query, err := b.QueryString()
	if err != nil {
		return nil, err
	}
	b.query = query
	log.Debugf(c, "query: '%s', opts: %#v", b.query, b.opts)
	iter := b.index.Search(c, b.query, b.opts)

	return &SampleSearchIterator{b, iter}, nil
}
Exemplo n.º 24
0
// [START deleting_documents_from_an_index]
func deleteHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)

	index, err := search.Open("users")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	id := "PA6-5000"
	err = index.Delete(ctx, id)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	fmt.Fprint(w, "Deleted document: ", id)
}
Exemplo n.º 25
0
// [START retrieving_documents_by_doc_ids]
func getHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)

	index, err := search.Open("users")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	id := "PA6-5000"
	var user User
	if err := index.Get(ctx, id, &user); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	fmt.Fprint(w, "Retrieved document: ", user)
}
Exemplo n.º 26
0
func TestPutIndexNewPackageAndUpdate(t *testing.T) {
	c, done, err := aetest.NewContext()
	if err != nil {
		t.Fatal(err)
	}
	defer done()

	// Put a new package into search index.
	if err := PutIndex(c, pdoc, "12345", 0.99, 1); err != nil {
		t.Fatal(err)
	}

	// Verify the package was put in is as expected.
	idx, err := search.Open("packages")
	if err != nil {
		t.Fatal(err)
	}
	var got Package
	if err = idx.Get(c, "12345", &got); err != nil && err != search.ErrNoSuchDocument {
		t.Fatal(err)
	}
	wanted := Package{
		Name:        pdoc.Name,
		Path:        pdoc.ImportPath,
		Synopsis:    pdoc.Synopsis,
		ImportCount: 1,
		Fork:        true,
		Stars:       10,
		Score:       0.99,
	}
	if got != wanted {
		t.Errorf("PutIndex got %v, want %v", got, wanted)
	}

	// Update the import count of the package.
	if err := PutIndex(c, nil, "12345", -1, 2); err != nil {
		t.Fatal(err)
	}
	if err := idx.Get(c, "12345", &got); err != nil && err != search.ErrNoSuchDocument {
		t.Fatal(err)
	}
	wanted.ImportCount = 2
	if got != wanted {
		t.Errorf("PutIndex got %v, want %v", got, wanted)
	}
}
Exemplo n.º 27
0
func handlePut(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)

	index, err := search.Open("example")
	if err != nil {
		http.Error(res, err.Error(), 500)
		return
	}

	var document = &Document{
		Name: "example",
		Text: `It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.
However little known the feelings or views of such a man may be on his first entering a neighbourhood, this truth is so well fixed in the minds of the surrounding families, that he is considered the rightful property of some one or other of their daughters.
"My dear Mr. Bennet," said his lady to him one day, "have you heard that Netherfield Park is let at last?"
Mr. Bennet replied that he had not.
"But it is," returned she; "for Mrs. Long has just been here, and she told me all about it."
Mr. Bennet made no answer.
"Do you not want to know who has taken it?" cried his wife impatiently.
"You want to tell me, and I have no objection to hearing it."
This was invitation enough.
"Why, my dear, you must know, Mrs. Long says that Netherfield is taken by a young man of large fortune from the north of England; that he came down on Monday in a chaise and four to see the place, and was so much delighted with it, that he agreed with Mr. Morris immediately; that he is to take possession before Michaelmas, and some of his servants are to be in the house by the end of next week."
"What is his name?"
"Bingley."
"Is he married or single?"
"Oh! Single, my dear, to be sure! A single man of large fortune; four or five thousand a year. What a fine thing for our girls!"
"How so? How can it affect them?"
"My dear Mr. Bennet," replied his wife, "how can you be so tiresome! You must know that I am thinking of his marrying one of them."
"Is that his design in settling here?"
"Design! Nonsense, how can you talk so! But it is very likely that he may fall in love with one of them, and therefore you must visit him as soon as he comes."
"I see no occasion for that. You and the girls may go, or you may send them by themselves, which perhaps will be still better, for as you are as handsome as any of them, Mr. Bingley may like you the best of the party."
"My dear, you flatter me. I certainly have had my share of beauty, but I do not pretend to be anything extraordinary now. When a woman has five grown-up daughters, she ought to give over thinking of her own beauty."
"In such cases, a woman has not often much beauty to think of."
"But, my dear, you must indeed go and see Mr. Bingley when he comes into the neighbourhood."
"It is more than I engage for, I assure you."
"But consider your daughters. Only think what an establishment it would be for one of them. Sir William and Lady Lucas are determined to go, merely on that account, for in general, you know, they visit no newcomers. Indeed you must go, for it will be impossible for us to visit him if you do not."
"You are over-scrupulous, surely. I dare say Mr. Bingley will be very glad to see you; and I will send a few lines by you to assure him of my hearty consent to his marrying whichever he chooses of the girls; though I must throw in a good word for my little Lizzy."
"I desire you will do no such thing. Lizzy is not a bit better than the others; and I am sure she is not half so handsome as Jane, nor half so good-humoured as Lydia. But you are always giving her the preference."`,
	}

	id, err := index.Put(ctx, "", document)

	fmt.Fprintf(res, "ID: %v, ERROR: %v", id, err)
}
func executeIdiomTextSearchQuery(c context.Context, query string, limit int) ([]*Idiom, error) {
	// log.Infof(c, query)
	index, err := gaesearch.Open("idioms")
	if err != nil {
		return nil, err
	}
	if limit == 0 {
		// Limit is not optional. 0 means zero result.
		return nil, nil
	}
	idiomKeys := make([]*datastore.Key, 0, limit)
	// This is an *IDsOnly* search, where docID == Idiom.Id
	it := index.Search(c, query, &gaesearch.SearchOptions{
		Limit:   limit,
		IDsOnly: true,
	})
	for {
		docID, err := it.Next(nil)
		if err == gaesearch.Done {
			break
		}
		if err != nil {
			return nil, err
		}
		idiomID, err := strconv.Atoi(docID)
		if err != nil {
			return nil, err
		}
		key := newIdiomKey(c, idiomID)
		idiomKeys = append(idiomKeys, key)
	}
	// Fetch Idioms in a []Idiom
	buffer := make([]Idiom, len(idiomKeys))
	err = datastore.GetMulti(c, idiomKeys, buffer)
	// Convert []Idiom to []*Idiom
	idioms := make([]*Idiom, len(buffer))
	for i := range buffer {
		// Do not take the address of the 2nd range variable, it would make a copy.
		// Better take the address in the existing buffer.
		idioms[i] = &buffer[i]
	}
	return idioms, err
}
Exemplo n.º 29
0
// AddToSearchIndexTask is the implementation of the task described above.
// This adds an item to the search index.
func AddToSearchIndexTask(con *data.Context, searchItem *Item, namespace string, URL string) error {
	index, err := search.Open("items_" + namespace)
	if err != nil {
		con.Log.Errorf("Error while opening the item index %v", err)
		return err
	}

	searchItem.HTMLforSearch = search.HTML(optimizeSearchInput(string(searchItem.HTMLforSearch)))

	searchItem.Description = optimizeSearchInput(searchItem.Description)

	_, err = index.Put(con.C, strconv.QuoteToASCII(URL), searchItem)
	if err != nil {
		con.Log.Errorf("Error while puting the search item in the index %v", err)
		return err
	}

	return nil
}
Exemplo n.º 30
0
func searchMovies(ctx context.Context, query string) ([]movie, error) {
	idx, err := search.Open("movies")
	if err != nil {
		return nil, err
	}

	it := idx.Search(ctx, query, nil)
	movies := []movie{}
	for {
		var mov movie
		_, err := it.Next(&mov)
		if err == search.Done {
			break
		} else if err != nil {
			return nil, err
		}
		movies = append(movies, mov)
	}
	return movies, nil
}