Skip to content

Commit 1dd5997

Browse files
committed
_all field, closes #63.
1 parent 1d2d467 commit 1dd5997

32 files changed

+1256
-49
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/action/terms/TermsRequest.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@
2020
package org.elasticsearch.action.terms;
2121

2222
import org.elasticsearch.ElasticSearchIllegalArgumentException;
23-
import org.elasticsearch.action.ActionRequestValidationException;
2423
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequest;
25-
import org.elasticsearch.util.Required;
24+
import org.elasticsearch.index.mapper.AllFieldMapper;
2625

2726
import java.io.DataInput;
2827
import java.io.DataOutput;
2928
import java.io.IOException;
3029

31-
import static org.elasticsearch.action.Actions.*;
32-
3330
/**
3431
* Terms request represent a request to get terms in one or more indices of specific fields and their
3532
* document frequencies (in how many document each term exists).
3633
*
34+
* <p>By default, the "_all" field will be used to extract terms and frequencies.
35+
*
3736
* <p>This is very handy to implement things like tag clouds and auto complete (using {@link #prefix(String)} or
3837
* {@link #regexp(String)}).
3938
*
@@ -103,7 +102,9 @@ public static SortType fromString(String value, SortType defaultSort) {
103102
}
104103
}
105104

106-
private String[] fields;
105+
private static final String[] DEFAULT_FIELDS = new String[]{AllFieldMapper.NAME};
106+
107+
private String[] fields = DEFAULT_FIELDS;
107108

108109
private String from;
109110

@@ -134,20 +135,12 @@ public static SortType fromString(String value, SortType defaultSort) {
134135

135136
/**
136137
* Constructs a new terms requests with the provided indices. Don't pass anything for it to run
137-
* over all the indices. Note, the {@link #fields(String...)} is required.
138+
* over all the indices.
138139
*/
139140
public TermsRequest(String... indices) {
140141
super(indices, null);
141142
}
142143

