Example usage for org.apache.lucene.util BytesRef BytesRef

List of usage examples for org.apache.lucene.util BytesRef BytesRef

Introduction

In this page you can find the example usage for org.apache.lucene.util BytesRef BytesRef.

Prototype

public BytesRef(CharSequence text) 

Source Link

Document

Initialize the byte[] from the UTF8 bytes for the provided String.

Usage

From source file:dk.statsbiblioteket.netark.dvenabler.wrapper.SortedSetDocValuesWrapper.java

License:Apache License

public long getOrd(String value) {
    int ord = Collections.binarySearch(values, new BytesRef(value));
    if (ord < 0) {
        throw new IllegalStateException("The ord for value '" + value + "' for docID " + docID + " in field '"
                + field + "' could not be located but should always be present");
    }//from   w  ww  .  j a  v a2s  .com
    return ord;
}

From source file:edu.cmu.lti.oaqa.annographix.solr.AnnotEncoderVer3.java

License:Apache License

@Override
public BytesRef encode(char[] buffer, int offset, int length) {
    int sep1pos = -1, sep2pos = -1, sep3pos = -1, sepQty = 0;

    for (int i = 0; i < length; ++i) {
        char c = buffer[offset + i];
        if (c == UtilConst.PAYLOAD_ID_SEP_CHAR) {
            ++sepQty;/*from w ww  . j a  va  2  s .  c o m*/
            if (1 == sepQty)
                sep1pos = i;
            else if (2 == sepQty)
                sep2pos = i;
            else if (3 == sepQty)
                sep3pos = i;
            else {
                String errData = new String(buffer, offset, length);
                throw new RuntimeException("Cannot parse payload input: " + errData);
            }
        }
    }

    int wordStartPos = ArrayUtil.parseInt(buffer, offset, sep1pos);
    int wordEndPos = ArrayUtil.parseInt(buffer, offset + sep1pos + 1, sep2pos - sep1pos - 1);
    int annotId = ArrayUtil.parseInt(buffer, offset + sep2pos + 1, sep3pos - sep2pos - 1);
    int parentId = ArrayUtil.parseInt(buffer, offset + sep3pos + 1, length - sep3pos - 1);

    BytesRef result = new BytesRef(PayloadHelper.encodeInt(wordStartPos));
    result.append(new BytesRef(PayloadHelper.encodeInt(wordEndPos)));
    result.append(new BytesRef(PayloadHelper.encodeInt(annotId)));
    result.append(new BytesRef(PayloadHelper.encodeInt(parentId)));
    return result;
}

From source file:edu.upenn.library.solrplugins.CaseInsensitiveSortingTextField.java

License:Apache License

@Override
public BytesRef normalizeQueryTarget(String val, boolean strict, String fieldName, boolean appendExtraDelim)
        throws IOException {
    TokenStream ts = getQueryAnalyzer().tokenStream(fieldName, val);
    try {//from  ww w . j  a  v  a2s. c o  m
        ts.reset();
        CharTermAttribute termAtt = ts.getAttribute(CharTermAttribute.class);
        TypeAttribute typeAtt = ts.getAttribute(TypeAttribute.class);
        String matchType = strict ? INDEXED_TOKEN_TYPE : NORMALIZED_TOKEN_TYPE;
        while (ts.incrementToken()) {
            if (matchType.equals(typeAtt.type())) {
                BytesRefBuilder ret = new BytesRefBuilder();
                ret.copyChars(termAtt.toString());
                if (!strict || appendExtraDelim) {
                    ret.append(delimBytes, 0, delimBytes.length);
                }
                return ret.get();
            }
        }
        return new BytesRef(BytesRef.EMPTY_BYTES);
    } finally {
        ts.close();
    }
}

From source file:examples.fst.FstTest.java

public static void main(String[] args) throws IOException {
    // Input values (keys). These must be provided to Builder in Unicode sorted order!
    String inputValues[] = { "cat", "dog", "dogs" };
    long outputValues[] = { 5, 7, 12 };

    PositiveIntOutputs outputs = PositiveIntOutputs.getSingleton();
    Builder<Long> builder = new Builder<Long>(INPUT_TYPE.BYTE1, outputs);
    BytesRefBuilder scratchBytes = new BytesRefBuilder();
    IntsRefBuilder scratchInts = new IntsRefBuilder();
    for (int i = 0; i < inputValues.length; i++) {
        scratchBytes.copyChars(inputValues[i]);
        builder.add(Util.toIntsRef(scratchBytes.toBytesRef(), scratchInts), outputValues[i]);
    }/*from  ww  w .j a va2  s . c  om*/
    FST<Long> fst = builder.finish();

    Long value = Util.get(fst, new BytesRef("dog"));
    System.out.println(value); // 7

    // Only works because outputs are also in sorted order
    IntsRef key = Util.getByOutput(fst, 12);
    System.out.println(Util.toBytesRef(key, scratchBytes).utf8ToString()); // dogs

}

