Пример #1
0
func main() {
	// Open an index if it already exists.
	index, err := bleve.Open("example.bleve")
	if err != nil {
		mapping := bleve.NewIndexMapping()
		index, err = bleve.New("example.bleve", mapping)
		if err != nil {
			fmt.Println(err)
			return
		}
	}

	data := struct {
		Name string
	}{
		Name: "text",
	}

	// Index some data.
	index.Index("id", data)

	// Search for some text
	query := bleve.NewMatchQuery("text")
	search := bleve.NewSearchRequest(query)
	searchResults, err := index.Search(search)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(searchResults)
}
Пример #2
0
func main() {
	// open a new index
	mapping := bleve.NewIndexMapping()
	index, err := bleve.New("example.bleve", mapping)
	if err != nil {
		fmt.Println(err)
		return
	}

	data := struct {
		Name string
	}{
		Name: "text",
	}

	// index some data
	index.Index("id", data)

	// search for some text
	query := bleve.NewMatchQuery("text")
	search := bleve.NewSearchRequest(query)
	searchResults, err := index.Search(search)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(searchResults)
}
Пример #3
0
func (self *Wiki) Search(queryString string) ([]*SearchResult, error) {
	query := bleve.NewMatchQuery(queryString)
	search := bleve.NewSearchRequest(query)
	search.Highlight = bleve.NewHighlight()
	searchResult, err := self.Index.Search(search)
	if err != nil {
		return nil, err
	}
	results := []*SearchResult{}
	for _, result := range searchResult.Hits {
		section, article := self.Find(result.ID)
		if article == nil && section == nil {
			return nil, fmt.Errorf("%v section or article not found", result.ID)
		}
		text := ""
		for _, values := range result.Fragments {
			for _, value := range values {
				text += value
			}
		}
		name := ""
		if section != nil {
			name = section.Name
		}
		if article != nil {
			name = article.Name
		}
		results = append(results, &SearchResult{Path: result.ID, Name: name, Text: text})
	}
	return results, nil
}
Пример #4
0
func (pages *Pages) QueryMatch(field string, match string) *Pages {
	q := bleve.NewMatchQuery(match)
	q.SetField(field)
	pages.queries = append(pages.queries, q)
	pages.clear()
	return pages
}
Пример #5
0
func TestFindByAnything(t *testing.T) {
	db, eventList := dbCreate()
	idx := idxCreate()
	indexEvents(idx, eventList)

	// We are looking to an Event with some string which match with dotGo
	query := bleve.NewMatchQuery("dotGo")
	searchRequest := bleve.NewSearchRequest(query)
	searchResult, err := idx.Search(searchRequest)
	if err != nil {
		t.Error("Something wrong happen with the search", err, ballotX)
	} else {
		t.Log("Should search the query", checkMark)
	}

	if searchResult.Total != 1 {
		t.Error("Only 1 result are expected, got ", searchResult.Total, ballotX)
	} else {
		t.Log("Should return only one result", checkMark)
	}

	event := &Event{}
	db.First(&event, &searchResult.Hits[0].ID)
	if event.Name != "dotGo 2015" {
		t.Error("Expected \"dotGo 2015\", Receive: ", event.Name)
	} else {
		t.Log("Should return an event with the name equal a", event.Name, checkMark)
	}

	idxDestroy()
	dbDestroy()
}
Пример #6
0
//See https://godoc.org/github.com/blevesearch/bleve#SearchResult
func Search(keyword string) (*bleve.SearchResult, error) {
	fmt.Println(keyword)

	query := bleve.NewMatchQuery(keyword)
	request := bleve.NewSearchRequest(query)
	request.Size = 20
	//request.Fields = []string{"Name", "Text", "Size", "IsDir", "ModTime"}

	result, err := LimeIndex.Search(request)
	if err != nil {
		return nil, err
	}
	return result, nil
}
Пример #7
0
func makeSearchQuery(queryString string, ids []string) (query.Query, error) {
	nodes, err := blevext.Parse(queryString)
	if err != nil {
		return nil, err
	}

	addIdsFilter := func(q query.Query) query.Query {
		if len(ids) == 0 {
			return q
		}
		return bleve.NewConjunctionQuery(
			query.NewDocIDQuery(ids),
			q,
		)
	}

	var makeQuery func(*blevext.Node) (query.Query, error)
	makeQuery = func(n *blevext.Node) (query.Query, error) {
		if n == nil {
			return bleve.NewMatchAllQuery(), nil
		}
		switch n.Kind {
		case blevext.NodeAnd, blevext.NodeOr:
			left, err := makeQuery(n.Children[0])
			if err != nil {
				return nil, err
			}
			right, err := makeQuery(n.Children[1])
			if err != nil {
				return nil, err
			}
			if n.Kind == blevext.NodeOr {
				q := query.NewDisjunctionQuery([]query.Query{left, right})
				q.Min = 1
				return q, nil
			}
			return query.NewConjunctionQuery([]query.Query{left, right}), nil
		case blevext.NodeString, blevext.NodePhrase:
			fn := func(s string) query.FieldableQuery {
				return bleve.NewMatchQuery(s)
			}
			if n.Kind == blevext.NodePhrase {
				fn = func(s string) query.FieldableQuery {
					return blevext.NewAllMatchQuery(s)
				}
			}
			htmlQuery := fn(n.Value)
			htmlQuery.SetField("html")
			titleQuery := fn(n.Value)
			titleQuery.SetField("title")
			q := query.NewDisjunctionQuery([]query.Query{
				addIdsFilter(htmlQuery),
				addIdsFilter(titleQuery),
			})
			q.Min = 1
			return q, nil
		}
		return nil, fmt.Errorf("unknown query node type: %d", n.Kind)
	}
	return makeQuery(nodes)
}
Пример #8
0
func getJobSearchRequest(r *http.Request) (*bleve.SearchRequest, error) {
	musts := []query.Query{}
	// mustNots := []query.Query{}
	// shoulds := []query.Query{}

	tagShoulds := []query.Query{}
	for _, tag := range r.URL.Query()["tags"] {
		tagShoulds = append(tagShoulds, bleve.NewMatchQuery(tag))
	}
	if len(tagShoulds) > 0 {
		booleanQuery := bleve.NewBooleanQuery()
		booleanQuery.AddShould(tagShoulds...)
		musts = append(musts, booleanQuery)
	}

	value1 := 0.0
	if len(r.URL.Query().Get("price_from")) != 0 {
		intValue1, err := strconv.ParseInt(r.URL.Query().Get("price_from"), 10, 64)
		if err != nil {
			return nil, err
		}
		value1 = float64(intValue1)
	}
	value2 := math.MaxFloat64
	if len(r.URL.Query().Get("price_to")) != 0 {
		intValue2, err := strconv.ParseInt(r.URL.Query().Get("price_to"), 10, 64)
		if err != nil {
			return nil, err
		}
		value2 = float64(intValue2)
	}

	inclusiveValue1 := true
	inclusiveValue2 := false
	numericRangeIncludiveQuery := bleve.NewNumericRangeInclusiveQuery(
		&value1,
		&value2,
		&inclusiveValue1,
		&inclusiveValue2,
	)
	numericRangeIncludiveQuery.SetField("price")
	musts = append(musts, numericRangeIncludiveQuery)

	period := int64(30)
	if len(r.URL.Query().Get("period")) != 0 {
		periodTemp, err := strconv.ParseInt(r.URL.Query().Get("period"), 10, 64)
		if err != nil {
			return nil, err
		}
		if periodTemp > 0 && periodTemp <= 365 {
			period = periodTemp
		}
	}

	now := time.Now()
	dateTo := time.Now().Add(time.Duration(24*period) * time.Hour)
	dateRangeQuery := bleve.NewDateRangeQuery(now, dateTo)
	dateRangeQuery.SetField("startDate")
	musts = append(musts, dateRangeQuery)

	query := bleve.NewBooleanQuery()
	query.AddMust(musts...)
	// query.AddMustNot(mustNots...)
	// query.AddShould(shoulds...)

	searchRequest := bleve.NewSearchRequest(query)
	searchRequest.Fields = []string{"*"}

	return searchRequest, nil
}