1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.query;
17
18 import static org.codelibs.core.stream.StreamUtil.stream;
19
20 import org.apache.logging.log4j.LogManager;
21 import org.apache.logging.log4j.Logger;
22 import org.apache.lucene.index.Term;
23 import org.apache.lucene.search.PhraseQuery;
24 import org.apache.lucene.search.Query;
25 import org.codelibs.fess.Constants;
26 import org.codelibs.fess.entity.QueryContext;
27 import org.codelibs.fess.exception.InvalidQueryException;
28 import org.codelibs.fess.mylasta.direction.FessConfig;
29 import org.codelibs.fess.util.ComponentUtil;
30 import org.lastaflute.core.message.UserMessages;
31 import org.opensearch.index.query.QueryBuilder;
32
33
34
35
36 public class PhraseQueryCommand extends QueryCommand {
37
38
39
40
41 public PhraseQueryCommand() {
42 super();
43 }
44
45 private static final Logger logger = LogManager.getLogger(PhraseQueryCommand.class);
46
47 @Override
48 protected String getQueryClassName() {
49 return PhraseQuery.class.getSimpleName();
50 }
51
52 @Override
53 public QueryBuilder execute(final QueryContext context, final Query query, final float boost) {
54 if (query instanceof final PhraseQuery phraseQuery) {
55 if (logger.isDebugEnabled()) {
56 logger.debug("PhraseQuery: query={}, boost={}", query, boost);
57 }
58 return convertPhraseQuery(context, phraseQuery, boost);
59 }
60 throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
61 "Unknown q: " + query.getClass() + " => " + query);
62 }
63
64
65
66
67
68
69
70
71
72 protected QueryBuilder convertPhraseQuery(final QueryContext context, final PhraseQuery phraseQuery, final float boost) {
73 final Term[] terms = phraseQuery.getTerms();
74 if (terms.length == 0) {
75 throw new InvalidQueryException(messages -> messages.addErrorsInvalidQueryUnknown(UserMessages.GLOBAL_PROPERTY_KEY),
76 "Unknown phrase query: " + phraseQuery);
77 }
78 final FessConfig fessConfig = ComponentUtil.getFessConfig();
79 final String field = terms[0].field();
80 final String[] texts = stream(terms).get(stream -> stream.map(Term::text).toArray(n -> new String[n]));
81
82 return convertPhraseQuery(fessConfig, context, phraseQuery, boost, field, texts);
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96 protected QueryBuilder convertPhraseQuery(final FessConfig fessConfig, final QueryContext context, final PhraseQuery phraseQuery,
97 final float boost, final String field, final String[] texts) {
98 final String text = String.join(" ", texts);
99
100 if (Constants.DEFAULT_FIELD.equals(field)) {
101 context.addFieldLog(field, text);
102 stream(texts).of(stream -> stream.forEach(t -> context.addHighlightedQuery(t)));
103 return buildDefaultQueryBuilder(fessConfig, context, (f, b) -> buildMatchPhraseQuery(f, text).boost(b * boost));
104 }
105
106 if (isSearchField(field)) {
107 context.addFieldLog(field, text);
108 stream(texts).of(stream -> stream.forEach(t -> context.addHighlightedQuery(t)));
109 return buildMatchPhraseQuery(field, text);
110 }
111
112 context.addFieldLog(Constants.DEFAULT_FIELD, text);
113 return buildDefaultQueryBuilder(fessConfig, context, (f, b) -> buildMatchPhraseQuery(f, text).boost(b * boost));
114 }
115
116 }