Example usage for org.apache.lucene.queryparser.classic MultiFieldQueryParser MultiFieldQueryParser

List of usage examples for org.apache.lucene.queryparser.classic MultiFieldQueryParser MultiFieldQueryParser

Introduction

In this page you can find the example usage for org.apache.lucene.queryparser.classic MultiFieldQueryParser MultiFieldQueryParser.

Prototype

public MultiFieldQueryParser(String[] fields, Analyzer analyzer, Map<String, Float> boosts) 

Source Link

Document

Creates a MultiFieldQueryParser.

Usage

From source file:Example.lucene.SearchNHilight.java

public static void main(String[] args) throws IOException, ParseException, InvalidTokenOffsetsException {
    //... Above, create documents with two fields, one with term vectors (tv) and one without (notv)
    Analyzer analyzer = new ThaiAnalyzer(Version.LUCENE_45);

    Directory index = FSDirectory.open(new File("data/indexing"));
    String querystr = args.length > 0 ? args[0] : "golf user";
    // the "title" arg specifies the default field to use
    // when no field is explicitly specified in the query.
    Query query = new MultiFieldQueryParser(Version.LUCENE_45, new String[] { "content" }, analyzer)
            .parse(querystr);// w ww.ja va 2s. c om

    // 3. search
    int hitsPerPage = 10;
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);

    TopDocs hits = searcher.search(query, 10);

    SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
    Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));
    String Preview;
    for (int i = 0; i < 10; i++) {
        int id = hits.scoreDocs[i].doc;
        Document doc = searcher.doc(id);
        String text;
        Preview = "";
        System.out.println(doc.get("url"));
        System.out.println(doc.get("title"));
        text = doc.get("content");
        TokenStream tokenStream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), id, "content",
                analyzer);
        TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);//highlighter.getBestFragments(tokenStream, text, 3, "...");
        int k = 0;
        for (TextFragment frag1 : frag) {
            if ((frag1 != null) && (frag1.getScore() > 0)) {
                Preview += (frag1.toString()) + "...<br>";
                k++;
                // Get 2 Line Preview
                if (k >= 2)
                    break;
            }
        }
        //Term vector
        System.out.println("-------------");
    }
}

From source file:Example.lucene.TestSearch.java

public static void main(String[] args) throws ParseException, IOException {

    Analyzer analyzer = new ThaiAnalyzer(Version.LUCENE_45);

    Directory index = FSDirectory.open(new File("data/indexingonly"));

    // 2. query//from   w w  w.j  av  a  2  s.c om
    String querystr = args.length > 0 ? args[0] : "golf user";

    // the "title" arg specifies the default field to use
    // when no field is explicitly specified in the query.
    Query q = new MultiFieldQueryParser(Version.LUCENE_45, new String[] { "content" }, analyzer)
            .parse(querystr);

    // 3. search
    int hitsPerPage = 10;
    try (IndexReader reader = DirectoryReader.open(index)) {
        IndexSearcher searcher = new IndexSearcher(reader);

        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
        searcher.search(q, collector);

        TopDocs td = collector.topDocs(5);
        ScoreDoc[] hits = td.scoreDocs;

        // 4. display results
        System.out.println("Found " + hits.length + " hits. from " + td.totalHits + " docs.");
        for (int i = 0; i < hits.length; ++i) {
            int docId = hits[i].doc;
            Document d = searcher.doc(docId);
            System.out.println((i + 1) + ". " + d.get("id") + "\t" + d.get("url") + "\t" + d.get("title") + "\t"
                    + d.get("content"));

        }
    }
}

From source file:irlucene.CFCRetrieval.java