From source file:fr.paris.lutece.plugins.calendar.service.search.CalendarLuceneSearchEngine.java

License:Open Source License

/**
 * Return search results//from w  w w  .  j a  v a 2  s. com
 * @param arrayAgendaIds The calendar ids
 * @param arrayCategory the category ids
 * @param strContent The search query
 * @param dateBegin The date begin
 * @param dateEnd The date end
 * @return Results as a collection of SearchResult
 */
public List<CalendarSearchResult> getSearchResults(String[] arrayAgendaIds, String[] arrayCategory,
        String strContent, Date dateBegin, Date dateEnd) {
    ArrayList<CalendarSearchItem> listResults = new ArrayList<CalendarSearchItem>();

    if (arrayAgendaIds == null || arrayAgendaIds.length == 0) {
        return new ArrayList<CalendarSearchResult>();
    }
    IndexSearcher searcher = null;

    //Filter filterRole = getFilterRoles( request );
    Filter filterRole = null;

    try {
        IndexReader ir = DirectoryReader.open(IndexationService.getDirectoryIndex());
        searcher = new IndexSearcher(ir);

        Collection<String> queriesForSearchInContent = new ArrayList<String>();
        Collection<String> queriesForSearchInTitle = new ArrayList<String>();
        Collection<String> fieldsForSearchInContent = new ArrayList<String>();
        Collection<String> fieldsForSearchInTitle = new ArrayList<String>();
        Collection<BooleanClause.Occur> flagsForSearchInContent = new ArrayList<BooleanClause.Occur>();
        Collection<BooleanClause.Occur> flagsForSearchInTitle = new ArrayList<BooleanClause.Occur>();

        //Calendar Id
        if (arrayAgendaIds.length > 0) {
            String strQueryCalendar = OPEN_PARENTHESIS;
            int intMoreCalendar = 0;

            for (String strAgendaId : arrayAgendaIds) {
                strQueryCalendar += (strAgendaId + "_" + Constants.CALENDAR_SHORT_NAME);
                ++intMoreCalendar;

                if ((arrayAgendaIds.length > 1) && (intMoreCalendar < arrayAgendaIds.length)) {
                    strQueryCalendar += (SPACE + OR + SPACE);
                }
            }

            strQueryCalendar += CLOSE_PARENTHESIS;

            Query queryAgendaId = new TermQuery(new Term(Constants.FIELD_CALENDAR_ID, strQueryCalendar));
            queriesForSearchInContent.add(queryAgendaId.toString());
            queriesForSearchInTitle.add(queryAgendaId.toString());
            fieldsForSearchInContent.add(Constants.FIELD_CALENDAR_ID);
            flagsForSearchInContent.add(BooleanClause.Occur.MUST);
            fieldsForSearchInTitle.add(Constants.FIELD_CALENDAR_ID);
            flagsForSearchInTitle.add(BooleanClause.Occur.MUST);
        }

        //category Id
        if ((arrayCategory != null) && (arrayCategory.length > 0)) {
            String strQueryCategory = OPEN_PARENTHESIS;
            int intMoreCategory = 0;

            for (String strCategoryId : arrayCategory) {
                strQueryCategory += strCategoryId;
                ++intMoreCategory;

                if ((arrayCategory.length > 1) && (intMoreCategory < arrayCategory.length)) {
                    strQueryCategory += (SPACE + OR + SPACE);
                }
            }

            strQueryCategory += CLOSE_PARENTHESIS;

            Query queryAgendaId = new TermQuery(new Term(Constants.FIELD_CATEGORY, strQueryCategory));
            queriesForSearchInContent.add(queryAgendaId.toString());
            queriesForSearchInTitle.add(queryAgendaId.toString());
            fieldsForSearchInContent.add(Constants.FIELD_CATEGORY);
            flagsForSearchInContent.add(BooleanClause.Occur.MUST);
            fieldsForSearchInTitle.add(Constants.FIELD_CATEGORY);
            flagsForSearchInTitle.add(BooleanClause.Occur.MUST);
        }

        //Type (=calendar)
        PhraseQuery queryType = new PhraseQuery();
        queryType.add(new Term(SearchItem.FIELD_TYPE, Constants.PLUGIN_NAME));
        queriesForSearchInContent.add(queryType.toString());
        queriesForSearchInTitle.add(queryType.toString());
        fieldsForSearchInContent.add(SearchItem.FIELD_TYPE);
        flagsForSearchInContent.add(BooleanClause.Occur.MUST);
        fieldsForSearchInTitle.add(SearchItem.FIELD_TYPE);
        flagsForSearchInTitle.add(BooleanClause.Occur.MUST);

        //Content
        if (StringUtils.isNotBlank(strContent)) {
            Query queryContent = new TermQuery(new Term(SearchItem.FIELD_CONTENTS, strContent));
            queriesForSearchInTitle.add(queryContent.toString());
            fieldsForSearchInContent.add(SearchItem.FIELD_CONTENTS);
            flagsForSearchInContent.add(BooleanClause.Occur.MUST);

            Query queryTitle = new TermQuery(new Term(SearchItem.FIELD_TITLE, strContent));
            queriesForSearchInContent.add(queryTitle.toString());
            fieldsForSearchInTitle.add(SearchItem.FIELD_TITLE);
            flagsForSearchInTitle.add(BooleanClause.Occur.MUST);
        }

        //Dates
        if ((dateBegin != null) && (dateEnd != null)) {
            BytesRef strDateBegin = new BytesRef(Utils.getDate(dateBegin));
            BytesRef strDateEnd = new BytesRef(Utils.getDate(dateEnd));
            Query queryDate = new TermRangeQuery(SearchItem.FIELD_DATE, strDateBegin, strDateEnd, true, true);
            queriesForSearchInContent.add(queryDate.toString());
            queriesForSearchInTitle.add(queryDate.toString());
            fieldsForSearchInContent.add(SearchItem.FIELD_DATE);
            flagsForSearchInContent.add(BooleanClause.Occur.MUST);
            fieldsForSearchInTitle.add(SearchItem.FIELD_DATE);
            flagsForSearchInTitle.add(BooleanClause.Occur.MUST);
        } else {
            BytesRef strDate = new BytesRef(Utils.getDate(new Date()));
            Query queryDate = new TermRangeQuery(SearchItem.FIELD_DATE, strDate, null, true, true);
            queriesForSearchInContent.add(queryDate.toString());
            queriesForSearchInTitle.add(queryDate.toString());
            fieldsForSearchInContent.add(SearchItem.FIELD_DATE);
            flagsForSearchInContent.add(BooleanClause.Occur.MUST);
            fieldsForSearchInTitle.add(SearchItem.FIELD_DATE);
            flagsForSearchInTitle.add(BooleanClause.Occur.MUST);

        }

        //Search in contents
        Query queryMulti = MultiFieldQueryParser.parse(IndexationService.LUCENE_INDEX_VERSION,
                queriesForSearchInContent.toArray(new String[queriesForSearchInContent.size()]),
                fieldsForSearchInContent.toArray(new String[fieldsForSearchInContent.size()]),
                flagsForSearchInContent.toArray(new BooleanClause.Occur[flagsForSearchInContent.size()]),
                IndexationService.getAnalyser());

        // Get results documents
        TopDocs hits = null;

        int nLimit = Integer.parseInt(AppPropertiesService.getProperty(PROPERTY_RESULTS_LIMIT));
        hits = searcher.search(queryMulti, filterRole, nLimit);

        for (int i = 0; hits.totalHits > i; i++) {
            ScoreDoc hit = hits.scoreDocs[i];
            Document document = searcher.doc(hit.doc);
            CalendarSearchItem si = new CalendarSearchItem(document);
            listResults.add(si);
        }

        //Search in titles
        Query queryMultiTitle = MultiFieldQueryParser.parse(IndexationService.LUCENE_INDEX_VERSION,
                queriesForSearchInTitle.toArray(new String[queriesForSearchInTitle.size()]),
                fieldsForSearchInTitle.toArray(new String[fieldsForSearchInTitle.size()]),
                flagsForSearchInTitle.toArray(new BooleanClause.Occur[flagsForSearchInTitle.size()]),
                IndexationService.getAnalyser());

        // Get results documents
        TopDocs hitsTitle = null;

        hitsTitle = searcher.search(queryMultiTitle, filterRole, nLimit);

        for (int i = 0; hitsTitle.totalHits > i; i++) {
            ScoreDoc hit = hitsTitle.scoreDocs[i];
            Document document = searcher.doc(hit.doc);
            CalendarSearchItem si = new CalendarSearchItem(document);
            listResults.add(si);
        }
    } catch (Exception e) {
        AppLogService.error(e.getMessage(), e);
    }

    return convertList(listResults);
}

