示例#1
0
func buildFuzzyQuery() *bleve.SearchRequest {
	termQuery := bleve.NewFuzzyQuery("wate")
	termQuery.SetField("text")
	termSearch := bleve.NewSearchRequest(termQuery)
	return termSearch
}
示例#2
0
文件: parse.go 项目: bcampbell/qs
//   part = lit {"~" number} | range | "(" exprList ")"
func (p *Parser) parsePart(ctx context) (query.Query, error) {

	tok := p.next()

	//   lit
	if tok.typ == tLITERAL {
		var q query.Query
		if strings.ContainsAny(tok.val, "*?") {
			q = bleve.NewWildcardQuery(tok.val)
		} else {
			if p.peek().typ == tFUZZY {
				fuzziness, err := p.parseFuzzySuffix()
				if err != nil {
					return nil, err
				}
				fuzz := bleve.NewFuzzyQuery(tok.val)
				fuzz.SetFuzziness(fuzziness)
				q = fuzz
			} else {
				q = bleve.NewMatchPhraseQuery(tok.val)
			}
		}
		if ctx.field != "" {
			if fieldable, ok := q.(query.FieldableQuery); ok {
				fieldable.SetField(ctx.field)
			} else {
				return nil, ParseError{ctx.fieldPos, "unexpected field"}
			}
		}
		return q, nil
	}
	if tok.typ == tQUOTED {
		// strip quotes (ugh)
		txt := string(tok.val[1 : len(tok.val)-1])
		/*
			if strings.ContainsAny(txt, "*?") {
				return nil, ParseError{tok.pos, "wildcards not supported in phrases"}
			}
		*/
		q := bleve.NewMatchPhraseQuery(txt)
		if ctx.field != "" {
			q.SetField(ctx.field)
		}
		return q, nil
	}

	//   | "(" exprList ")"
	if tok.typ == tLPAREN {
		q, err := p.parseExprList(ctx)
		if err != nil {
			return nil, err
		}
		tok = p.next()
		if tok.typ != tRPAREN {
			return nil, ParseError{tok.pos, "missing )"}
		}
		return q, nil
	}

	//   | range
	if tok.typ == tLSQUARE || tok.typ == tLBRACE {
		p.backup()
		q, err := p.parseRange(ctx)
		if err != nil {
			return nil, err
		}
		return q, nil
	}

	//   | relational
	if tok.typ == tGREATER || tok.typ == tLESS {
		p.backup()
		q, err := p.parseRelational(ctx)
		if err != nil {
			return nil, err
		}
		return q, nil
	}

	if tok.typ == tERROR {
		return nil, ParseError{tok.pos, tok.val}
	}

	return nil, ParseError{tok.pos, fmt.Sprintf("unexpected %s", tok.val)}
}