blob: c79933dbbd0dee26095e4279bc0421b07a90352f (
plain) (
blame)
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.querytransform;
import com.yahoo.component.ComponentId;
import com.yahoo.component.chain.dependencies.After;
import com.yahoo.component.chain.dependencies.Before;
import com.yahoo.container.QrSearchersConfig;
import com.yahoo.search.Searcher;
import com.yahoo.processing.request.CompoundName;
import com.yahoo.search.searchchain.Execution;
import java.util.List;
/**
* <p>Detects and removes certain phrases from the query.</p>
*
* @author bratseth
*/
@After("rawQuery")
@Before("transformedQuery")
public class NonPhrasingSearcher extends Searcher {
private static final CompoundName suggestonly=new CompoundName("suggestonly");
private PhraseMatcher phraseMatcher;
public NonPhrasingSearcher(ComponentId id, QrSearchersConfig config) {
super(id);
setupAutomatonFile(config.com().yahoo().prelude().querytransform().NonPhrasingSearcher().automatonfile());
}
/**
* Creates a nonphrasing searcher
*
* @param phraseAutomatonFile the file containing phrases which should be removed
* @throws IllegalStateException if the automata component is unavailable
* in the current environment
* @throws IllegalArgumentException if the file is not found
*/
public NonPhrasingSearcher(String phraseAutomatonFile) {
setupAutomatonFile(phraseAutomatonFile);
}
private void setupAutomatonFile(String phraseAutomatonFile) {
if (phraseAutomatonFile == null || phraseAutomatonFile.trim().equals("")) {
//no file, just use dummy matcher
phraseMatcher = PhraseMatcher.getNullMatcher();
} else {
//use real matcher
phraseMatcher = new PhraseMatcher(phraseAutomatonFile);
}
}
@Override
public com.yahoo.search.Result search(com.yahoo.search.Query query, Execution execution) {
List<PhraseMatcher.Phrase> phrases=phraseMatcher.matchPhrases(query.getModel().getQueryTree().getRoot());
if (phrases!=null && !query.properties().getBoolean(suggestonly, false)) {
remove(phrases);
query.trace("Removing stop words",true,2);
}
return execution.search(query);
}
private void remove(List<PhraseMatcher.Phrase> phrases) {
// Removing the leaf replace phrases first to preserve
// the start index of each replace phrase until removing
for (int i=phrases.size()-1; i>=0; i-- ) {
PhraseMatcher.Phrase phrase= phrases.get(i);
if (phrase.getLength()<phrase.getOwner().getItemCount()) // Don't removeField all
phrase.remove();
}
}
}
|