From source file:fr.paris.lutece.plugins.directory.service.directorysearch.DirectoryLuceneSearchEngine.java

License:Open Source License

/**
 * {@inheritDoc}//from w w  w.  ja  v  a  2 s  .  c  o  m
 */
@Override
public List<Integer> getSearchResults(HashMap<String, Object> mapQuery) {
    ArrayList<Integer> listResults = new ArrayList<Integer>();
    IndexSearcher searcher = null;

    try {
        searcher = DirectorySearchService.getInstance().getSearcher();

        Collection<String> queries = new ArrayList<String>();
        Collection<String> fields = new ArrayList<String>();
        Collection<BooleanClause.Occur> flags = new ArrayList<BooleanClause.Occur>();

        // contains id directory
        if (mapQuery.containsKey(DirectorySearchItem.FIELD_ID_DIRECTORY)) {
            Query queryIdDirectory = new TermQuery(new Term(DirectorySearchItem.FIELD_ID_DIRECTORY,
                    Integer.toString((Integer) mapQuery.get(DirectorySearchItem.FIELD_ID_DIRECTORY))));
            queries.add(queryIdDirectory.toString());
            fields.add(DirectorySearchItem.FIELD_ID_DIRECTORY);
            flags.add(BooleanClause.Occur.MUST);
        }

        if (mapQuery.containsKey(DirectorySearchItem.FIELD_ID_DIRECTORY_ENTRY)) {
            Query queryIdDirectory = new TermQuery(new Term(DirectorySearchItem.FIELD_ID_DIRECTORY_ENTRY,
                    Integer.toString((Integer) mapQuery.get(DirectorySearchItem.FIELD_ID_DIRECTORY_ENTRY))));
            queries.add(queryIdDirectory.toString());
            fields.add(DirectorySearchItem.FIELD_ID_DIRECTORY_ENTRY);
            flags.add(BooleanClause.Occur.MUST);
        }

        if (mapQuery.containsKey(DirectorySearchItem.FIELD_ID_DIRECTORY_FIELD)) {
            Collection<String> queriesIdDirectoryField = new ArrayList<String>();
            Collection<String> fieldsIdDirectoryField = new ArrayList<String>();
            Collection<BooleanClause.Occur> flagsIdDirectoryField = new ArrayList<BooleanClause.Occur>();

            for (Integer idField : (List<Integer>) mapQuery.get(DirectorySearchItem.FIELD_ID_DIRECTORY_FIELD)) {
                Query queryIdDirectory = new TermQuery(
                        new Term(DirectorySearchItem.FIELD_ID_DIRECTORY_FIELD, Integer.toString(idField)));
                queriesIdDirectoryField.add(queryIdDirectory.toString());
                fieldsIdDirectoryField.add(DirectorySearchItem.FIELD_ID_DIRECTORY_FIELD);
                flagsIdDirectoryField.add(BooleanClause.Occur.SHOULD);
            }

            Query queryMultiIdDirectoryField = MultiFieldQueryParser.parse(
                    IndexationService.LUCENE_INDEX_VERSION,
                    queriesIdDirectoryField.toArray(new String[queriesIdDirectoryField.size()]),
                    queriesIdDirectoryField.toArray(new String[fieldsIdDirectoryField.size()]),
                    flagsIdDirectoryField.toArray(new BooleanClause.Occur[flagsIdDirectoryField.size()]),
                    IndexationService.getAnalyser());

            queries.add(queryMultiIdDirectoryField.toString());
            fields.add(DirectorySearchItem.FIELD_ID_DIRECTORY_FIELD);
            flags.add(BooleanClause.Occur.MUST);
        }

        //contains content
        if (mapQuery.containsKey(DirectorySearchItem.FIELD_CONTENTS)) {
            Query queryContent = new TermQuery(new Term(DirectorySearchItem.FIELD_CONTENTS,
                    (String) mapQuery.get(DirectorySearchItem.FIELD_CONTENTS)));
            queries.add(queryContent.toString());
            fields.add(DirectorySearchItem.FIELD_CONTENTS);
            flags.add(BooleanClause.Occur.MUST);
        }

        //contains date
        if (mapQuery.containsKey(DirectorySearchItem.FIELD_DATE)) {
            Query queryDate = new TermQuery(new Term(DirectorySearchItem.FIELD_DATE, DateTools.dateToString(
                    (Date) mapQuery.get(DirectorySearchItem.FIELD_DATE), DateTools.Resolution.DAY)));
            queries.add(queryDate.toString());
            fields.add(DirectorySearchItem.FIELD_CONTENTS);
            flags.add(BooleanClause.Occur.MUST);
        }

        //contains range date
        if (mapQuery.containsKey(DirectorySearchItem.FIELD_DATE_BEGIN)
                && mapQuery.containsKey(DirectorySearchItem.FIELD_DATE_END)) {
            BytesRef strLowerTerm = new BytesRef(DateTools.dateToString(
                    (Date) mapQuery.get(DirectorySearchItem.FIELD_DATE_BEGIN), DateTools.Resolution.DAY));
            BytesRef strUpperTerm = new BytesRef(DateTools.dateToString(
                    (Date) mapQuery.get(DirectorySearchItem.FIELD_DATE_END), DateTools.Resolution.DAY));
            Query queryRangeDate = new TermRangeQuery(DirectorySearchItem.FIELD_DATE, strLowerTerm,
                    strUpperTerm, true, true);
            queries.add(queryRangeDate.toString());
            fields.add(DirectorySearchItem.FIELD_DATE);
            flags.add(BooleanClause.Occur.MUST);
        }

        //record date creation
        //contains date creation
        if (mapQuery.containsKey(DirectorySearchItem.FIELD_DATE_CREATION)) {
            Query queryDate = new TermQuery(new Term(DirectorySearchItem.FIELD_DATE_CREATION,
                    DateTools.dateToString((Date) mapQuery.get(DirectorySearchItem.FIELD_DATE_CREATION),
                            DateTools.Resolution.DAY)));
            queries.add(queryDate.toString());
            fields.add(DirectorySearchItem.FIELD_DATE_CREATION);
            flags.add(BooleanClause.Occur.MUST);
        }

        //contains range date
        if (mapQuery.containsKey(DirectorySearchItem.FIELD_DATE_CREATION_BEGIN)
                && mapQuery.containsKey(DirectorySearchItem.FIELD_DATE_CREATION_END)) {
            BytesRef strLowerTerm = new BytesRef(
                    DateTools.dateToString((Date) mapQuery.get(DirectorySearchItem.FIELD_DATE_CREATION_BEGIN),
                            DateTools.Resolution.DAY));
            BytesRef strUpperTerm = new BytesRef(
                    DateTools.dateToString((Date) mapQuery.get(DirectorySearchItem.FIELD_DATE_CREATION_END),
                            DateTools.Resolution.DAY));
            Query queryRangeDate = new TermRangeQuery(DirectorySearchItem.FIELD_DATE_CREATION, strLowerTerm,
                    strUpperTerm, true, true);
            queries.add(queryRangeDate.toString());
            fields.add(DirectorySearchItem.FIELD_DATE_CREATION);
            flags.add(BooleanClause.Occur.MUST);
        }

        //record date creation
        //contains date creation
        if (mapQuery.containsKey(DirectorySearchItem.FIELD_DATE_MODIFICATION)) {
            Query queryDate = new TermQuery(new Term(DirectorySearchItem.FIELD_DATE_MODIFICATION,
                    DateTools.dateToString((Date) mapQuery.get(DirectorySearchItem.FIELD_DATE_MODIFICATION),
                            DateTools.Resolution.DAY)));
            queries.add(queryDate.toString());
            fields.add(DirectorySearchItem.FIELD_DATE_MODIFICATION);
            flags.add(BooleanClause.Occur.MUST);
        }

        //contains range modification date
        if (mapQuery.containsKey(DirectorySearchItem.FIELD_DATE_MODIFICATION_BEGIN)
                && mapQuery.containsKey(DirectorySearchItem.FIELD_DATE_MODIFICATION_END)) {
            BytesRef strLowerTerm = new BytesRef(DateTools.dateToString(
                    (Date) mapQuery.get(DirectorySearchItem.FIELD_DATE_MODIFICATION_BEGIN),
                    DateTools.Resolution.DAY));
            BytesRef strUpperTerm = new BytesRef(
                    DateTools.dateToString((Date) mapQuery.get(DirectorySearchItem.FIELD_DATE_MODIFICATION_END),
                            DateTools.Resolution.DAY));
            Query queryRangeDate = new TermRangeQuery(DirectorySearchItem.FIELD_DATE_MODIFICATION, strLowerTerm,
                    strUpperTerm, true, true);
            queries.add(queryRangeDate.toString());
            fields.add(DirectorySearchItem.FIELD_DATE_MODIFICATION);
            flags.add(BooleanClause.Occur.MUST);
        }

        Query queryMulti = MultiFieldQueryParser.parse(IndexationService.LUCENE_INDEX_VERSION,
                queries.toArray(new String[queries.size()]), fields.toArray(new String[fields.size()]),
                flags.toArray(new BooleanClause.Occur[flags.size()]), IndexationService.getAnalyser());

        // Get results documents
        TopDocs topDocs = searcher.search(queryMulti, LuceneSearchEngine.MAX_RESPONSES);
        ScoreDoc[] hits = topDocs.scoreDocs;

        for (int i = 0; i < hits.length; i++) {
            int docId = hits[i].doc;
            Document document = searcher.doc(docId);
            listResults.add(new DirectorySearchItem(document).getIdDirectoryRecord());
        }
    } catch (Exception e) {
        AppLogService.error(e.getMessage(), e);
    }

    return listResults;
}

