Elasticsearch调用Lucene查询接口源码分析四:通配符查询

Elasticsearch调用Lucene查询接口源码分析四:通配符查询

  • 简介
  • 查询语法
  • 源码分析

简介

支持两种通配符:问号(?)和星号()。可以使用问号(?)来进行单字符的通配符查询,或者利用星号(*)进行多字符的通配符查询。

查询语法

例1,搜索 tiny 或者 tony,可以使用查询语句 “t?ny”:
{
“query” : {
“wildcard” : {
“title” : {
“value” : “t?ny”
}}}}
例2,查询 Teach, Teacher 和 Teaching,可以使用查询语句 “Teach*”:
{
“query” : {
“wildcard” : {
“title” : {
“value” : “Teach*”
}}}}

源码分析

'''(1)Elasticsearch code'''
public class WildcardQueryParser implements QueryParser {
    public static final String NAME = "wildcard";

    @Override
    public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
        XContentParser parser = parseContext.parser();

        XContentParser.Token token = parser.nextToken();
        if (token != XContentParser.Token.FIELD_NAME) {
            throw new QueryParsingException(parseContext.index(), "[wildcard] query malformed, no field");
        }
        String fieldName = parser.currentName();
        String rewriteMethod = null;

        String value = null;
        float boost = 1.0f;
        token = parser.nextToken();
        if (token == XContentParser.Token.START_OBJECT) {
            String currentFieldName = null;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else {
                    if ("wildcard".equals(currentFieldName)) {
                        value = parser.text();
                    } else if ("value".equals(currentFieldName)) {
                        value = parser.text();
                    } else if ("boost".equals(currentFieldName)) {
                        boost = parser.floatValue();
                    } else if ("rewrite".equals(currentFieldName)) {
                        rewriteMethod = parser.textOrNull();
                    } else {
                        throw new QueryParsingException(parseContext.index(), "[wildcard] query does not support [" + currentFieldName + "]");
                    }
                }
            }
            parser.nextToken();
        } else {
            value = parser.text();
            parser.nextToken();
        }

        if (value == null) {
            throw new QueryParsingException(parseContext.index(), "No value specified for prefix query");
        }

        BytesRef valueBytes;
        MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName);
        if (smartNameFieldMappers != null && smartNameFieldMappers.hasMapper()) {
            fieldName = smartNameFieldMappers.mapper().names().indexName();
            valueBytes = smartNameFieldMappers.mapper().indexedValueForSearch(value);
        } else {
            valueBytes = new BytesRef(value);
        }
        '''构造Lucene的WildcardQuery对象(Term("title","Teach*"))'''
        WildcardQuery query = new WildcardQuery(new Term(fieldName, valueBytes));
        '''设置query重写的方法,复杂的query在查询之前需要进行重写,比如:Teach*将被重写成Teach, Teacher 和 Teaching(这三个词在索引中存在),然后按重写后的词查找索引,这三个词之间是或的关系'''
        QueryParsers.setRewriteMethod(query, rewriteMethod);
        query.setRewriteMethod(QueryParsers.parseRewriteMethod(rewriteMethod));
        query.setBoost(boost);
        return wrapSmartNameQuery(query, smartNameFieldMappers, parseContext);
    }
}

'''(2)Lucene code'''
public class WildcardQuery extends AutomatonQuery {
   ...
}
public class AutomatonQuery extends MultiTermQuery {
   ...
}
public abstract class MultiTermQuery extends Query {
    '''WildcardQuery是一种MultiTermQuery,调用Elasticsearch设置的重写方法'''
    public final Query rewrite(IndexReader reader) throws IOException {
    return rewriteMethod.rewrite(reader, this);
  }

你可能感兴趣的:(elasticsearch,Lucene)