This commit is contained in:
Shinsuke Sugaya 2015-05-14 19:20:32 +09:00
parent a08e90adba
commit acb58e7ebe
2 changed files with 188 additions and 39 deletions

View file

@ -130,6 +130,8 @@ public class QueryHelper implements Serializable {
protected boolean useBigram = true;
protected boolean useFuzzyOnTextField = false;
protected String additionalQuery;
protected int maxFilterQueriesForRole = Integer.MAX_VALUE;
@ -431,7 +433,7 @@ public class QueryHelper implements Serializable {
buf.append('*');
}
appendQueryValue(buf, targetWord, isInUrl ? false
: isBigramField(field));
: isBigramField(field), true);
if (isInUrl) {
buf.append('*');
}
@ -444,7 +446,7 @@ public class QueryHelper implements Serializable {
queryBuf.append('*');
}
appendQueryValue(queryBuf, targetWord, isInUrl ? false
: isBigramField(field));
: isBigramField(field), true);
if (isInUrl) {
queryBuf.append('*');
}
@ -569,7 +571,7 @@ public class QueryHelper implements Serializable {
}
protected void appendQueryValue(final StringBuilder buf,
final String query, final boolean useBigram) {
final String query, final boolean useBigram, final boolean useFuzzy) {
// check reserved
boolean reserved = false;
for (final String element : Constants.RESERVED) {
@ -662,7 +664,9 @@ public class QueryHelper implements Serializable {
}
if (fuzzyValue != null) {
buf.append(fuzzyValue);
if (useFuzzy) {
buf.append(fuzzyValue);
}
} else if (proximityValue != null) {
buf.append(proximityValue);
} else if (caretValue != null) {
@ -903,13 +907,14 @@ public class QueryHelper implements Serializable {
if (notOperatorFlag) {
final StringBuilder buf = new StringBuilder(100);
buf.append(prefix);
appendQueryValue(buf, targetWord, isBigramField(field));
appendQueryValue(buf, targetWord, isBigramField(field),
true);
notOperatorList.add(buf.toString());
notOperatorFlag = false;
} else {
queryBuf.append(prefix);
appendQueryValue(queryBuf, targetWord,
isBigramField(field));
isBigramField(field), true);
queryOperandCount++;
}
nonPrefix = true;
@ -986,18 +991,18 @@ public class QueryHelper implements Serializable {
final String value, final String queryLanguage) {
buf.append('(');
buf.append(fieldHelper.titleField).append(':');
appendQueryValue(buf, value, useBigram);
appendQueryValue(buf, value, useBigram, useFuzzyOnTextField);
appendFieldBoostValue(buf, fieldHelper.titleField, value);
buf.append(_OR_);
buf.append(fieldHelper.contentField).append(':');
appendQueryValue(buf, value, useBigram);
appendQueryValue(buf, value, useBigram, useFuzzyOnTextField);
appendFieldBoostValue(buf, fieldHelper.contentField, value);
if (StringUtil.isNotBlank(queryLanguage)) {
final String languageField = "content_" + queryLanguage;
buf.append(_OR_);
buf.append(languageField);
buf.append(':');
appendQueryValue(buf, value, false);
appendQueryValue(buf, value, false, true);
appendFieldBoostValue(buf, languageField, value);
}
buf.append(')');
@ -1331,6 +1336,14 @@ public class QueryHelper implements Serializable {
this.useBigram = useBigram;
}
public boolean isUseFuzzyOnTextField() {
return useFuzzyOnTextField;
}
public void setUseFuzzyOnTextField(boolean useFuzzyOnTextField) {
this.useFuzzyOnTextField = useFuzzyOnTextField;
}
/**
* @return the additionalQuery
*/

View file

@ -392,13 +392,13 @@ public class QueryHelperTest extends S2TestCase {
getRequest().setAttribute(Constants.DEFAULT_OPERATOR, op);
// ~
assertEquals("title:QUERY~ OR content:QUERY~", queryHelper
assertEquals("title:QUERY OR content:QUERY", queryHelper
.buildQuery("QUERY~").getQuery());
assertEquals("(title:QUERY1~ OR content:QUERY1~) " + op
assertEquals("(title:QUERY1 OR content:QUERY1) " + op
+ " (title:QUERY2 OR content:QUERY2)", queryHelper
.buildQuery("QUERY1~ QUERY2").getQuery());
assertEquals("(title:QUERY1~ OR content:QUERY1~) " + op
+ " (title:QUERY2~ OR content:QUERY2~)", queryHelper
assertEquals("(title:QUERY1 OR content:QUERY1) " + op
+ " (title:QUERY2 OR content:QUERY2)", queryHelper
.buildQuery("QUERY1~ QUERY2~").getQuery());
assertEquals("mimetype:QUERY1~",
@ -407,20 +407,20 @@ public class QueryHelperTest extends S2TestCase {
+ " (title:QUERY2 OR content:QUERY2)", queryHelper
.buildQuery("mimetype:QUERY1~ QUERY2").getQuery());
assertEquals("title:QUERY1\\ QUERY2~ OR content:QUERY1\\ QUERY2~",
assertEquals("title:QUERY1\\ QUERY2 OR content:QUERY1\\ QUERY2",
queryHelper.buildQuery("\"QUERY1 QUERY2\"~").getQuery());
assertEquals("title:QUERY1~ OR content:QUERY1~", queryHelper
assertEquals("title:QUERY1 OR content:QUERY1", queryHelper
.buildQuery("\"QUERY1~\"").getQuery());
// ~0.8
assertEquals("title:QUERY~0.8 OR content:QUERY~0.8", queryHelper
assertEquals("title:QUERY OR content:QUERY", queryHelper
.buildQuery("QUERY~0.8").getQuery());
assertEquals("(title:QUERY1~0.8 OR content:QUERY1~0.8) " + op
assertEquals("(title:QUERY1 OR content:QUERY1) " + op
+ " (title:QUERY2 OR content:QUERY2)", queryHelper
.buildQuery("QUERY1~0.8 QUERY2").getQuery());
assertEquals("(title:QUERY1~0.5 OR content:QUERY1~0.5) " + op
+ " (title:QUERY2~0.8 OR content:QUERY2~0.8)", queryHelper
assertEquals("(title:QUERY1 OR content:QUERY1) " + op
+ " (title:QUERY2 OR content:QUERY2)", queryHelper
.buildQuery("QUERY1~0.5 QUERY2~0.8").getQuery());
assertEquals("mimetype:QUERY1~0.8",
@ -429,17 +429,86 @@ public class QueryHelperTest extends S2TestCase {
+ " (title:QUERY2 OR content:QUERY2)", queryHelper
.buildQuery("mimetype:QUERY1~0.8 QUERY2").getQuery());
assertEquals(
"title:QUERY1\\ QUERY2~0.8 OR content:QUERY1\\ QUERY2~0.8",
assertEquals("title:QUERY1\\ QUERY2 OR content:QUERY1\\ QUERY2",
queryHelper.buildQuery("\"QUERY1 QUERY2\"~0.8").getQuery());
assertEquals("title:QUERY1~0.8 OR content:QUERY1~0.8", queryHelper
assertEquals("title:QUERY1 OR content:QUERY1", queryHelper
.buildQuery("\"QUERY1~0.8\"").getQuery());
assertEquals("title:QUERY1~0.8 OR content:QUERY1~0.8", queryHelper
assertEquals("title:QUERY1 OR content:QUERY1", queryHelper
.buildQuery("\"QUERY1~0.8a\"").getQuery());
assertEquals("title:QUERY1~ OR content:QUERY1~", queryHelper
assertEquals("title:QUERY1 OR content:QUERY1", queryHelper
.buildQuery("\"QUERY1~a\"").getQuery());
}
getRequest().setLocale(Locale.JAPANESE);
for (final String op : new String[] { "AND", "OR" }) {
getRequest().setAttribute(Constants.DEFAULT_OPERATOR, op);
// ~
assertEquals("title:QUERY OR content:QUERY OR content_ja:QUERY~",
queryHelper.buildQuery("QUERY~").getQuery());
assertEquals(
"(title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~) "
+ op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2)",
queryHelper.buildQuery("QUERY1~ QUERY2").getQuery());
assertEquals(
"(title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~) "
+ op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2~)",
queryHelper.buildQuery("QUERY1~ QUERY2~").getQuery());
assertEquals("mimetype:QUERY1~",
queryHelper.buildQuery("mimetype:QUERY1~").getQuery());
assertEquals("mimetype:QUERY1~ " + op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2)",
queryHelper.buildQuery("mimetype:QUERY1~ QUERY2")
.getQuery());
assertEquals(
"title:QUERY1\\ QUERY2 OR content:QUERY1\\ QUERY2 OR content_ja:QUERY1\\ QUERY2~",
queryHelper.buildQuery("\"QUERY1 QUERY2\"~").getQuery());
assertEquals(
"title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~",
queryHelper.buildQuery("\"QUERY1~\"").getQuery());
// ~0.8
assertEquals(
"title:QUERY OR content:QUERY OR content_ja:QUERY~0.8",
queryHelper.buildQuery("QUERY~0.8").getQuery());
assertEquals(
"(title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~0.8) "
+ op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2)",
queryHelper.buildQuery("QUERY1~0.8 QUERY2").getQuery());
assertEquals(
"(title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~0.5) "
+ op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2~0.8)",
queryHelper.buildQuery("QUERY1~0.5 QUERY2~0.8").getQuery());
assertEquals("mimetype:QUERY1~0.8",
queryHelper.buildQuery("mimetype:QUERY1~0.8").getQuery());
assertEquals("mimetype:QUERY1~0.8 " + op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2)",
queryHelper.buildQuery("mimetype:QUERY1~0.8 QUERY2")
.getQuery());
assertEquals(
"title:QUERY1\\ QUERY2 OR content:QUERY1\\ QUERY2 OR content_ja:QUERY1\\ QUERY2~0.8",
queryHelper.buildQuery("\"QUERY1 QUERY2\"~0.8").getQuery());
assertEquals(
"title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~0.8",
queryHelper.buildQuery("\"QUERY1~0.8\"").getQuery());
assertEquals(
"title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~0.8",
queryHelper.buildQuery("\"QUERY1~0.8a\"").getQuery());
assertEquals(
"title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~",
queryHelper.buildQuery("\"QUERY1~a\"").getQuery());
}
}
public void test_proximitySearches() {
@ -1075,13 +1144,13 @@ public class QueryHelperTest extends S2TestCase {
getRequest().setAttribute(Constants.DEFAULT_OPERATOR, op);
// ~
assertEquals("title:QUERY~ OR content:QUERY~",
assertEquals("title:QUERY OR content:QUERY",
queryHelper.buildFacetQuery("QUERY~"));
assertEquals("(title:QUERY1~ OR content:QUERY1~) " + op
assertEquals("(title:QUERY1 OR content:QUERY1) " + op
+ " (title:QUERY2 OR content:QUERY2)",
queryHelper.buildFacetQuery("QUERY1~ QUERY2"));
assertEquals("(title:QUERY1~ OR content:QUERY1~) " + op
+ " (title:QUERY2~ OR content:QUERY2~)",
assertEquals("(title:QUERY1 OR content:QUERY1) " + op
+ " (title:QUERY2 OR content:QUERY2)",
queryHelper.buildFacetQuery("QUERY1~ QUERY2~"));
assertEquals("mimetype:QUERY1~",
@ -1090,20 +1159,20 @@ public class QueryHelperTest extends S2TestCase {
+ " (title:QUERY2 OR content:QUERY2)",
queryHelper.buildFacetQuery("mimetype:QUERY1~ QUERY2"));
assertEquals("title:QUERY1\\ QUERY2~ OR content:QUERY1\\ QUERY2~",
assertEquals("title:QUERY1\\ QUERY2 OR content:QUERY1\\ QUERY2",
queryHelper.buildFacetQuery("\"QUERY1 QUERY2\"~"));
assertEquals("title:QUERY1~ OR content:QUERY1~",
assertEquals("title:QUERY1 OR content:QUERY1",
queryHelper.buildFacetQuery("\"QUERY1~\""));
// ~0.8
assertEquals("title:QUERY~0.8 OR content:QUERY~0.8",
assertEquals("title:QUERY OR content:QUERY",
queryHelper.buildFacetQuery("QUERY~0.8"));
assertEquals("(title:QUERY1~0.8 OR content:QUERY1~0.8) " + op
assertEquals("(title:QUERY1 OR content:QUERY1) " + op
+ " (title:QUERY2 OR content:QUERY2)",
queryHelper.buildFacetQuery("QUERY1~0.8 QUERY2"));
assertEquals("(title:QUERY1~0.5 OR content:QUERY1~0.5) " + op
+ " (title:QUERY2~0.8 OR content:QUERY2~0.8)",
assertEquals("(title:QUERY1 OR content:QUERY1) " + op
+ " (title:QUERY2 OR content:QUERY2)",
queryHelper.buildFacetQuery("QUERY1~0.5 QUERY2~0.8"));
assertEquals("mimetype:QUERY1~0.8",
@ -1112,15 +1181,82 @@ public class QueryHelperTest extends S2TestCase {
+ " (title:QUERY2 OR content:QUERY2)",
queryHelper.buildFacetQuery("mimetype:QUERY1~0.8 QUERY2"));
assertEquals(
"title:QUERY1\\ QUERY2~0.8 OR content:QUERY1\\ QUERY2~0.8",
assertEquals("title:QUERY1\\ QUERY2 OR content:QUERY1\\ QUERY2",
queryHelper.buildFacetQuery("\"QUERY1 QUERY2\"~0.8"));
assertEquals("title:QUERY1~0.8 OR content:QUERY1~0.8",
assertEquals("title:QUERY1 OR content:QUERY1",
queryHelper.buildFacetQuery("\"QUERY1~0.8\""));
assertEquals("title:QUERY1~0.8 OR content:QUERY1~0.8",
assertEquals("title:QUERY1 OR content:QUERY1",
queryHelper.buildFacetQuery("\"QUERY1~0.8a\""));
assertEquals("title:QUERY1~ OR content:QUERY1~",
assertEquals("title:QUERY1 OR content:QUERY1",
queryHelper.buildFacetQuery("\"QUERY1~a\""));
}
getRequest().setLocale(Locale.JAPANESE);
for (final String op : new String[] { "AND", "OR" }) {
getRequest().setAttribute(Constants.DEFAULT_OPERATOR, op);
// ~
assertEquals("title:QUERY OR content:QUERY OR content_ja:QUERY~",
queryHelper.buildFacetQuery("QUERY~"));
assertEquals(
"(title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~) "
+ op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2)",
queryHelper.buildFacetQuery("QUERY1~ QUERY2"));
assertEquals(
"(title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~) "
+ op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2~)",
queryHelper.buildFacetQuery("QUERY1~ QUERY2~"));
assertEquals("mimetype:QUERY1~",
queryHelper.buildFacetQuery("mimetype:QUERY1~"));
assertEquals("mimetype:QUERY1~ " + op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2)",
queryHelper.buildFacetQuery("mimetype:QUERY1~ QUERY2"));
assertEquals(
"title:QUERY1\\ QUERY2 OR content:QUERY1\\ QUERY2 OR content_ja:QUERY1\\ QUERY2~",
queryHelper.buildFacetQuery("\"QUERY1 QUERY2\"~"));
assertEquals(
"title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~",
queryHelper.buildFacetQuery("\"QUERY1~\""));
// ~0.8
assertEquals(
"title:QUERY OR content:QUERY OR content_ja:QUERY~0.8",
queryHelper.buildFacetQuery("QUERY~0.8"));
assertEquals(
"(title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~0.8) "
+ op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2)",
queryHelper.buildFacetQuery("QUERY1~0.8 QUERY2"));
assertEquals(
"(title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~0.5) "
+ op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2~0.8)",
queryHelper.buildFacetQuery("QUERY1~0.5 QUERY2~0.8"));
assertEquals("mimetype:QUERY1~0.8",
queryHelper.buildFacetQuery("mimetype:QUERY1~0.8"));
assertEquals("mimetype:QUERY1~0.8 " + op
+ " (title:QUERY2 OR content:QUERY2 OR content_ja:QUERY2)",
queryHelper.buildFacetQuery("mimetype:QUERY1~0.8 QUERY2"));
assertEquals(
"title:QUERY1\\ QUERY2 OR content:QUERY1\\ QUERY2 OR content_ja:QUERY1\\ QUERY2~0.8",
queryHelper.buildFacetQuery("\"QUERY1 QUERY2\"~0.8"));
assertEquals(
"title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~0.8",
queryHelper.buildFacetQuery("\"QUERY1~0.8\""));
assertEquals(
"title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~0.8",
queryHelper.buildFacetQuery("\"QUERY1~0.8a\""));
assertEquals(
"title:QUERY1 OR content:QUERY1 OR content_ja:QUERY1~",
queryHelper.buildFacetQuery("\"QUERY1~a\""));
}
}