From source file:fr.paris.lutece.plugins.helpdesk.service.helpdesksearch.HelpdeskLuceneSearchEngine.java

License:Open Source License

/**
 * Return search results/*w w  w.  j ava2  s  .  c  o  m*/
 * @param nIdFaq The id Faq
 * @param strContent The search query
 * @param dateBegin The date begin
 * @param dateEnd The date end
 * @param subject The {@link Subject}
 * @param bSearchSubSubjects true if the query must include sub-subjects
 * @param request The {@link HttpServletRequest}
 * @return Results as a collection of SearchResult
 */
public List<SearchResult> getSearchResults(int nIdFaq, String strContent, Date dateBegin, Date dateEnd,
        Subject subject, boolean bSearchSubSubjects, HttpServletRequest request) {
    ArrayList<SearchItem> listResults = new ArrayList<SearchItem>();
    IndexSearcher searcher = null;

    Query filterRole = getFilterRoles(request);

    try (Directory directory = IndexationService.getDirectoryIndex();
            IndexReader ir = DirectoryReader.open(directory);) {
        searcher = new IndexSearcher(ir);

        Collection<String> queries = new ArrayList<String>();
        Collection<String> fields = new ArrayList<String>();
        Collection<BooleanClause.Occur> flags = new ArrayList<BooleanClause.Occur>();

        //Faq Id
        if (nIdFaq != -1) {
            Query queryFaqId = new TermQuery(new Term(HelpdeskSearchItem.FIELD_FAQ_ID, String.valueOf(nIdFaq)));
            queries.add(queryFaqId.toString());
            fields.add(HelpdeskSearchItem.FIELD_FAQ_ID);
            flags.add(BooleanClause.Occur.MUST);
        }

        //Type (=helpdesk)
        PhraseQuery.Builder queryTypeBuilder = new PhraseQuery.Builder();
        queryTypeBuilder.add(new Term(HelpdeskSearchItem.FIELD_TYPE, HelpdeskPlugin.PLUGIN_NAME));
        PhraseQuery queryType = queryTypeBuilder.build();
        queries.add(queryType.toString());
        fields.add(HelpdeskSearchItem.FIELD_TYPE);
        flags.add(BooleanClause.Occur.MUST);

        //Content
        if ((strContent != null) && !strContent.equals(EMPTY_STRING)) {
            Query queryContent = new TermQuery(new Term(HelpdeskSearchItem.FIELD_CONTENTS, strContent));
            queries.add(queryContent.toString());
            fields.add(HelpdeskSearchItem.FIELD_CONTENTS);
            flags.add(BooleanClause.Occur.MUST);
        }

        //Dates
        if ((dateBegin != null) && (dateEnd != null)) {
            BytesRef strDateBegin = new BytesRef(DateTools.dateToString(dateBegin, DateTools.Resolution.DAY));
            BytesRef strDateEnd = new BytesRef(DateTools.dateToString(dateEnd, DateTools.Resolution.DAY));
            Query queryDate = new TermRangeQuery(HelpdeskSearchItem.FIELD_DATE, strDateBegin, strDateEnd, true,
                    true);
            queries.add(queryDate.toString());
            fields.add(HelpdeskSearchItem.FIELD_DATE);
            flags.add(BooleanClause.Occur.MUST);
        }

        //Subjects
        if ((bSearchSubSubjects) && (subject != null)) {
            Plugin plugin = PluginService.getPlugin(HelpdeskPlugin.PLUGIN_NAME);
            Collection<Term> listSubjects = new ArrayList<Term>();
            getListSubjects(listSubjects, subject, plugin);

            String strQuerySubject = OPEN_PARENTHESIS;

            for (Term term : listSubjects) {
                Query querySubject = new TermQuery(term);
                strQuerySubject += (querySubject.toString() + SPACE);
            }

            strQuerySubject += CLOSE_PARENTHESIS;
            queries.add(strQuerySubject);
            fields.add(HelpdeskSearchItem.FIELD_SUBJECT);
            flags.add(BooleanClause.Occur.MUST);
        } else {
            if ((subject != null)) {
                Query querySubject = new TermQuery(
                        new Term(HelpdeskSearchItem.FIELD_SUBJECT, String.valueOf(subject.getId())));
                queries.add(querySubject.toString());
                fields.add(HelpdeskSearchItem.FIELD_SUBJECT);
                flags.add(BooleanClause.Occur.MUST);
            }
        }

        Query queryMulti = MultiFieldQueryParser.parse((String[]) queries.toArray(new String[queries.size()]),
                (String[]) fields.toArray(new String[fields.size()]),
                (BooleanClause.Occur[]) flags.toArray(new BooleanClause.Occur[flags.size()]),
                IndexationService.getAnalyser());

        BooleanQuery.Builder bQueryMultiBuilder = new BooleanQuery.Builder();
        bQueryMultiBuilder.add(queryMulti, BooleanClause.Occur.MUST);
        if (filterRole != null) {
            bQueryMultiBuilder.add(filterRole, BooleanClause.Occur.FILTER);
        }

        TopDocs topDocs = searcher.search(bQueryMultiBuilder.build(), LuceneSearchEngine.MAX_RESPONSES);

        ScoreDoc[] hits = topDocs.scoreDocs;

        for (int i = 0; i < hits.length; i++) {
            int docId = hits[i].doc;
            Document document = searcher.doc(docId);
            SearchItem si = new SearchItem(document);
            listResults.add(si);
        }
    } catch (Exception e) {
        AppLogService.error(e.getMessage(), e);
    }

    return convertList(listResults);
}

