View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.es.query;
17  
18  import java.io.IOException;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Objects;
22  
23  import org.apache.lucene.search.Query;
24  import org.codelibs.fesen.common.ParseField;
25  import org.codelibs.fesen.common.io.stream.NamedWriteable;
26  import org.codelibs.fesen.common.io.stream.StreamOutput;
27  import org.codelibs.fesen.common.xcontent.ObjectParser;
28  import org.codelibs.fesen.common.xcontent.XContentBuilder;
29  import org.codelibs.fesen.common.xcontent.XContentParser;
30  import org.codelibs.fesen.index.query.AbstractQueryBuilder;
31  import org.codelibs.fesen.index.query.QueryBuilder;
32  import org.codelibs.fesen.index.query.QueryShardContext;
33  
34  public class StoredLtrQueryBuilder extends AbstractQueryBuilder<StoredLtrQueryBuilder> implements NamedWriteable {
35      public static final String NAME = "sltr";
36  
37      public static final ParseField MODEL_NAME = new ParseField("model");
38      public static final ParseField FEATURESET_NAME = new ParseField("featureset");
39      public static final ParseField STORE_NAME = new ParseField("store");
40      public static final ParseField PARAMS = new ParseField("params");
41      public static final ParseField ACTIVE_FEATURES = new ParseField("active_features");
42      private static final ObjectParser<StoredLtrQueryBuilder, Void> PARSER;
43  
44      static {
45          PARSER = new ObjectParser<>(NAME);
46          PARSER.declareString(StoredLtrQueryBuilder::modelName, MODEL_NAME);
47          PARSER.declareString(StoredLtrQueryBuilder::featureSetName, FEATURESET_NAME);
48          PARSER.declareString(StoredLtrQueryBuilder::storeName, STORE_NAME);
49          PARSER.declareField(StoredLtrQueryBuilder::params, XContentParser::map, PARAMS, ObjectParser.ValueType.OBJECT);
50          PARSER.declareStringArray(StoredLtrQueryBuilder::activeFeatures, ACTIVE_FEATURES);
51          PARSER.declareFloat(QueryBuilder::boost, AbstractQueryBuilder.BOOST_FIELD);
52          PARSER.declareString(QueryBuilder::queryName, AbstractQueryBuilder.NAME_FIELD);
53      }
54  
55      private String modelName;
56      private String featureSetName;
57      private String storeName;
58      private Map<String, Object> params;
59      private List<String> activeFeatures;
60  
61      @Override
62      public String getWriteableName() {
63          return NAME;
64      }
65  
66      @Override
67      protected void doWriteTo(final StreamOutput out) throws IOException {
68          out.writeOptionalString(modelName);
69          out.writeOptionalString(featureSetName);
70          out.writeMap(params);
71          out.writeOptionalStringArray(activeFeatures != null ? activeFeatures.toArray(new String[0]) : null);
72          out.writeOptionalString(storeName);
73      }
74  
75      @Override
76      protected void doXContent(final XContentBuilder builder, final Params params) throws IOException {
77          builder.startObject(NAME);
78          if (modelName != null) {
79              builder.field(MODEL_NAME.getPreferredName(), modelName);
80          }
81          if (featureSetName != null) {
82              builder.field(FEATURESET_NAME.getPreferredName(), featureSetName);
83          }
84          if (storeName != null) {
85              builder.field(STORE_NAME.getPreferredName(), storeName);
86          }
87          if (this.params != null && !this.params.isEmpty()) {
88              builder.field(PARAMS.getPreferredName(), this.params);
89          }
90          if (this.activeFeatures != null && !this.activeFeatures.isEmpty()) {
91              builder.field(ACTIVE_FEATURES.getPreferredName(), this.activeFeatures);
92          }
93          printBoostAndQueryName(builder);
94          builder.endObject();
95      }
96  
97      @Override
98      protected Query doToQuery(final QueryShardContext context) throws IOException {
99          throw new UnsupportedOperationException("Query processing is not supported.");
100     }
101 
102     @Override
103     protected boolean doEquals(final StoredLtrQueryBuilder other) {
104         return Objects.equals(modelName, other.modelName) && Objects.equals(featureSetName, other.featureSetName)
105                 && Objects.equals(storeName, other.storeName) && Objects.equals(params, other.params)
106                 && Objects.equals(activeFeatures, other.activeFeatures);
107     }
108 
109     @Override
110     protected int doHashCode() {
111         return Objects.hash(modelName, featureSetName, storeName, params, activeFeatures);
112     }
113 
114     public String modelName() {
115         return modelName;
116     }
117 
118     public StoredLtrQueryBuilder modelName(final String modelName) {
119         this.modelName = Objects.requireNonNull(modelName);
120         return this;
121     }
122 
123     public String featureSetName() {
124         return featureSetName;
125     }
126 
127     public StoredLtrQueryBuilder featureSetName(final String featureSetName) {
128         this.featureSetName = featureSetName;
129         return this;
130     }
131 
132     public String storeName() {
133         return storeName;
134     }
135 
136     public StoredLtrQueryBuilder storeName(final String storeName) {
137         this.storeName = storeName;
138         return this;
139     }
140 
141     public Map<String, Object> params() {
142         return params;
143     }
144 
145     public StoredLtrQueryBuilder params(final Map<String, Object> params) {
146         this.params = Objects.requireNonNull(params);
147         return this;
148     }
149 
150     public List<String> activeFeatures() {
151         return activeFeatures;
152     }
153 
154     public StoredLtrQueryBuilder activeFeatures(final List<String> activeFeatures) {
155         this.activeFeatures = Objects.requireNonNull(activeFeatures);
156         return this;
157     }
158 }