private void outputConstraints(boolean skipConstraintValues, StringBuilder sb) { if (constraints.isEmpty()) { return; } sb.append(" WHERE "); Monoflop mf = Monoflop.create(); for (Constraint constraint : constraints) { if (mf.successiveCall()) { sb.append(" AND "); } sb.append(constraint.toString(skipConstraintValues)); } }
/** * Returns the facet filters collected when creating the result * * @return the facet filters defined by the query */ public List<Facet> getFacets() { if (facetsProcessed.firstCall() && response != null) { for (Facet facet : termFacets) { fillFacet(facet); } } return termFacets; }
/** * Creates a new monoflop. * * @return a new monoflop which was not toggled yet. */ public static Monoflop create() { return new Monoflop(); }
private static void reportUnknownType(Output output, String typeName) { output.line("Unknown type: " + typeName); Monoflop mf = Monoflop.create(); for (String name : index.getSchema().getTypeNames()) { if (name.toLowerCase().contains(typeName.toLowerCase())) { if (mf.firstCall()) { output.line("Did you mean one of those: "); } output.line(" * " + name); } } }
private void appendFieldList(Compiler c, boolean applyAliases) { Monoflop mf = Monoflop.create(); List<Mapping> requiredFields = new ArrayList<>(); fields.forEach(field -> { appendToSELECT(c, applyAliases, mf, field, true, requiredFields); }); // make sure that the join fields are always fetched requiredFields.forEach(requiredField -> { appendToSELECT(c, applyAliases, mf, requiredField, false, null); }); }
private void appendToSELECT(Compiler c, boolean applyAliases, Monoflop mf, Mapping field, boolean createJoinFetch, List<Mapping> requiredFieldsCollector) { if (mf.successiveCall()) { c.getSELECTBuilder().append(", "); } Tuple<String, EntityDescriptor> joinInfo = c.determineAlias(field.getParent()); c.getSELECTBuilder().append(joinInfo.getFirst()); c.getSELECTBuilder().append("."); String columnName = joinInfo.getSecond().getProperty(field.getName()).getPropertyName(); c.getSELECTBuilder().append(columnName); if (!c.defaultAlias.equals(joinInfo.getFirst())) { if (applyAliases) { c.getSELECTBuilder().append(" AS "); c.getSELECTBuilder().append(joinInfo.getFirst()); c.getSELECTBuilder().append("_"); c.getSELECTBuilder().append(columnName); } if (createJoinFetch) { c.createJoinFetch(field, fields, requiredFieldsCollector); } } }
/** * Reads and then toggles the monoflop. * <p> * If the monoflop is in its initial state, this will return <tt>false</tt> (and toggle the monoflop). * Therefore all successive class will return <tt>true</tt>. * <p> * This is just an alias for {@link #toggle()}. * * @return <tt>false</tt> if the monoflop was not toggled yet, <tt>true</tt> for all successive calls */ public boolean successiveCall() { return toggle(); }
/** * Reads and then toggles the monoflop. * <p> * If the monoflop is in its initial state, this will return <tt>true</tt> (and toggle the monoflop). * Therefore all successive class will return <tt>false</tt>. * <p> * This is just an alias for {@link #inverseToggle()}. * * @return <tt>true</tt> if the monoflop was not toggled yet, <tt>false</tt> for all successive calls */ public boolean firstCall() { return inverseToggle(); }
/** * Adds a path part to the url. * <p> * Once the first parameter has been added, the path can no longer be modified. * * @param uriPartsToAdd the uri part to add. This should not contain a leading '/' as it is added automatically. If * an array (vararg) is given, all components are appended to the internal {@link * StringBuilder} without any additional characters. * @return the builder itself for fluent method calls */ public URLBuilder addPart(@Nonnull String... uriPartsToAdd) { url.append("/"); for (String uriPart : uriPartsToAdd) { if (Strings.isFilled(uriPart)) { if (questionMark.isToggled()) { throw new IllegalStateException(Strings.apply( "Cannot add '%s'! Parameters where already added to: '%s'.", uriPart, url)); } url.append(uriPart); } } return this; }
@Override public SpanQueryBuilder createSpanQuery() { Monoflop mflop = Monoflop.create(); SpanOrQueryBuilder builder = null; for (Constraint constraint : constraints) { if (constraint instanceof SpanConstraint) { SpanConstraint spanConstraint = (SpanConstraint) constraint; if (mflop.firstCall()) { builder = QueryBuilders.spanOrQuery(spanConstraint.createSpanQuery()); } else { builder.addClause(spanConstraint.createSpanQuery()); } } else { throw new UnsupportedOperationException( "Or-Constraint contains a non span constraint, which is not allowed!"); } } return builder; }
/** * Creates a new constraint which queriess the given field. * * @param input the query to parse * @param defaultField the defaultField to search in * @param tokenizer the function to used for tokenization of the input * @param autoExpand should single token queries be auto expanded. That will put a "*" after the resulting token * but limits the number of expansions to the top 256 terms. */ public RobustQueryParser(String input, String defaultField, Function<String, Iterable<List<String>>> tokenizer, boolean autoExpand) { this.input = Value.of(input).trim(); this.defaultField = defaultField; this.tokenizer = tokenizer; this.autoExpand = autoExpand; this.parsed = Monoflop.create(); }
/** * Toggles the monoflop and returns its state <b>before</b> it was toggled. * <p> * If the monoflop is in its initial state, this will return <tt>true</tt> (and toggle the monoflop). * Therefore all successive class will return <tt>false</tt>. * * @return <tt>true</tt> if the monoflop was not toggled yet, <tt>false</tt> for all successive calls */ public boolean inverseToggle() { return !toggle(); }
@Override public void asString(StringBuilder builder) { Monoflop mf = Monoflop.create(); builder.append("("); inner.stream().filter(Objects::nonNull).forEach(c -> { if (mf.successiveCall()) { builder.append(getCombiner()); } c.asString(builder); }); builder.append(")"); }
protected QueryBuilder transformTokenList(String field, List<String> tokens) { if (tokens.size() == 1) { return QueryBuilders.termQuery(field, tokens.get(0)); } else { Monoflop monoflop = Monoflop.create(); SpanNearQueryBuilder builder = null; for (String token : tokens) { if (monoflop.firstCall()) { builder = QueryBuilders.spanNearQuery(QueryBuilders.spanTermQuery(field, token), 3); } else { builder.addClause(QueryBuilders.spanTermQuery(field, token)); } } return builder; } } }
/** * Adds a parameter to the url. * * @param key the name of the parameter * @param value the value of the parameter . If the given value is <tt>null</tt> an empty parameter will be added. * @param urlEncode <tt>true</tt> if the given value should be url encoded before adding * @return the builder itself for fluent method calls */ public URLBuilder addParameter(@Nonnull String key, @Nullable Object value, boolean urlEncode) { if (questionMark.firstCall()) { url.append("?"); } else { url.append("&"); } url.append(key); url.append("="); String stringValue = value == null ? "" : value.toString(); if (urlEncode) { stringValue = Strings.urlEncode(stringValue); } url.append(stringValue); return this; }
private void where(Compiler compiler) { if (constaints.isEmpty()) { return; } compiler.getWHEREBuilder().append(" WHERE "); Monoflop mf = Monoflop.create(); for (SQLConstraint c : constaints) { if (mf.successiveCall()) { compiler.getWHEREBuilder().append(" AND "); } c.appendSQL(compiler); } }
@Override public SpanNearQueryBuilder createQuery() { Monoflop mflop = Monoflop.create(); SpanNearQueryBuilder builder = null; for (SpanConstraint constraint : constraints) { if (mflop.firstCall()) { builder = QueryBuilders.spanNearQuery(constraint.createSpanQuery(), slop).inOrder(inOrder).boost(boost); } else { builder.addClause(constraint.createSpanQuery()).inOrder(inOrder).boost(boost); } } return builder; }
@Override public QueryBuilder createQuery() { if (parsed.firstCall()) { LookaheadReader reader = new LookaheadReader(new StringReader(input)); QueryBuilder main = parseQuery(reader); if (!reader.current().isEndOfInput()) { IndexAccess.LOG.FINE("Unexpected character in query: " + reader.current()); } // If we cannot compile a query from a non empty input, we probably dropped all short tokens // like a search for "S 8" would be completely dropped. Therefore we resort to "S8". if (main == null && !Strings.isEmpty(input) && input.contains(" ")) { reader = new LookaheadReader(new StringReader(input.replaceAll("\\s", ""))); main = parseQuery(reader); } finishedQuery = main; } return finishedQuery; }
@Override public void appendSQL(SmartQuery.Compiler compiler) { Monoflop mf = Monoflop.create(); compiler.getWHEREBuilder().append("("); inner.stream().filter(Objects::nonNull).forEach(c -> { if (mf.successiveCall()) { compiler.getWHEREBuilder().append(getCombiner()); } c.appendSQL(compiler); }); compiler.getWHEREBuilder().append(")"); }