From source file:indexer.Cell.java

List<DocVector> getVectors(IndexReader reader, Terms terms, int numDimensions) throws Exception {
    List<DocVector> containedPoints = new ArrayList<>();

    TermsEnum termsEnum = terms.iterator();
    // seek to a specific term
    boolean found = termsEnum.seekExact(new BytesRef(this.toString()));

    if (found) {//from   w w w .  j  a va  2  s .com
        // enumerate through documents
        DocsEnum docsEnum = termsEnum.docs(null, null);
        int docid;
        while ((docid = docsEnum.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
            Document d = reader.document(docid);
            DocVector dvec = new DocVector(d, numDimensions, DocVector.numIntervals, null);
            containedPoints.add(dvec);
        }
    }

    return containedPoints;
}

From source file:indexer.CompressionUtils.java

static BytesRef compress(String str) {
    ByteArrayOutputStream out = null;
    try {//w  ww  .jav a 2  s . com
        out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
    } catch (Exception ex) {
        return new BytesRef("");
    }
    return out == null ? new BytesRef("") : new BytesRef(out.toByteArray());
}

From source file:info.boytsov.lucene.FreqWordDict.java

License:Open Source License

public Integer getTermPos(String term) {
    BytesRef text = new BytesRef(term.getBytes());

    if (termTextPos.containsKey(text)) {
        return new Integer(termTextPos.get(text));
    }/*  w w  w . ja  v a2s  .c o  m*/

    return null;
}