Beispiel #1
0
  public static Fuzziness parse(XContentParser parser) throws IOException {
    XContentParser.Token token = parser.currentToken();
    switch (token) {
      case VALUE_STRING:
      case VALUE_NUMBER:
        final String fuzziness = parser.text();
        if (AUTO.asString().equalsIgnoreCase(fuzziness)) {
          return AUTO;
        }
        try {
          final int minimumSimilarity = Integer.parseInt(fuzziness);
          switch (minimumSimilarity) {
            case 0:
              return ZERO;
            case 1:
              return ONE;
            case 2:
              return TWO;
            default:
              return build(fuzziness);
          }
        } catch (NumberFormatException ex) {
          return build(fuzziness);
        }

      default:
        throw new IllegalArgumentException("Can't parse fuzziness on token: [" + token + "]");
    }
  }
 @Override
 protected void doWriteTo(StreamOutput out) throws IOException {
   out.writeString(fieldName);
   out.writeGenericValue(value);
   type.writeTo(out);
   operator.writeTo(out);
   out.writeVInt(slop);
   out.writeVInt(prefixLength);
   out.writeVInt(maxExpansions);
   out.writeBoolean(fuzzyTranspositions);
   out.writeBoolean(lenient);
   zeroTermsQuery.writeTo(out);
   // optional fields
   out.writeOptionalString(analyzer);
   out.writeOptionalString(minimumShouldMatch);
   out.writeOptionalString(fuzzyRewrite);
   if (fuzziness == null) {
     out.writeBoolean(false);
   } else {
     out.writeBoolean(true);
     fuzziness.writeTo(out);
   }
   if (cutoffFrequency == null) {
     out.writeBoolean(false);
   } else {
     out.writeBoolean(true);
     out.writeFloat(cutoffFrequency);
   }
 }
 @Override
 public Query fuzzyQuery(
     String value,
     Fuzziness fuzziness,
     int prefixLength,
     int maxExpansions,
     boolean transpositions) {
   long iValue = ipToLong(value);
   long iSim;
   try {
     iSim = ipToLong(fuzziness.asString());
   } catch (ElasticsearchIllegalArgumentException e) {
     iSim = fuzziness.asLong();
   }
   return NumericRangeQuery.newLongRange(
       names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true);
 }
 @Override
 public Query fuzzyQuery(
     String value,
     Fuzziness fuzziness,
     int prefixLength,
     int maxExpansions,
     boolean transpositions) {
   long iValue = dateMathParser.parse(value, System.currentTimeMillis());
   long iSim;
   try {
     iSim = fuzziness.asTimeValue().millis();
   } catch (Exception e) {
     // not a time format
     iSim = fuzziness.asLong();
   }
   return NumericRangeQuery.newLongRange(
       names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true);
 }