143-
@Override public ActionRequestValidationException validate() {
144-
ActionRequestValidationException validationException = super.validate();
145-
if (fields == null || fields.length == 0) {
146-
validationException = addValidationError("fields is missing", validationException);
147-
}
148-
return validationException;
149-
}
150-
151144
/**
152145
* The fields within each document which terms will be iterated over and returned with the
153146
* document frequencies.
@@ -158,9 +151,9 @@ public String[] fields() {
158151

159152
/**
160153
* The fields within each document which terms will be iterated over and returned with the
161-
* document frequencies.
154+
* document frequencies. By default will use the "_all" field.
162155
*/
163-
@Required public TermsRequest fields(String... fields) {
156+
public TermsRequest fields(String... fields) {
164157
this.fields = fields;
165158
return this;
166159
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.mapper;
21+
22+
import org.apache.lucene.util.StringHelper;
23+
24+
/**
25+
* @author kimchy (shay.banon)
26+
*/
27+
public interface AllFieldMapper extends FieldMapper<Void>, InternalMapper {
28+
29+
public static final String NAME = StringHelper.intern("_all");
30+
}

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public interface DocumentMapper {
5353

5454
BoostFieldMapper boostMapper();
5555

56+
AllFieldMapper allFieldMapper();
57+
5658
DocumentFieldMappers mappers();
5759

5860
/**
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.mapper.json;
21+
22+
import org.apache.lucene.analysis.Analyzer;
23+
import org.apache.lucene.document.Field;
24+
import org.apache.lucene.document.Fieldable;
25+
import org.elasticsearch.index.analysis.NamedAnalyzer;
26+
import org.elasticsearch.index.mapper.AllFieldMapper;
27+
import org.elasticsearch.index.mapper.MergeMappingException;
28+
import org.elasticsearch.util.json.JsonBuilder;
29+
import org.elasticsearch.util.lucene.Lucene;
30+
31+
import java.io.IOException;
32+
33+
import static org.elasticsearch.util.lucene.all.AllTokenFilter.*;
34+
35+
/**
36+
* @author kimchy (shay.banon)
37+
*/
38+
public class JsonAllFieldMapper extends JsonFieldMapper<Void> implements AllFieldMapper {
39+
40+
public static final String JSON_TYPE = "allField";
41+
42+
public static class Defaults extends JsonFieldMapper.Defaults {
43+
public static final String NAME = AllFieldMapper.NAME;
44+
public static final String INDEX_NAME = AllFieldMapper.NAME;
45+
public static final boolean ENABLED = true;
46+
}
47+
48+
49+
public static class Builder extends JsonFieldMapper.Builder<Builder, JsonAllFieldMapper> {
50+
51+
private boolean enabled = Defaults.ENABLED;
52+
53+
public Builder() {
54+
super(Defaults.NAME);
55+
builder = this;
56+
indexName = Defaults.INDEX_NAME;
57+
}
58+
59+
public Builder enabled(boolean enabled) {
60+
this.enabled = enabled;
61+
return this;
62+
}
63+
64+
@Override public Builder termVector(Field.TermVector termVector) {
65+
return super.termVector(termVector);
66+
}
67+
68+
@Override protected Builder indexAnalyzer(NamedAnalyzer indexAnalyzer) {
69+
return super.indexAnalyzer(indexAnalyzer);
70+
}
71+
72+
@Override protected Builder searchAnalyzer(NamedAnalyzer searchAnalyzer) {
73+
return super.searchAnalyzer(searchAnalyzer);
74+
}
75+
76+
@Override public JsonAllFieldMapper build(BuilderContext context) {
77+
return new JsonAllFieldMapper(name, termVector, omitNorms, omitTermFreqAndPositions,
78+
indexAnalyzer, searchAnalyzer, enabled);
79+
}
80+
}
81+
82+
83+
private boolean enabled;
84+
85+
public JsonAllFieldMapper() {
86+
this(Defaults.NAME, Defaults.TERM_VECTOR, Defaults.OMIT_NORMS, Defaults.OMIT_TERM_FREQ_AND_POSITIONS, null, null, Defaults.ENABLED);
87+
}
88+
89+
protected JsonAllFieldMapper(String name, Field.TermVector termVector, boolean omitNorms, boolean omitTermFreqAndPositions,
90+
NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer, boolean enabled) {
91+
super(new Names(name, name, name, name), Field.Index.ANALYZED, Field.Store.NO, termVector, 1.0f, omitNorms, omitTermFreqAndPositions,
92+
indexAnalyzer, searchAnalyzer);
93+
this.enabled = enabled;
94+
}
95+
96+
public boolean enabled() {
97+
return this.enabled;
98+
}
99+
100+
@Override protected Field parseCreateField(JsonParseContext jsonContext) throws IOException {
101+
if (!enabled) {
102+
return null;
103+
}
104+
Analyzer analyzer = indexAnalyzer();
105+
if (analyzer == null) {
106+
analyzer = jsonContext.docMapper().indexAnalyzer();
107+
if (analyzer == null) {
108+
analyzer = Lucene.STANDARD_ANALYZER;
109+
}
110+
}
111+
return new Field(names.indexName(), allTokenStream(names.indexName(), jsonContext.allEntries().finishTexts(), analyzer), termVector);
112+
}
113+
114+
@Override public Void value(Fieldable field) {
115+
return null;
116+
}
117+
118+
@Override public String valueAsString(Fieldable field) {
119+
return null;
120+
}
121+
122+
@Override public Object valueForSearch(Fieldable field) {
123+
return null;
124+
}
125+
126+
@Override public String indexedValue(String value) {
127+
return null;
128+
}
129+
130+
@Override public String indexedValue(Void value) {
131+
return null;
132+
}
133+
134+
@Override protected String jsonType() {
135+
return JSON_TYPE;
136+
}
137+
138+
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
139+
builder.startObject(JSON_TYPE);
140+
builder.field("enabled", enabled);
141+
builder.field("termVector", termVector.name().toLowerCase());
142+
if (indexAnalyzer != null && !indexAnalyzer.name().startsWith("_")) {
143+
builder.field("indexAnalyzer", indexAnalyzer.name());
144+
}
145+
if (searchAnalyzer != null && !searchAnalyzer.name().startsWith("_")) {
146+
builder.field("searchAnalyzer", searchAnalyzer.name());
147+
}
148+
builder.endObject();
149+
}
150+
151+
@Override public void merge(JsonMapper mergeWith, JsonMergeContext mergeContext) throws MergeMappingException {
152+
// do nothing here, no merging, but also no exception
153+
}
154+
}

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/json/JsonDateFieldMapper.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import java.io.IOException;
3535

3636
/**
37-
* @author kimchy (Shay Banon)
37+
* @author kimchy (shay.banon)
3838
*/
3939
public class JsonDateFieldMapper extends JsonNumberFieldMapper<Long> {
4040

@@ -68,8 +68,10 @@ public Builder dateTimeFormatter(FormatDateTimeFormatter dateTimeFormatter) {
6868
}
6969

7070
@Override public JsonDateFieldMapper build(BuilderContext context) {
71-
return new JsonDateFieldMapper(buildNames(context), dateTimeFormatter,
71+
JsonDateFieldMapper fieldMapper = new JsonDateFieldMapper(buildNames(context), dateTimeFormatter,
7272
precisionStep, index, store, boost, omitNorms, omitTermFreqAndPositions, nullValue);
73+
fieldMapper.includeInAll(includeInAll);
74+
return fieldMapper;
7375
}
7476
}
7577

@@ -137,6 +139,9 @@ protected JsonDateFieldMapper(Names names, FormatDateTimeFormatter dateTimeForma
137139
if (dateAsString == null) {
138140
return null;
139141
}
142+
if (includeInAll == null || includeInAll) {
143+
jsonContext.allEntries().addText(names.fullName(), dateAsString, boost);
144+
}
140145
long value = dateTimeFormatter.parser().parseMillis(dateAsString);
141146
Field field = null;
142147
if (stored()) {
@@ -164,5 +169,8 @@ protected JsonDateFieldMapper(Names names, FormatDateTimeFormatter dateTimeForma
164169
if (nullValue != null) {
165170
builder.field("nullValue", nullValue);
166171
}
172+
if (includeInAll != null) {
173+
builder.field("includeInAll", includeInAll);
174+
}
167175
}
168176
}

0 commit comments

Comments
 (0)