Package org.apache.lucene.search.vectorhighlight

Examples of org.apache.lucene.search.vectorhighlight.FastVectorHighlighter


    TopDocs topDocs = indexSearcher.search(new TermQuery(new Term("dscr", "입니다")), 3);
   
    System.out.println("ddd : " + indexReader.document(0));
    System.out.println(topDocs.totalHits);

    FastVectorHighlighter highlighter = new FastVectorHighlighter();
   
    System.out.println("field query : " + highlighter.getFieldQuery(new TermQuery(new Term("dscr", "입니다"))));
   
    //System.out.println(topDocs.scoreDocs[0].doc);
    String fragment = highlighter.getBestFragment(highlighter.getFieldQuery(new TermQuery(new Term("dscr", "텍스트"))),
        indexReader, topDocs.scoreDocs[0].doc, "dscr", 30);
   
    System.out.println(fragment);
   
//        assertThat(topDocs.totalHits, equalTo(1));
View Full Code Here


        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);
        System.out.println(topDocs.totalHits);
        
        FastVectorHighlighter highlighter = new FastVectorHighlighter();
        String fragment = highlighter.getBestFragment(highlighter.getFieldQuery(new TermQuery(new Term("content", "big"))),
            reader, topDocs.scoreDocs[0].doc, "content", 200);
      
        System.out.println(fragment);
    }
