Fix tests since Lucene 4.2 we can support date math in Fuzzy-Search Syntax

This commit is contained in:
Simon Willnauer 2013-03-07 12:34:25 +01:00
parent a37f1f55cc
commit ebadd9ebbd
2 changed files with 6 additions and 39 deletions

View File

@ -129,45 +129,14 @@ public class MapperQueryParser extends QueryParser {
} }
/** /**
* We override this one so we can get the fuzzy part to be treated as string, so people can do: "age:10~5". Note, * We override this one so we can get the fuzzy part to be treated as string, so people can do: "age:10~5" or "timestamp:2012-10-10~5d"
* we would love to support also "timestamp:2012-10-10~5d", but sadly the parser expects only numbers after the ~,
* hopefully we can change that in Lucene.
*/ */
@Override @Override
Query handleBareTokenQuery(String qfield, Token term, Token fuzzySlop, boolean prefix, boolean wildcard, boolean fuzzy, boolean regexp) throws ParseException { Query handleBareFuzzy(String qfield, Token fuzzySlop, String termImage) throws ParseException {
Query q; if (fuzzySlop.image.length() == 1) {
return getFuzzyQuery(qfield, termImage, Float.toString(fuzzyMinSim));
String termImage = discardEscapeChar(term.image);
if (wildcard) {
q = getWildcardQuery(qfield, term.image);
} else if (prefix) {
q = getPrefixQuery(qfield,
discardEscapeChar(term.image.substring
(0, term.image.length() - 1)));
} else if (regexp) {
q = getRegexpQuery(qfield, term.image.substring(1, term.image.length() - 1));
} else if (fuzzy) {
// float fms = fuzzyMinSim;
// try {
// fms = Float.valueOf(fuzzySlop.image.substring(1)).floatValue();
// } catch (Exception ignored) {
// }
// if (fms < 0.0f) {
// throw new ParseException("Minimum similarity for a FuzzyQuery has to be between 0.0f and 1.0f !");
// } else if (fms >= 1.0f && fms != (int) fms) {
// throw new ParseException("Fractional edit distances are not allowed!");
// }
// q = getFuzzyQuery(qfield, termImage, fms);
if (fuzzySlop.image.length() == 1) {
q = getFuzzyQuery(qfield, termImage, Float.toString(fuzzyMinSim));
} else {
q = getFuzzyQuery(qfield, termImage, fuzzySlop.image.substring(1));
}
} else {
q = getFieldQuery(qfield, termImage, false);
} }
return q; return getFuzzyQuery(qfield, termImage, fuzzySlop.image.substring(1));
} }
@Override @Override

View File

@ -644,13 +644,11 @@ public class SimpleQueryTests extends AbstractNodesTests {
assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1")); assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1"));
// Note, this test fails, i.e returns 0 results, the reason is that Lucene QP only supports numbers after the ~
// once this is changed in lucene to support strings, then this test will fail (good!)
searchResponse = client.prepareSearch() searchResponse = client.prepareSearch()
.setQuery(queryString("date:2012-02-02~1d")) .setQuery(queryString("date:2012-02-02~1d"))
.execute().actionGet(); .execute().actionGet();
assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0));
assertThat(searchResponse.getHits().totalHits(), equalTo(0l)); assertThat(searchResponse.getHits().totalHits(), equalTo(1l));
} }
@Test @Test