Beispiel #5
0
 public static Fuzziness build(Object fuzziness) {
   if (fuzziness instanceof Fuzziness) {
     return (Fuzziness) fuzziness;
   }
   String string = fuzziness.toString();
   if (AUTO.asString().equalsIgnoreCase(string)) {
     return AUTO;
   }
   return new Fuzziness(string);
 }
 @Override
 public Query fuzzyQuery(
     String value,
     Fuzziness fuzziness,
     int prefixLength,
     int maxExpansions,
     boolean transpositions) {
   int iValue = Integer.parseInt(value);
   int iSim = fuzziness.asInt();
   return NumericRangeQuery.newIntRange(
       names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true);
 }
 @Override
 public Query fuzzyQuery(
     Object value,
     Fuzziness fuzziness,
     int prefixLength,
     int maxExpansions,
     boolean transpositions) {
   byte iValue = parseValue(value);
   byte iSim = fuzziness.asByte();
   return NumericRangeQuery.newIntRange(
       name(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true);
 }
 @Override
 public Query fuzzyQuery(
     String value,
     Fuzziness fuzziness,
     int prefixLength,
     int maxExpansions,
     boolean transpositions) {
   double iValue = Double.parseDouble(value);
   double iSim = fuzziness.asDouble();
   return NumericRangeQuery.newDoubleRange(
       names.indexName(), precisionStep, iValue - iSim, iValue + iSim, true, true);
 }
 @Override
 public Query fuzzyQuery(
     String value,
     Fuzziness fuzziness,
     int prefixLength,
     int maxExpansions,
     boolean transpositions) {
   short iValue = Short.parseShort(value);
   short iSim = fuzziness.asShort();
   return NumericRangeQuery.newIntRange(
       names().indexName(), numericPrecisionStep(), iValue - iSim, iValue + iSim, true, true);
 }
 @Override
 public Query fuzzyQuery(
     Object value,
     Fuzziness fuzziness,
     int prefixLength,
     int maxExpansions,
     boolean transpositions) {
   failIfNotIndexed();
   InetAddress base = parse(value);
   int mask = fuzziness.asInt();
   return XInetAddressPoint.newPrefixQuery(name(), base, mask);
 }
Beispiel #11
0
 protected Query blendTermQuery(Term term, MappedFieldType fieldType) {
   if (fuzziness != null) {
     if (fieldType != null) {
       try {
         Query query =
             fieldType.fuzzyQuery(
                 term.text(), fuzziness, fuzzyPrefixLength, maxExpansions, transpositions);
         if (query instanceof FuzzyQuery) {
           QueryParsers.setRewriteMethod((FuzzyQuery) query, fuzzyRewriteMethod);
         }
         return query;
       } catch (RuntimeException e) {
         return new TermQuery(term);
         // See long comment below about why we're lenient here.
       }
     }
     int edits = fuzziness.asDistance(term.text());
     FuzzyQuery query =
         new FuzzyQuery(term, edits, fuzzyPrefixLength, maxExpansions, transpositions);
     QueryParsers.setRewriteMethod(query, fuzzyRewriteMethod);
     return query;
   }
   if (fieldType != null) {
     /*
      * Its a bit weird to default to lenient here but its the backwards
      * compatible. It makes some sense when you think about what we are
      * doing here: at this point the user has forced an analyzer and
      * passed some string to the match query. We cut it up using the
      * analyzer and then tried to cram whatever we get into the field.
      * lenient=true here means that we try the terms in the query and on
      * the off chance that they are actually valid terms then we
      * actually try them. lenient=false would mean that we blow up the
      * query if they aren't valid terms. "valid" in this context means
      * "parses properly to something of the type being queried." So "1"
      * is a valid number, etc.
      *
      * We use the text form here because we we've received the term from
      * an analyzer that cut some string into text.
      */
     Query query = termQuery(fieldType, term.bytes(), true);
     if (query != null) {
       return query;
     }
   }
   return new TermQuery(term);
 }
  @Override
  public void doXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(NAME);
    builder.startObject(fieldName);

    builder.field(MatchQueryParser.QUERY_FIELD.getPreferredName(), value);
    builder.field(
        MatchQueryParser.TYPE_FIELD.getPreferredName(),
        type.toString().toLowerCase(Locale.ENGLISH));
    builder.field(MatchQueryParser.OPERATOR_FIELD.getPreferredName(), operator.toString());
    if (analyzer != null) {
      builder.field(MatchQueryParser.ANALYZER_FIELD.getPreferredName(), analyzer);
    }
    builder.field(MatchQueryParser.SLOP_FIELD.getPreferredName(), slop);
    if (fuzziness != null) {
      fuzziness.toXContent(builder, params);
    }
    builder.field(MatchQueryParser.PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength);
    builder.field(MatchQueryParser.MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions);
    if (minimumShouldMatch != null) {
      builder.field(
          MatchQueryParser.MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), minimumShouldMatch);
    }
    if (fuzzyRewrite != null) {
      builder.field(MatchQueryParser.FUZZY_REWRITE_FIELD.getPreferredName(), fuzzyRewrite);
    }
    // LUCENE 4 UPGRADE we need to document this & test this
    builder.field(
        MatchQueryParser.FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), fuzzyTranspositions);
    builder.field(MatchQueryParser.LENIENT_FIELD.getPreferredName(), lenient);
    builder.field(
        MatchQueryParser.ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString());
    if (cutoffFrequency != null) {
      builder.field(MatchQueryParser.CUTOFF_FREQUENCY_FIELD.getPreferredName(), cutoffFrequency);
    }
    printBoostAndQueryName(builder);
    builder.endObject();
    builder.endObject();
  }
 @Override
 protected MatchQueryBuilder doReadFrom(StreamInput in) throws IOException {
   MatchQueryBuilder matchQuery = new MatchQueryBuilder(in.readString(), in.readGenericValue());
   matchQuery.type = MatchQuery.Type.readTypeFrom(in);
   matchQuery.operator = Operator.readOperatorFrom(in);
   matchQuery.slop = in.readVInt();
   matchQuery.prefixLength = in.readVInt();
   matchQuery.maxExpansions = in.readVInt();
   matchQuery.fuzzyTranspositions = in.readBoolean();
   matchQuery.lenient = in.readBoolean();
   matchQuery.zeroTermsQuery = MatchQuery.ZeroTermsQuery.readZeroTermsQueryFrom(in);
   // optional fields
   matchQuery.analyzer = in.readOptionalString();
   matchQuery.minimumShouldMatch = in.readOptionalString();
   matchQuery.fuzzyRewrite = in.readOptionalString();
   if (in.readBoolean()) {
     matchQuery.fuzziness = Fuzziness.readFuzzinessFrom(in);
   }
   if (in.readBoolean()) {
     matchQuery.cutoffFrequency = in.readFloat();
   }
   return matchQuery;
 }
  @Override
  public QueryBuilder translate(FuzzyQuery fuzzyQuery) {
    FuzzyQueryBuilder fuzzyQueryBuilder =
        QueryBuilders.fuzzyQuery(fuzzyQuery.getField(), fuzzyQuery.getValue());

    if (fuzzyQuery.getFuzziness() != null) {
      fuzzyQueryBuilder.fuzziness(Fuzziness.build(fuzzyQuery.getFuzziness()));
    }

    if (fuzzyQuery.getMaxExpansions() != null) {
      fuzzyQueryBuilder.maxExpansions(fuzzyQuery.getMaxExpansions());
    }

    if (fuzzyQuery.getPrefixLength() != null) {
      fuzzyQueryBuilder.prefixLength(fuzzyQuery.getPrefixLength());
    }

    if (!fuzzyQuery.isDefaultBoost()) {
      fuzzyQueryBuilder.boost(fuzzyQuery.getBoost());
    }

    return fuzzyQueryBuilder;
  }
 /** Sets the fuzziness used when evaluated to a fuzzy query type. Defaults to "AUTO". */
 public MatchQueryBuilder fuzziness(Object fuzziness) {
   this.fuzziness = Fuzziness.build(fuzziness);
   return this;
 }