public ScoreDoc[] query(QueryData queryData, float titleBoost) {
    HashMap<String, Float> boosts;
    MultiFieldQueryParser queryParser;//from  w  w w.j ava2 s.  c o m
    Query q;
    IndexReader indexReader;
    IndexSearcher indexSearcher;
    TopDocs docs;
    ScoreDoc[] hits = null;
    try {
        boosts = new HashMap<>();
        if (titleBoost != 0) {
            boosts.put("title", titleBoost);
        }
        queryParser = new MultiFieldQueryParser(
                new String[] { "paperNumber", "recordNumber", "acessionNumber", "authors", "title", "source",
                        "majorSubjects", "minorSubjects", "abstractExtract", "references", "citations" },
                analyzer, boosts);
        q = queryParser.parse(queryData.getQuery());
        indexReader = DirectoryReader.open(index);
        indexSearcher = new IndexSearcher(indexReader);
        docs = indexSearcher.search(q, indexReader.numDocs());

        hits = docs.scoreDocs;
        indexReader.close();
    } catch (ParseException | IOException ex) {
        Logger.getLogger(CFCRetrieval.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hits;
}

From source file:irlucene.MEDRetrieval.java

public ScoreDoc[] query(QueryData queryData) {
    HashMap<String, Float> boosts;
    MultiFieldQueryParser queryParser;//from w w  w .jav a 2s. c o m
    Query q;
    IndexReader indexReader;
    IndexSearcher indexSearcher;
    TopDocs docs;
    ScoreDoc[] hits = null;
    try {
        boosts = new HashMap<>();
        queryParser = new MultiFieldQueryParser(new String[] { "id", "content" }, analyzer, boosts);
        q = queryParser.parse(queryData.getQuery());
        indexReader = DirectoryReader.open(index);
        indexSearcher = new IndexSearcher(indexReader);
        docs = indexSearcher.search(q, indexReader.numDocs());

        hits = docs.scoreDocs;
        indexReader.close();
    } catch (ParseException | IOException ex) {
        Logger.getLogger(CFCRetrieval.class.getName()).log(Level.SEVERE, null, ex);
    }
    return hits;
}

From source file:Main.WebAPI.Search.java

/**
 * //from  ww w  . j a v a  2 s .c  om
 * @param args args[0] is a query
 * 
 * @throws IOException
 * @throws ParseException
 * @throws InvalidTokenOffsetsException 
 */

public static void main(String[] args) throws IOException, ParseException, InvalidTokenOffsetsException {
    //... Above, create documents with two fields, one with term vectors (tv) and one without (notv)
    Analyzer analyzer = new ThaiAnalyzer(Version.LUCENE_45);

    Directory index = FSDirectory.open(new File("data/indexing"));
    String querystr = args.length > 0 ? args[0] : "mike lab";
    // the "title" arg specifies the default field to use
    // when no field is explicitly specified in the query.
    Query query = new MultiFieldQueryParser(Version.LUCENE_45, new String[] { "content" }, analyzer)
            .parse(querystr);

    // 3. search
    int hitsPerPage = 10;
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);

    TopDocs hits = searcher.search(query, 10);

    SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter();
    Highlighter highlighter = new Highlighter(htmlFormatter, new QueryScorer(query));
    String Preview;
    for (int i = 0; i < 10; i++) {
        int id = hits.scoreDocs[i].doc;
        Document doc = searcher.doc(id);
        String text;
        Preview = "";
        System.out.println(doc.get("url"));
        System.out.println(doc.get("title"));
        text = doc.get("content");
        TokenStream tokenStream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), id, "content",
                analyzer);
        TextFragment[] frag = highlighter.getBestTextFragments(tokenStream, text, false, 10);//highlighter.getBestFragments(tokenStream, text, 3, "...");
        int k = 0;
        for (TextFragment frag1 : frag) {
            if ((frag1 != null) && (frag1.getScore() > 0)) {
                Preview += (frag1.toString()) + "...<br>";
                k++;
                // Get 2 Line Preview
                if (k >= 2)
                    break;
            }
        }
        //Term vector
        System.out.println("-------------");
    }
}

From source file:net.semanticmetadata.lire.impl.searcher.KeyWordsImageSearcher.java

License:Open Source License

public KeyWordsImageSearcher(int numMaxHits) {
    this.numMaxHits = numMaxHits;
    //???/* w w w.  jav a2s  .  c  o m*/
    qp = new MultiFieldQueryParser(LuceneUtils.LUCENE_VERSION, new String[] { DocumentBuilder.FIELD_NAME_TITLE,
            DocumentBuilder.FIELD_NAME_TAGS, DocumentBuilder.FIELD_NAME_LOCATION }, new IKAnalyzer());
}

From source file:net.ymate.platform.module.search.SearchParser.java

License:Apache License

public static Query parse(String[] fields, String value, Operator op) {
    QueryParser parser = new MultiFieldQueryParser(Searchs.getConfig().getLuceneVersion(), fields,
            Searchs.getConfig().getAnalyzerImpl());
    parser.setDefaultOperator(op);/* w  w  w  . j av  a 2  s . co m*/
    return parse(parser, value);
}

From source file:net.ymate.platform.module.search.support.SearchHelper.java

License:Apache License

public SearchHelper<T> query(String[] fields, String value, Operator opt) {
    QueryParser _parser = new MultiFieldQueryParser(Searchs.getConfig().getLuceneVersion(), fields,
            Searchs.getConfig().getAnalyzerImpl());
    _parser.setDefaultOperator(opt);//from  ww w.  j a va2  s.  c o m
    return query(_parser, value);
}

From source file:org.apache.clerezza.rdf.cris.GenericCondition.java

License:Apache License

/**
 * Creates a new GenericCondition./*ww  w  . ja va 2  s  .c o  m*/
 *
 * @param properties    the properties to search for.
 * @param searchQuery    the search string (pattern).
 * @param escapeQuery    whether to escape special characters in the query string or not.
 */
public GenericCondition(Collection<VirtualProperty> properties, String searchQuery, boolean escapeQuery) {
    fields = new String[properties.size()];
    Iterator<VirtualProperty> it = properties.iterator();
    for (int i = 0; i < properties.size(); ++i) {
        fields[i] = it.next().stringKey;
    }

    if (escapeQuery) {
        searchQuery = QueryParser.escape(searchQuery);
    }
    this.searchQuery = searchQuery;

    this.queryParser = new MultiFieldQueryParser(Version.LUCENE_41, fields,
            new StandardAnalyzer(Version.LUCENE_41));
    queryParser.setAllowLeadingWildcard(true);
}

From source file:org.apache.derby.optional.api.LuceneUtils.java

License:Apache License

/**
 * <p>/* ww  w .j a v a 2s  . c om*/
 * Get the default, classic QueryParser.
 * </p>
 */
public static QueryParser defaultQueryParser(Version version, String[] fieldNames, Analyzer analyzer) {
    return new MultiFieldQueryParser(version, fieldNames, analyzer);
}