Example usage for org.apache.lucene.search.spans SpanQuery toString

List of usage examples for org.apache.lucene.search.spans SpanQuery toString

Introduction

In this page you can find the example usage for org.apache.lucene.search.spans SpanQuery toString.

Prototype

public abstract String toString(String field);

Source Link

Document

Prints a query to a string, with field assumed to be the default field and omitted.

Usage

From source file:nl.inl.blacklab.search.lucene.BLSpanOrQuery.java

License:Apache License

@Override
public String toString(String field) {
    StringBuilder buffer = new StringBuilder();
    buffer.append("spanOr([");
    Iterator<SpanQuery> i = clauses.iterator();
    while (i.hasNext()) {
        SpanQuery clause = i.next();
        buffer.append(clause.toString(field));
        if (i.hasNext()) {
            buffer.append(", ");
        }//from   w  w w .j a  v a2 s. c o  m
    }
    buffer.append("])");
    buffer.append(ToStringUtils.boost(getBoost()));
    return buffer.toString();
}

From source file:nl.inl.blacklab.search.lucene.SpanQueryBase.java

License:Apache License

public String clausesToString(String field, String separator) {
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < clauses.length; i++) {
        SpanQuery clause = clauses[i];
        buffer.append(clause.toString(field));
        if (i != clauses.length - 1) {
            buffer.append(", ");
        }// w w w . j a va2 s . c o m
    }
    buffer.append(ToStringUtils.boost(getBoost()));
    return buffer.toString();
}

From source file:nl.inl.blacklab.tools.QueryTool.java

License:Apache License

/**
 * Parse and execute a query in the current query format.
 *
 * @param query/*from   www. ja  va  2 s.c om*/
 *            the query
 */
private void parseAndExecuteQuery(String query) {
    Timer t = new Timer();
    try {

        // See if we want to choose any random words
        if (query.contains("@@")) {
            StringBuffer resultString = new StringBuffer();
            Pattern regex = Pattern.compile("@@[A-Za-z0-9_\\-]+");
            Matcher regexMatcher = regex.matcher(query);
            while (regexMatcher.find()) {
                // You can vary the replacement text for each match on-the-fly
                String wordListName = regexMatcher.group().substring(2);
                List<String> list = wordLists.get(wordListName);
                if (list == null) {
                    errprintln("Word list '" + wordListName + "' not found!");
                    return;
                }
                int randomIndex = (int) (Math.random() * list.size());
                regexMatcher.appendReplacement(resultString, list.get(randomIndex));
            }
            regexMatcher.appendTail(resultString);
            query = resultString.toString();
        }

        Parser parser = parsers.get(currentParserIndex);
        TextPattern pattern = parser.parse(query);
        if (pattern == null) {
            errprintln("No query to execute.");
            return;
        }
        pattern = pattern.rewrite();
        if (verbose)
            outprintln("TextPattern: " + pattern.toString(searcher, CONTENTS_FIELD));

        // If the query included filter clauses, use those. Otherwise use the global filter, if any.
        Query filterForThisQuery = parser.getIncludedFilterQuery();
        if (filterForThisQuery == null)
            filterForThisQuery = filterQuery;
        Filter filter = filterForThisQuery == null ? null : new QueryWrapperFilter(filterForThisQuery);

        // Execute search
        SpanQuery spanQuery = searcher.createSpanQuery(pattern, filter);
        if (verbose)
            outprintln("SpanQuery: " + spanQuery.toString(CONTENTS_FIELD));
        hits = searcher.find(spanQuery);
        docs = null;
        groups = null;
        collocations = null;
        showWhichGroup = -1;
        showSetting = ShowSetting.HITS;
        firstResult = 0;
        showResultsPage();
        reportTime(t.elapsed());
        if (determineTotalNumberOfHits)
            statInfo = "" + hits.size();
        else
            statInfo = "?";
        commandWasQuery = true;
    } catch (ParseException e) {
        // Parse error
        errprintln("Invalid query: " + e.getMessage());
        errprintln("(Type 'help' for examples or see accompanying documents)");
    } catch (UnsupportedOperationException e) {
        // Unimplemented part of query language used
        errprintln("Cannot execute query; " + e.getMessage());
        errprintln("(Type 'help' for examples or see accompanying documents)");
    }
}

From source file:uk.co.flax.luwak.util.XSpanNearQuery.java

License:Apache License

@Override
public String toString(String field) {
    StringBuilder buffer = new StringBuilder();
    buffer.append("spanNear([");
    Iterator<SpanQuery> i = clauses.iterator();
    while (i.hasNext()) {
        SpanQuery clause = i.next();
        buffer.append(clause.toString(field));
        if (i.hasNext()) {
            buffer.append(", ");
        }//from   w  w w  . j  ava2s . co m
    }
    buffer.append("], ");
    buffer.append(slop);
    buffer.append(", ");
    buffer.append(inOrder);
    buffer.append(")");
    return buffer.toString();
}