1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.querytransform;
import com.yahoo.component.chain.dependencies.After;
import com.yahoo.component.chain.dependencies.Provides;
import com.yahoo.prelude.query.PredicateQueryItem;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.grouping.request.parser.TokenMgrError;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.searchchain.Execution;
import java.math.BigInteger;
import static com.yahoo.prelude.querytransform.NormalizingSearcher.ACCENT_REMOVAL;
import static com.yahoo.prelude.querytransform.StemmingSearcher.STEMMING;
import static com.yahoo.yolean.Exceptions.toMessageString;
/**
* Searcher that builds a PredicateItem from the &boolean properties and inserts it into a query.
* @author <a href="mailto:magnarn@yahoo-inc.com">Magnar Nedland</a>
*/
@After({ STEMMING, ACCENT_REMOVAL })
@Provides(BooleanSearcher.PREDICATE)
public class BooleanSearcher extends Searcher {
private static final CompoundName FIELD = new CompoundName("boolean.field");
private static final CompoundName ATTRIBUTES = new CompoundName("boolean.attributes");
private static final CompoundName RANGE_ATTRIBUTES = new CompoundName("boolean.rangeAttributes");
public static final String PREDICATE = "predicate";
@Override
public Result search(Query query, Execution execution) {
String fieldName = query.properties().getString(FIELD);
if (fieldName != null) {
return search(query, execution, fieldName);
} else {
if (query.isTraceable(5)) {
query.trace("BooleanSearcher: Nothing added to query", false, 5);
}
}
return execution.search(query);
}
private Result search(Query query, Execution execution, String fieldName) {
String attributes = query.properties().getString(ATTRIBUTES);
String rangeAttributes = query.properties().getString(RANGE_ATTRIBUTES);
if (query.isTraceable(5)) {
query.trace("BooleanSearcher: fieldName(" + fieldName + "), attributes(" + attributes +
"), rangeAttributes(" + rangeAttributes + ")", false, 5);
}
if (attributes != null || rangeAttributes != null) {
try {
addPredicateTerm(query, fieldName, attributes, rangeAttributes);
if (query.isTraceable(4)) {
query.trace("BooleanSearcher: Added boolean operator", true, 4);
}
} catch (TokenMgrError e) {
return new Result(query, ErrorMessage.createInvalidQueryParameter(toMessageString(e)));
}
} else {
if (query.isTraceable(5)) {
query.trace("BooleanSearcher: Nothing added to query", false, 5);
}
}
return execution.search(query);
}
// Adds a boolean term ANDed to the query, based on the supplied properties.
private void addPredicateTerm(Query query, String fieldName, String attributes, String rangeAttributes) {
PredicateQueryItem item = new PredicateQueryItem();
item.setIndexName(fieldName);
new PredicateValueAttributeParser(item).parse(attributes);
new PredicateRangeAttributeParser(item).parse(rangeAttributes);
QueryTreeUtil.andQueryItemWithRoot(query, item);
}
static public class PredicateValueAttributeParser extends BooleanAttributeParser {
private PredicateQueryItem item;
public PredicateValueAttributeParser(PredicateQueryItem item) {
this.item = item;
}
@Override
protected void addAttribute(String attribute, String value) {
item.addFeature(attribute, value);
}
@Override
protected void addAttribute(String attribute, String value, BigInteger subQueryMask) {
item.addFeature(attribute, value, subQueryMask.longValue());
}
}
static private class PredicateRangeAttributeParser extends BooleanAttributeParser {
private PredicateQueryItem item;
public PredicateRangeAttributeParser(PredicateQueryItem item) {
this.item = item;
}
@Override
protected void addAttribute(String attribute, String value) {
item.addRangeFeature(attribute, Long.parseLong(value));
}
@Override
protected void addAttribute(String attribute, String value, BigInteger subQueryMask) {
item.addRangeFeature(attribute, Long.parseLong(value), subQueryMask.longValue());
}
}
}
|