Search in sources :

Example 96 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project orientdb by orientechnologies.

the class OLuceneIndexType method getQueryParser.

protected static Query getQueryParser(OIndexDefinition index, String key, Analyzer analyzer) throws ParseException {
    QueryParser queryParser;
    if ((key).startsWith("(")) {
        queryParser = new QueryParser("", analyzer);
    } else {
        String[] fields = null;
        if (index.isAutomatic()) {
            fields = index.getFields().toArray(new String[index.getFields().size()]);
        } else {
            int length = index.getTypes().length;
            fields = new String[length];
            for (int i = 0; i < length; i++) {
                fields[i] = "k" + i;
            }
        }
        queryParser = new MultiFieldQueryParser(fields, analyzer);
    }
    return queryParser.parse(key);
}
Also used : MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser)

Example 97 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project searchcode-server by boyter.

the class IndexService method deleteByCodeId.

/**
 * Deletes a file from the index using the code id which seems to be
 * the most reliable way of doing it. Code id being a hash of the file
 * name and location.
 * TODO Update the record and set the facets to a value we can ignore
 */
@Override
public synchronized void deleteByCodeId(String codeId) throws IOException {
    Directory dir = FSDirectory.open(this.INDEX_WRITE_LOCATION);
    Analyzer analyzer = new CodeAnalyzer();
    IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
    IndexWriter writer = null;
    Query query;
    try {
        writer = new IndexWriter(dir, iwc);
        QueryParser parser = new QueryParser(Values.CONTENTS, analyzer);
        query = parser.parse(Values.CODEID + ":" + QueryParser.escape(codeId));
        writer.deleteDocuments(query);
    } catch (ParseException | NoSuchFileException ex) {
        this.logger.severe(String.format("e9a71f33::error in class %s exception %s", ex.getClass(), ex.getMessage()));
    }
    this.helpers.closeQuietly(writer);
}
Also used : QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) NoSuchFileException(java.nio.file.NoSuchFileException) ParseException(org.apache.lucene.queryparser.classic.ParseException) Analyzer(org.apache.lucene.analysis.Analyzer) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory)

Example 98 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project searchcode-server by boyter.

the class IndexService method getProjectFileTree.

/**
 * Collects project stats for a repo given its name
 */
@Override
public SearchResult getProjectFileTree(String repoName) {
    List<CodeResult> codeResults = new ArrayList<>();
    if (this.helpers.isNullEmptyOrWhitespace(repoName)) {
        return new SearchResult(0, 0, Values.EMPTYSTRING, codeResults, null, null, null, null, null);
    }
    IndexReader reader = null;
    try {
        reader = DirectoryReader.open(FSDirectory.open(this.INDEX_READ_LOCATION));
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new CodeAnalyzer();
        QueryParser parser = new QueryParser(Values.CONTENTS, analyzer);
        Query query = parser.parse(Values.REPO_NAME_LITERAL + ":" + this.helpers.replaceForIndex(repoName));
        TopDocs results = searcher.search(query, Integer.MAX_VALUE);
        ScoreDoc[] hits = results.scoreDocs;
        for (int i = 0; i < results.totalHits; i++) {
            Document doc = searcher.doc(hits[i].doc);
            CodeResult codeResult = this.createCodeResult(null, Values.EMPTYSTRING, doc, hits[i].doc, hits[i].score);
            codeResults.add(codeResult);
            if (i == 1_000) {
                break;
            }
        }
    } catch (Exception ex) {
        this.logger.severe(String.format("1c90b7f9::error in class %s exception %s", ex.getClass(), ex.getMessage()));
    } finally {
        this.helpers.closeQuietly(reader);
    }
    codeResults.sort(Comparator.comparing(x -> x.displayLocation));
    return new SearchResult(0, 0, Values.EMPTYSTRING, codeResults, null, null, null, null, null);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) ParseException(org.apache.lucene.queryparser.classic.ParseException) org.apache.lucene.facet(org.apache.lucene.facet) NoSuchFileException(java.nio.file.NoSuchFileException) java.util(java.util) ScoreDoc(org.apache.lucene.search.ScoreDoc) RepoResult(com.searchcode.app.model.RepoResult) StringUtils(org.apache.commons.lang3.StringUtils) com.searchcode.app.util(com.searchcode.app.util) SortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState) SortedSetDocValuesFacetField(org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField) Directory(org.apache.lucene.store.Directory) StatsService(com.searchcode.app.service.StatsService) JobService(com.searchcode.app.service.JobService) FSDirectory(org.apache.lucene.store.FSDirectory) Path(java.nio.file.Path) TopDocs(org.apache.lucene.search.TopDocs) SortedSetDocValuesFacetCounts(org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts) Singleton(com.searchcode.app.service.Singleton) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Analyzer(org.apache.lucene.analysis.Analyzer) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) TaxonomyWriter(org.apache.lucene.facet.taxonomy.TaxonomyWriter) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) Collectors(java.util.stream.Collectors) Values(com.searchcode.app.config.Values) org.apache.lucene.index(org.apache.lucene.index) com.searchcode.app.dto(com.searchcode.app.dto) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Paths(java.nio.file.Paths) DefaultSortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState) org.apache.lucene.document(org.apache.lucene.document) Data(com.searchcode.app.dao.Data) Properties(com.searchcode.app.util.Properties) IndexSearcher(org.apache.lucene.search.IndexSearcher) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Query(org.apache.lucene.search.Query) Analyzer(org.apache.lucene.analysis.Analyzer) ParseException(org.apache.lucene.queryparser.classic.ParseException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser)

