|
| 1 | +package se.l4.silo.engine.index.search.internal; |
| 2 | + |
| 3 | +import org.apache.lucene.search.BooleanClause; |
| 4 | +import org.apache.lucene.search.BooleanQuery; |
| 5 | +import org.apache.lucene.search.Query; |
| 6 | +import org.apache.lucene.util.QueryBuilder; |
| 7 | +import org.eclipse.collections.api.factory.Sets; |
| 8 | +import org.eclipse.collections.api.set.ImmutableSet; |
| 9 | + |
| 10 | +import se.l4.silo.StorageException; |
| 11 | +import se.l4.silo.engine.index.search.locales.LocaleSupport; |
| 12 | +import se.l4.silo.engine.index.search.query.QueryEncounter; |
| 13 | + |
| 14 | +public class UserQueryParser |
| 15 | +{ |
| 16 | + private final QueryEncounter<?> encounter; |
| 17 | + private boolean typeAhead; |
| 18 | + private ImmutableSet<String> fields; |
| 19 | + |
| 20 | + private UserQueryParser( |
| 21 | + QueryEncounter<?> encounter |
| 22 | + ) |
| 23 | + { |
| 24 | + fields = Sets.immutable.empty(); |
| 25 | + this.encounter = encounter; |
| 26 | + } |
| 27 | + |
| 28 | + public static UserQueryParser create( |
| 29 | + QueryEncounter<?> encounter |
| 30 | + ) |
| 31 | + { |
| 32 | + return new UserQueryParser(encounter); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Set the fields to match. |
| 37 | + * |
| 38 | + * @param fields |
| 39 | + * @return |
| 40 | + */ |
| 41 | + public UserQueryParser withFields(String... fields) |
| 42 | + { |
| 43 | + this.fields = Sets.immutable.of(fields); |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Set if this should be parsed as a type-ahead query. |
| 49 | + * |
| 50 | + * @param x |
| 51 | + * @return |
| 52 | + */ |
| 53 | + public UserQueryParser withTypeAhead(boolean typeAhead) |
| 54 | + { |
| 55 | + this.typeAhead = typeAhead; |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + public Query parse( |
| 60 | + String q |
| 61 | + ) |
| 62 | + { |
| 63 | + return parse(encounter.currentLanguage(), q); |
| 64 | + } |
| 65 | + |
| 66 | + private Query parse(LocaleSupport locale, String query) |
| 67 | + { |
| 68 | + QueryBuilder builder = new QueryBuilder( |
| 69 | + typeAhead ? locale.getTypeAheadAnalyzer() : locale.getTextAnalyzer() |
| 70 | + ); |
| 71 | + |
| 72 | + boolean inQuote = false; |
| 73 | + BooleanQuery.Builder result = new BooleanQuery.Builder(); |
| 74 | + int last = 0; |
| 75 | + for(int i=0, n=query.length(); i<n; i++) |
| 76 | + { |
| 77 | + char c = query.charAt(i); |
| 78 | + if(c == '"') |
| 79 | + { |
| 80 | + if(inQuote) |
| 81 | + { |
| 82 | + Query fq = getPhraseQuery( |
| 83 | + builder, |
| 84 | + query.substring(last, i) |
| 85 | + ); |
| 86 | + |
| 87 | + if(fq != null) |
| 88 | + { |
| 89 | + result.add(fq, BooleanClause.Occur.MUST); |
| 90 | + } |
| 91 | + } |
| 92 | + else if(last < i-1) |
| 93 | + { |
| 94 | + Query fq = getBooleanQuery( |
| 95 | + builder, |
| 96 | + query.substring(last, i) |
| 97 | + ); |
| 98 | + |
| 99 | + if(fq != null) |
| 100 | + { |
| 101 | + result.add(fq, BooleanClause.Occur.MUST); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + last = i+1; |
| 106 | + inQuote = ! inQuote; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if(inQuote) |
| 111 | + { |
| 112 | + Query fq = getPhraseQuery( |
| 113 | + builder, |
| 114 | + query.substring(last, query.length()) |
| 115 | + ); |
| 116 | + |
| 117 | + if(fq != null) |
| 118 | + { |
| 119 | + result.add(fq, BooleanClause.Occur.MUST); |
| 120 | + } |
| 121 | + } |
| 122 | + else if(last < query.length()) |
| 123 | + { |
| 124 | + Query fq = getBooleanQuery( |
| 125 | + builder, |
| 126 | + query.substring(last, query.length()) |
| 127 | + ); |
| 128 | + |
| 129 | + if(fq != null) |
| 130 | + { |
| 131 | + result.add(fq, BooleanClause.Occur.MUST); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return result.build(); |
| 136 | + } |
| 137 | + |
| 138 | + private Query getBooleanQuery( |
| 139 | + QueryBuilder builder, |
| 140 | + String q |
| 141 | + ) |
| 142 | + { |
| 143 | + if(fields.size() == 1) |
| 144 | + { |
| 145 | + return builder.createBooleanQuery(fields.getAny(), q, BooleanClause.Occur.MUST); |
| 146 | + } |
| 147 | + |
| 148 | + throw new StorageException("No support for querying multiple fields"); |
| 149 | + } |
| 150 | + |
| 151 | + private Query getPhraseQuery( |
| 152 | + QueryBuilder builder, |
| 153 | + String q |
| 154 | + ) |
| 155 | + { |
| 156 | + if(fields.size() == 1) |
| 157 | + { |
| 158 | + return builder.createPhraseQuery(fields.getAny(), q); |
| 159 | + } |
| 160 | + |
| 161 | + throw new StorageException("No support for querying multiple fields"); |
| 162 | + } |
| 163 | +} |
0 commit comments