comparison src/goodjava/lucene/queryparser/SynonymParser.java @ 1458:6b6c11c9164e

goodjava.lucene
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 20 Mar 2020 10:58:53 -0600
parents src/goodjava/queryparser/SynonymParser.java@27efb1fcbcb5
children b04b8fc5f4f4
comparison
equal deleted inserted replaced
1457:a84ce37f3892 1458:6b6c11c9164e
1 package goodjava.lucene.queryparser;
2
3 import java.util.Map;
4 import org.apache.lucene.search.Query;
5 import org.apache.lucene.search.BooleanQuery;
6 import org.apache.lucene.search.BooleanClause;
7 import org.apache.lucene.search.SortField;
8 import goodjava.parser.ParseException;
9
10
11 public class SynonymParser implements FieldParser {
12 private final FieldParser fp;
13 private final Map<String,String[]> synonymMap;
14
15 public SynonymParser(FieldParser fp,Map<String,String[]> synonymMap) {
16 this.fp = fp;
17 this.synonymMap = synonymMap;
18 }
19
20 protected String[] getSynonyms(String query) {
21 return synonymMap.get(query);
22 }
23
24 public Query getQuery(SaneQueryParser qp,String field,String query) throws ParseException {
25 String[] synonyms = getSynonyms(query);
26 if( synonyms == null )
27 return fp.getQuery(qp,field,query);
28 BooleanQuery bq = new BooleanQuery();
29 bq.add( fp.getQuery(qp,field,query), BooleanClause.Occur.SHOULD );
30 for( String s : synonyms ) {
31 bq.add( fp.getQuery(qp,field,s), BooleanClause.Occur.SHOULD );
32 }
33 return bq;
34 }
35
36 public Query getRangeQuery(SaneQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException {
37 return fp.getRangeQuery(qp,field,minQuery,maxQuery,includeMin,includeMax);
38 }
39
40 public SortField getSortField(SaneQueryParser qp,String field,boolean reverse) throws ParseException {
41 return fp.getSortField(qp,field,reverse);
42 }
43 }