View Full Code Here

  public int numToHighlight() {
    return numToHighlight;
  }
 
  protected BenchmarkHighlighter getBenchmarkHighlighter(Query q){
    highlighter = new FastVectorHighlighter( false, false );
    final FieldQuery fq = highlighter.getFieldQuery( q );
    return new BenchmarkHighlighter(){
      public int doHighlight(IndexReader reader, int doc, String field,
          Document document, Analyzer analyzer, String text) throws Exception {
        String[] fragments = highlighter.getBestFragments(fq, reader, doc, field, fragSize, maxFrags);
View Full Code Here

    return numToHighlight;
  }
 
  @Override
  protected BenchmarkHighlighter getBenchmarkHighlighter(Query q){
    highlighter = new FastVectorHighlighter( false, false );
    final Query myq = q;
    return new BenchmarkHighlighter(){
      @Override
      public int doHighlight(IndexReader reader, int doc, String field,
          Document document, Analyzer analyzer, String text) throws Exception {
View Full Code Here

    return numToHighlight;
  }
 
  @Override
  protected BenchmarkHighlighter getBenchmarkHighlighter(Query q){
    highlighter = new FastVectorHighlighter( false, false );
    final Query myq = q;
    return new BenchmarkHighlighter(){
      @Override
      public int doHighlight(IndexReader reader, int doc, String field,
          Document document, Analyzer analyzer, String text) throws Exception {
View Full Code Here

        final IndexState state = getState(req, resp);
        if (state == null)
            return;
        final IndexSearcher searcher = state.borrowSearcher(isStaleOk(req));
        final String etag = state.getEtag();
        final FastVectorHighlighter fvh = new FastVectorHighlighter(true, true);
        final JSONArray result = new JSONArray();
        try {
            if (state.notModified(req)) {
                resp.setStatus(304);
                return;
            }
            for (final String queryString : getQueryStrings(req)) {
                final Analyzer analyzer = state.analyzer(req.getParameter("analyzer"));
                final Operator operator = "and".equalsIgnoreCase(req.getParameter("default_operator"))
                        ? Operator.AND : Operator.OR;
                final Query q = state.parse(queryString, operator, analyzer);

                final JSONObject queryRow = new JSONObject();
                queryRow.put("q", q.toString());
                if (getBooleanParameter(req, "debug")) {
                    queryRow.put("plan", QueryPlan.toPlan(q));
                    queryRow.put("analyzer", analyzer.getClass());
                }
                queryRow.put("etag", etag);
                if (getBooleanParameter(req, "rewrite")) {
                    final Query rewritten_q = q.rewrite(searcher
                            .getIndexReader());
                    queryRow.put("rewritten_q", rewritten_q.toString());

                    final JSONObject freqs = new JSONObject();

                    final Set<Term> terms = new HashSet<Term>();
                    rewritten_q.extractTerms(terms);
                    for (final Object term : terms) {
                        final int freq = searcher.getIndexReader().docFreq((Term) term);
                        freqs.put(term.toString(), freq);
                    }
                    queryRow.put("freqs", freqs);
                } else {
                    // Perform the search.
                    final TopDocs td;
                    final StopWatch stopWatch = new StopWatch();

                    final boolean include_docs = getBooleanParameter(req,
                            "include_docs");
                    final int highlights = getIntParameter(req, "highlights", 0);
                    final int highlight_length = max(getIntParameter(req, "highlight_length", 18), 18); // min for fast term vector highlighter is 18
                    final boolean include_termvectors = getBooleanParameter(req, "include_termvectors");
                    final int limit = getIntParameter(req, "limit",
                            ini.getInt("lucene.limit", 25));
                    final Sort sort = CustomQueryParser.toSort(req
                            .getParameter("sort"));
                    final int skip = getIntParameter(req, "skip", 0);

                    final Set<String> fieldsToLoad;
                    if (req.getParameter("include_fields") == null) {
                        fieldsToLoad = null;
                    } else {
                        final String[] fields = Utils.splitOnCommas(
                                req.getParameter("include_fields"));
                        final List<String> list = Arrays.asList(fields);
                        fieldsToLoad = new HashSet<String>(list);
                    }

                    if (sort == null) {
                        td = searcher.search(q, null, skip + limit);
                    } else {
                        td = searcher.search(q, null, skip + limit, sort);
                    }
                    stopWatch.lap("search");

                    // Fetch matches (if any).
                    final int max = Math.max(0, Math.min(td.totalHits - skip,
                            limit));
                    final JSONArray rows = new JSONArray();
                    final String[] fetch_ids = new String[max];
                    for (int i = skip; i < skip + max; i++) {
                        final Document doc;
                        if (fieldsToLoad == null) {
                            doc = searcher.doc(td.scoreDocs[i].doc);
                        } else {
                            doc = searcher.doc(td.scoreDocs[i].doc, fieldsToLoad);
                        }

                        final JSONObject row = new JSONObject();
                        final JSONObject fields = new JSONObject();
                        final JSONObject highlight_rows = new JSONObject();

                        // Include stored fields.
                        for (final IndexableField f : doc.getFields()) {
                            if (!f.fieldType().stored()) {
                                continue;
                            }
                            final String name = f.name();
                            final Object value;
                            if (f.numericValue() != null) {
                                value = f.numericValue();
                            } else {
                                value = f.stringValue();
                            }
                            if (value != null) {
                                if ("_id".equals(name)) {
                                    row.put("id", value);
                                } else {
                                    if (!fields.has(name)) {
                                        fields.put(name, value);
                                    } else {
                                        final Object obj = fields.get(name);
                                        if (obj instanceof String || obj instanceof Number) {
                                            final JSONArray arr = new JSONArray();
                                            arr.put(obj);
                                            arr.put(value);
                                            fields.put(name, arr);
                                        } else {
                                            assert obj instanceof JSONArray;
                                            ((JSONArray) obj).put(value);
                                        }
                                    }

                                    if (highlights > 0) {
                                        String[] frags = fvh.getBestFragments(fvh.getFieldQuery(q), searcher.getIndexReader(), td.scoreDocs[i].doc, name, highlight_length, highlights);
                                        highlight_rows.put(name, frags);
                                    }
                                }
                            }
                        }
View Full Code Here

    return numToHighlight;
  }
 
  @Override
  protected BenchmarkHighlighter getBenchmarkHighlighter(Query q){
    highlighter = new FastVectorHighlighter( false, false );
    final Query myq = q;
    return new BenchmarkHighlighter(){
      @Override
      public int doHighlight(IndexReader reader, int doc, String field,
          Document document, Analyzer analyzer, String text) throws Exception {
View Full Code Here

        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);

        assertThat(topDocs.totalHits, equalTo(1));

        FastVectorHighlighter highlighter = new FastVectorHighlighter();
        String fragment = highlighter.getBestFragment(highlighter.getFieldQuery(new TermQuery(new Term("content", "bad"))),
                reader, topDocs.scoreDocs[0].doc, "content", 30);
        assertThat(fragment, notNullValue());
        assertThat(fragment, equalTo("the big <b>bad</b> dog"));
    }
View Full Code Here

        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);

        assertThat(topDocs.totalHits, equalTo(1));

        FastVectorHighlighter highlighter = new FastVectorHighlighter();

        PrefixQuery prefixQuery = new PrefixQuery(new Term("content", "ba"));
        assertThat(prefixQuery.getRewriteMethod().getClass().getName(), equalTo(PrefixQuery.CONSTANT_SCORE_FILTER_REWRITE.getClass().getName()));
        String fragment = highlighter.getBestFragment(highlighter.getFieldQuery(prefixQuery),
                reader, topDocs.scoreDocs[0].doc, "content", 30);
        assertThat(fragment, nullValue());

        prefixQuery.setRewriteMethod(PrefixQuery.SCORING_BOOLEAN_QUERY_REWRITE);
        Query rewriteQuery = prefixQuery.rewrite(reader);
        fragment = highlighter.getBestFragment(highlighter.getFieldQuery(rewriteQuery),
                reader, topDocs.scoreDocs[0].doc, "content", 30);
        assertThat(fragment, notNullValue());

        // now check with the custom field query
        prefixQuery = new PrefixQuery(new Term("content", "ba"));
        assertThat(prefixQuery.getRewriteMethod().getClass().getName(), equalTo(PrefixQuery.CONSTANT_SCORE_FILTER_REWRITE.getClass().getName()));
        fragment = highlighter.getBestFragment(new CustomFieldQuery(prefixQuery, reader, highlighter),
                reader, topDocs.scoreDocs[0].doc, "content", 30);
        assertThat(fragment, notNullValue());
    }
View Full Code Here

        IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);

        assertThat(topDocs.totalHits, equalTo(1));

        FastVectorHighlighter highlighter = new FastVectorHighlighter();
        String fragment = highlighter.getBestFragment(highlighter.getFieldQuery(new TermQuery(new Term("content", "bad"))),
                reader, topDocs.scoreDocs[0].doc, "content", 30);
        assertThat(fragment, nullValue());
    }
View Full Code Here

TOP

Related Classes of org.apache.lucene.search.vectorhighlight.FastVectorHighlighter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.