Example 99 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project HongsCORE by ihongs.

the class LuceneRecord method padQry.

/**
 * 组织查询条件
 * 可覆盖此方法扩展查询条件
 *
 * @param qr
 * @param rd
 * @param r 递归层级
 * @throws HongsException
 */
protected void padQry(BooleanQuery.Builder qr, Map rd, int r) throws HongsException {
    // 条件数量, 否定数量, 计数规避全否定时查不到数据
    int i = 0, j = 0;
    Map<String, Map> fields = getFields();
    Set<String> ks = new LinkedHashSet(fields.keySet());
    ks.retainAll(rd.keySet());
    for (String k : ks) {
        Object v = rd.get(k);
        Map m = fields.get(k);
        if (m == null || v == null) {
            continue;
        }
        // 自定义条件
        if (!padQry(qr, rd, k, v)) {
            continue;
        }
        IQuery qa;
        String t = datatype(m);
        if (t != null)
            switch(t) {
                case "int":
                    qa = new IntQuery();
                    break;
                case "long":
                case "date":
                    qa = new LongQuery();
                    break;
                case "float":
                    qa = new FloatQuery();
                    break;
                case "double":
                case "number":
                    qa = new DoubleQuery();
                    break;
                case "string":
                case "search":
                    if (!srchable(m)) {
                        qa = new StringQuery();
                    } else {
                        SearchQuery qs;
                        qa = qs = new SearchQuery();
                        qs.analyser(getAnalyser(m));
                        qs.settings(m);
                    }
                    break;
                default:
                    continue;
            }
        else {
            continue;
        }
        // ** 常规查询 **/
        Map vd;
        if (v instanceof Map) {
            vd = (Map) v;
        } else if (v instanceof Collection || v instanceof Object[]) {
            Set a = Synt.asSet(v);
            a.remove("");
            if (!a.isEmpty()) {
                BooleanQuery.Builder qx = new BooleanQuery.Builder();
                for (Object b : a) {
                    qx.add(qa.whr(k, b), BooleanClause.Occur.SHOULD);
                }
                qr.add(qx.build(), BooleanClause.Occur.MUST);
                i++;
            }
            continue;
        } else {
            if (!v.equals("")) {
                qr.add(qa.whr(k, v), BooleanClause.Occur.MUST);
                i++;
            }
            continue;
        }
        // ** 条件关系 **/
        v = vd.get(Cnst.OR_REL);
        if (Cnst.OR_KEY.equals(v) || Cnst.NR_KEY.equals(v)) {
            List r2 = new ArrayList(vd.size() - 1);
            for (Object ot : vd.entrySet()) {
                Map.Entry et = (Map.Entry) ot;
                Object k2 = et.getKey();
                if (!Cnst.OR_KEY.equals(k2)) {
                    Object v2 = et.getValue();
                    r2.add(Synt.mapOf(k, Synt.mapOf(k2, v2)));
                }
            }
            if (!r2.isEmpty()) {
                padQry(qr, Synt.mapOf(v, r2), r - 1);
            }
            continue;
        }
        // ** 空值查询 **/
        v = vd.get(Cnst.IS_REL);
        if (v != null) {
            String b = qa instanceof SearchQuery ? "$" : "@";
            String a = Synt.asString(v).toUpperCase();
            Query p;
            try {
                p = new QueryParser(b + k, new StandardAnalyzer()).parse("[* TO *]");
            } catch (ParseException e) {
                throw new HongsExemption(e);
            }
            switch(a) {
                case "WELL":
                case "NOT-NULL":
                    qr.add(p, BooleanClause.Occur.MUST);
                    i++;
                    break;
                case "NULL":
                case "NOT-WELL":
                    qr.add(p, BooleanClause.Occur.MUST_NOT);
                    i++;
                    j++;
                    break;
                default:
                    throw new UnsupportedOperationException("Unsupported `is`: " + v);
            }
        }
        // ** 精确匹配 **/
        v = vd.get(Cnst.EQ_REL);
        if (v != null) {
            qr.add(qa.whr(k, v), BooleanClause.Occur.MUST);
            i++;
        }
        v = vd.get(Cnst.NE_REL);
        if (v != null) {
            qr.add(qa.whr(k, v), BooleanClause.Occur.MUST_NOT);
            i++;
            j++;
        }
        // ** 模糊匹配 **/
        v = vd.get(Cnst.CQ_REL);
        if (v != null && !"".equals(v)) {
            qr.add(qa.wdr(k, v), BooleanClause.Occur.MUST);
            i++;
        }
        v = vd.get(Cnst.NC_REL);
        if (v != null && !"".equals(v)) {
            qr.add(qa.wdr(k, v), BooleanClause.Occur.MUST_NOT);
            i++;
            j++;
        }
        // ** 集合查询 **/
        v = vd.get(Cnst.AI_REL);
        if (v != null) {
            Set vs = Synt.asSet(v);
            if (!vs.isEmpty()) {
                for (Object vv : vs) {
                    qr.add(qa.whr(k, vv), BooleanClause.Occur.MUST);
                    i++;
                }
            }
        }
        v = vd.get(Cnst.NI_REL);
        if (v != null) {
            Set vs = Synt.asSet(v);
            if (!vs.isEmpty()) {
                for (Object vv : vs) {
                    qr.add(qa.whr(k, vv), BooleanClause.Occur.MUST_NOT);
                    i++;
                    j++;
                }
            }
        }
        v = vd.get(Cnst.IN_REL);
        if (v != null) {
            Set vs = Synt.asSet(v);
            if (!vs.isEmpty()) {
                BooleanQuery.Builder qx = new BooleanQuery.Builder();
                for (Object vv : vs) {
                    qx.add(qa.whr(k, vv), BooleanClause.Occur.SHOULD);
                }
                qr.add(qx.build(), BooleanClause.Occur.MUST);
                i++;
            }
        }
        v = vd.get(Cnst.ON_REL);
        if (v != null) {
            if (v instanceof Collection || v instanceof Object[]) {
                Set a = Synt.asSet(v);
                // 与 in 不同
                a.remove("");
                if (!a.isEmpty()) {
                    BooleanQuery.Builder qx = new BooleanQuery.Builder();
                    for (Object b : a) {
                        qx.add(qa.whr(k, b), BooleanClause.Occur.SHOULD);
                    }
                    qr.add(qx.build(), BooleanClause.Occur.MUST);
                    i++;
                }
            } else {
                if (!v.equals("")) {
                    qr.add(qa.whr(k, v), BooleanClause.Occur.MUST);
                    i++;
                }
            }
        }
        // ** 区间查询 **/
        Object n, x;
        boolean l, g;
        n = vd.get(Cnst.GT_REL);
        if (n != null) {
            l = false;
        } else {
            n = vd.get(Cnst.GE_REL);
            if (n != null) {
                l = true;
            } else {
                n = null;
                l = true;
            }
        }
        x = vd.get(Cnst.LT_REL);
        if (x != null) {
            g = false;
        } else {
            x = vd.get(Cnst.LE_REL);
            if (x != null) {
                g = true;
            } else {
                x = null;
                g = true;
            }
        }
        if ((n != null && !"".equals(n)) || (x != null && !"".equals(x))) {
            Query qu = qa.whr(k, n, x, l, g);
            qr.add(qu, BooleanClause.Occur.MUST);
            i++;
        }
        Set s = null;
        v = vd.get(Cnst.RG_REL);
        if (v != null) {
            s = Synt.asSet(v);
        }
        if (s != null && !s.isEmpty()) {
            BooleanQuery.Builder qx = new BooleanQuery.Builder();
            for (Object o : s) {
                Object[] a = Synt.toRange(o);
                if (a == null) {
                    continue;
                }
                n = a[0];
                l = (boolean) a[2];
                x = a[1];
                g = (boolean) a[3];
                if (n == null && x == null) {
                    continue;
                }
                Query qu = qa.whr(k, n, x, l, g);
                qx.add(qu, BooleanClause.Occur.SHOULD);
            }
            BooleanQuery qz = qx.build();
            if (qz.clauses().size() > 0) {
                qr.add(qz, BooleanClause.Occur.MUST);
                i++;
            }
        }
    }
    Object v;
    // ** 全局搜索 **/
    v = rd.get(Cnst.WD_KEY);
    if (v != null && !"".equals(v)) {
        Set<String> fs = getRschable();
        if (fs.size() > 0) {
            BooleanQuery.Builder qx = new BooleanQuery.Builder();
            SearchQuery qs = new SearchQuery();
            for (String fn : fs) {
                Map fc = fields.get(fn);
                if (fc == null)
                    continue;
                qs.settings(/*parser*/
                fc);
                qs.analyser(getAnalyser(fc));
                qx.add(qs.wdr(fn, v), BooleanClause.Occur.SHOULD);
            }
            BooleanQuery qa = qx.build();
            if (!qa.clauses().isEmpty()) {
                qr.add(qa, BooleanClause.Occur.MUST);
                i++;
            }
        }
    }
    // ** 递归条件 **/
    v = rd.get(Cnst.AR_KEY);
    if (v != null) {
        if (r > 2) {
            throw new HongsException(400, "Key '" + Cnst.AR_KEY + "' can not exceed 2 layers");
        }
        Set<Map> set = Synt.asSet(v);
        if (set != null && !set.isEmpty()) {
            for (Map map : set) {
                // 规避 NullPointerException
                if (map == null)
                    continue;
                BooleanQuery.Builder qx = new BooleanQuery.Builder();
                padQry(qx, map, r + 1);
                BooleanQuery qa = qx.build();
                if (!qa.clauses().isEmpty()) {
                    // 权重
                    Query qb = qa;
                    v = map.get(Cnst.WT_REL);
                    if (v != null && !"".equals(v)) {
                        qb = new BoostQuery(qa, Synt.declare(v, 1f));
                    }
                    qr.add(qb, BooleanClause.Occur.MUST);
                    i++;
                }
            }
        }
    }
    v = rd.get(Cnst.NR_KEY);
    if (v != null) {
        if (r > 2) {
            throw new HongsException(400, "Key '" + Cnst.NR_KEY + "' can not exceed 2 layers");
        }
        Set<Map> set = Synt.asSet(v);
        if (set != null && !set.isEmpty()) {
            for (Map map : set) {
                // 规避 NullPointerException
                if (map == null)
                    continue;
                BooleanQuery.Builder qx = new BooleanQuery.Builder();
                padQry(qx, map, r + 1);
                BooleanQuery qa = qx.build();
                if (!qa.clauses().isEmpty()) {
                    // 权重
                    Query qb = qa;
                    v = map.get(Cnst.WT_REL);
                    if (v != null && !"".equals(v)) {
                        qb = new BoostQuery(qa, Synt.declare(v, 1f));
                    }
                    qr.add(qb, BooleanClause.Occur.MUST_NOT);
                    i++;
                    j++;
                }
            }
        }
    }
    v = rd.get(Cnst.OR_KEY);
    if (v != null) {
        if (r > 2) {
            throw new HongsException(400, "Key '" + Cnst.OR_KEY + "' can not exceed 2 layers");
        }
        Set<Map> set = Synt.asSet(v);
        if (set != null && !set.isEmpty()) {
            BooleanQuery.Builder qz = new BooleanQuery.Builder();
            for (Map map : set) {
                // 规避 NullPointerException
                if (map == null)
                    continue;
                BooleanQuery.Builder qx = new BooleanQuery.Builder();
                padQry(qx, map, r + 1);
                BooleanQuery qa = qx.build();
                if (!qa.clauses().isEmpty()) {
                    // 权重
                    Query qb = qa;
                    v = map.get(Cnst.WT_REL);
                    if (v != null && !"".equals(v)) {
                        qb = new BoostQuery(qa, Synt.declare(v, 1f));
                    }
                    qz.add(qb, BooleanClause.Occur.SHOULD);
                }
            }
            BooleanQuery qa = qz.build();
            if (!qa.clauses().isEmpty()) {
                qr.add(qa, BooleanClause.Occur.MUST);
                i++;
            }
        }
    }
    /**
     * 如果全部条件都是 MUST_NOT 会导致取不到数据
     * 故有必要增加一个 MUST all 从而规避这个问题
     */
    if (i > 0 & i == j) {
        qr.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BooleanQuery(org.apache.lucene.search.BooleanQuery) FormSet(io.github.ihongs.action.FormSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ArrayList(java.util.ArrayList) HongsExemption(io.github.ihongs.HongsExemption) BoostQuery(org.apache.lucene.search.BoostQuery) List(java.util.List) ArrayList(java.util.ArrayList) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) HongsException(io.github.ihongs.HongsException) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Collection(java.util.Collection) ParseException(org.apache.lucene.queryparser.classic.ParseException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 100 with QueryParser

use of org.apache.lucene.queryparser.classic.QueryParser in project tutorials by eugenp.

the class InMemoryLuceneIndex method searchIndex.

public List<Document> searchIndex(String inField, String queryString) {
    try {
        Query query = new QueryParser(inField, analyzer).parse(queryString);
        IndexReader indexReader = DirectoryReader.open(memoryIndex);
        IndexSearcher searcher = new IndexSearcher(indexReader);
        TopDocs topDocs = searcher.search(query, 10);
        List<Document> documents = new ArrayList<>();
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            documents.add(searcher.doc(scoreDoc.doc));
        }
        return documents;
    } catch (IOException | ParseException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) IndexReader(org.apache.lucene.index.IndexReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ParseException(org.apache.lucene.queryparser.classic.ParseException) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Aggregations

QueryParser (org.apache.lucene.queryparser.classic.QueryParser)114 Query (org.apache.lucene.search.Query)79 IndexSearcher (org.apache.lucene.search.IndexSearcher)54 Document (org.apache.lucene.document.Document)47 TopDocs (org.apache.lucene.search.TopDocs)39 ParseException (org.apache.lucene.queryparser.classic.ParseException)38 IOException (java.io.IOException)35 Analyzer (org.apache.lucene.analysis.Analyzer)34 ScoreDoc (org.apache.lucene.search.ScoreDoc)31 IndexReader (org.apache.lucene.index.IndexReader)27 ArrayList (java.util.ArrayList)25 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)24 BooleanQuery (org.apache.lucene.search.BooleanQuery)23 TermQuery (org.apache.lucene.search.TermQuery)19 MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)12 HashSet (java.util.HashSet)10 Directory (org.apache.lucene.store.Directory)10 IndexWriter (org.apache.lucene.index.IndexWriter)9 IndexableField (org.apache.lucene.index.IndexableField)8 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)7