Skip to content

Commit 4c13a9d

Browse files
committed
allow to also store the all field
1 parent d8ef200 commit 4c13a9d

File tree

14 files changed

+345
-35
lines changed

14 files changed

+345
-35
lines changed

.idea/libraries/lucene.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/elasticsearch/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ dependencies {
4747
compile 'org.apache.lucene:lucene-core:3.0.1'
4848
compile 'org.apache.lucene:lucene-analyzers:3.0.1'
4949
compile 'org.apache.lucene:lucene-queries:3.0.1'
50+
compile 'org.apache.lucene:lucene-fast-vector-highlighter:3.0.1'
5051

5152
compile('jgroups:jgroups:2.9.0.GA') { transitive = false }
5253
compile('org.jboss.netty:netty:3.1.5.GA') { transitive = false }

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
package org.elasticsearch.index.mapper.json;
2121

2222
import org.apache.lucene.analysis.Analyzer;
23+
import org.apache.lucene.analysis.TokenStream;
2324
import org.apache.lucene.document.Field;
2425
import org.apache.lucene.document.Fieldable;
2526
import org.elasticsearch.index.analysis.NamedAnalyzer;
2627
import org.elasticsearch.index.mapper.AllFieldMapper;
28+
import org.elasticsearch.index.mapper.DocumentMapper;
2729
import org.elasticsearch.index.mapper.MergeMappingException;
2830
import org.elasticsearch.util.json.JsonBuilder;
2931
import org.elasticsearch.util.lucene.Lucene;
32+
import org.elasticsearch.util.lucene.all.AllAnalyzer;
3033

3134
import java.io.IOException;
3235

@@ -61,6 +64,10 @@ public Builder enabled(boolean enabled) {
6164
return this;
6265
}
6366

67+
@Override public Builder store(Field.Store store) {
68+
return super.store(store);
69+
}
70+
6471
@Override public Builder termVector(Field.TermVector termVector) {
6572
return super.termVector(termVector);
6673
}
@@ -74,21 +81,23 @@ public Builder enabled(boolean enabled) {
7481
}
7582

7683
@Override public JsonAllFieldMapper build(BuilderContext context) {
77-
return new JsonAllFieldMapper(name, termVector, omitNorms, omitTermFreqAndPositions,
84+
return new JsonAllFieldMapper(name, store, termVector, omitNorms, omitTermFreqAndPositions,
7885
indexAnalyzer, searchAnalyzer, enabled);
7986
}
8087
}
8188

8289

8390
private boolean enabled;
8491

92+
private AllAnalyzer allAnalyzer;
93+
8594
public JsonAllFieldMapper() {
86-
this(Defaults.NAME, Defaults.TERM_VECTOR, Defaults.OMIT_NORMS, Defaults.OMIT_TERM_FREQ_AND_POSITIONS, null, null, Defaults.ENABLED);
95+
this(Defaults.NAME, Defaults.STORE, Defaults.TERM_VECTOR, Defaults.OMIT_NORMS, Defaults.OMIT_TERM_FREQ_AND_POSITIONS, null, null, Defaults.ENABLED);
8796
}
8897

89-
protected JsonAllFieldMapper(String name, Field.TermVector termVector, boolean omitNorms, boolean omitTermFreqAndPositions,
98+
protected JsonAllFieldMapper(String name, Field.Store store, Field.TermVector termVector, boolean omitNorms, boolean omitTermFreqAndPositions,
9099
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,
100+
super(new Names(name, name, name, name), Field.Index.ANALYZED, store, termVector, 1.0f, omitNorms, omitTermFreqAndPositions,
92101
indexAnalyzer, searchAnalyzer);
93102
this.enabled = enabled;
94103
}
@@ -101,14 +110,30 @@ public boolean enabled() {
101110
if (!enabled) {
102111
return null;
103112
}
104-
Analyzer analyzer = indexAnalyzer();
113+
// reset the entries
114+
jsonContext.allEntries().reset();
115+
116+
Analyzer analyzer = findAnalyzer(jsonContext.docMapper());
117+
TokenStream tokenStream = allTokenStream(names.indexName(), jsonContext.allEntries(), analyzer);
118+
if (stored()) {
119+
// TODO when its possible to pass char[] to field, we can optimize
120+
Field field = new Field(names.indexName(), jsonContext.allEntries().buildText(), store, index, termVector);
121+
field.setTokenStream(tokenStream);
122+
return field;
123+
} else {
124+
return new Field(names.indexName(), tokenStream, termVector);
125+
}
126+
}
127+
128+
private Analyzer findAnalyzer(DocumentMapper docMapper) {
129+
Analyzer analyzer = indexAnalyzer;
105130
if (analyzer == null) {
106-
analyzer = jsonContext.docMapper().indexAnalyzer();
131+
analyzer = docMapper.indexAnalyzer();
107132
if (analyzer == null) {
108133
analyzer = Lucene.STANDARD_ANALYZER;
109134
}
110135
}
111-
return new Field(names.indexName(), allTokenStream(names.indexName(), jsonContext.allEntries().finishTexts(), analyzer), termVector);
136+
return analyzer;
112137
}
113138

114139
@Override public Void value(Fieldable field) {
@@ -138,6 +163,7 @@ public boolean enabled() {
138163
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
139164
builder.startObject(JSON_TYPE);
140165
builder.field("enabled", enabled);
166+
builder.field("store", store.name().toLowerCase());
141167
builder.field("termVector", termVector.name().toLowerCase());
142168
if (indexAnalyzer != null && !indexAnalyzer.name().startsWith("_")) {
143169
builder.field("indexAnalyzer", indexAnalyzer.name());

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.codehaus.jackson.JsonFactory;
2525
import org.codehaus.jackson.JsonParser;
2626
import org.codehaus.jackson.JsonToken;
27+
import org.elasticsearch.index.analysis.NamedAnalyzer;
2728
import org.elasticsearch.index.mapper.*;
2829
import org.elasticsearch.util.Nullable;
2930
import org.elasticsearch.util.Preconditions;
@@ -58,9 +59,9 @@ public static class Builder {
5859

5960
private JsonAllFieldMapper allFieldMapper = new JsonAllFieldMapper();
6061

61-
private Analyzer indexAnalyzer;
62+
private NamedAnalyzer indexAnalyzer;
6263

63-
private Analyzer searchAnalyzer;
64+
private NamedAnalyzer searchAnalyzer;
6465

6566
private final JsonObjectMapper rootObjectMapper;
6667

@@ -107,7 +108,7 @@ public Builder mappingSource(String mappingSource) {
107108
return this;
108109
}
109110

110-
public Builder indexAnalyzer(Analyzer indexAnalyzer) {
111+
public Builder indexAnalyzer(NamedAnalyzer indexAnalyzer) {
111112
this.indexAnalyzer = indexAnalyzer;
112113
return this;
113114
}
@@ -116,7 +117,7 @@ public boolean hasIndexAnalyzer() {
116117
return indexAnalyzer != null;
117118
}
118119

119-
public Builder searchAnalyzer(Analyzer searchAnalyzer) {
120+
public Builder searchAnalyzer(NamedAnalyzer searchAnalyzer) {
120121
this.searchAnalyzer = searchAnalyzer;
121122
return this;
122123
}
@@ -393,6 +394,6 @@ void addFieldMapper(FieldMapper fieldMapper) {
393394
}
394395

395396
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
396-
rootObjectMapper.toJson(builder, params);
397+
rootObjectMapper.toJson(builder, params, allFieldMapper);
397398
}
398399
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@ private void serializeValue(JsonParseContext jsonContext, String currentFieldNam
411411
}
412412

413413
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
414+
toJson(builder, params, null);
415+
}
416+
417+
public void toJson(JsonBuilder builder, Params params, JsonMapper... additionalMappers) throws IOException {
414418
builder.startObject(name);
415419
builder.field("type", JSON_TYPE);
416420
builder.field("dynamic", dynamic);
@@ -434,6 +438,11 @@ private void serializeValue(JsonParseContext jsonContext, String currentFieldNam
434438
mapper.toJson(builder, params);
435439
}
436440
}
441+
if (additionalMappers != null) {
442+
for (JsonMapper mapper : additionalMappers) {
443+
mapper.toJson(builder, params);
444+
}
445+
}
437446

438447
if (!mappers.isEmpty()) {
439448
builder.startObject("properties");
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.util.io;
21+
22+
import java.io.Reader;
23+
24+
/**
25+
* @author kimchy (shay.banon)
26+
*/
27+
public abstract class CharSequenceReader extends Reader implements CharSequence {
28+
}

modules/elasticsearch/src/main/java/org/elasticsearch/util/io/FastCharArrayWriter.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,32 @@
2828
/**
2929
* A similar class to {@link java.io.CharArrayWriter} allowing to get the underlying <tt>char[]</tt> buffer.
3030
*
31-
* @author kimchy (Shay Banon)
31+
* @author kimchy (shay.banon)
3232
*/
3333
@NotThreadSafe
3434
public class FastCharArrayWriter extends Writer {
3535

36+
/**
37+
* A thread local based cache of {@link FastByteArrayOutputStream}.
38+
*/
39+
public static class Cached {
40+
41+
private static final ThreadLocal<FastCharArrayWriter> cache = new ThreadLocal<FastCharArrayWriter>() {
42+
@Override protected FastCharArrayWriter initialValue() {
43+
return new FastCharArrayWriter();
44+
}
45+
};
46+
47+
/**
48+
* Returns the cached thread local byte stream, with its internal stream cleared.
49+
*/
50+
public static FastCharArrayWriter cached() {
51+
FastCharArrayWriter os = cache.get();
52+
os.reset();
53+
return os;
54+
}
55+
}
56+
3657
/**
3758
* The buffer where data is stored.
3859
*/

modules/elasticsearch/src/main/java/org/elasticsearch/util/io/FastStringReader.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
/**
2828
* A character stream whose source is a string that is <b>not thread safe</b>
2929
*
30-
* @author kimchy (Shay Banon)
30+
* @author kimchy (shay.banon
31+
* )
3132
*/
3233
@NotThreadSafe
33-
public class FastStringReader extends Reader {
34+
public class FastStringReader extends CharSequenceReader {
3435

3536
private String str;
3637
private int length;
@@ -55,6 +56,18 @@ private void ensureOpen() throws IOException {
5556
throw new IOException("Stream closed");
5657
}
5758

59+
@Override public int length() {
60+
return length;
61+
}
62+
63+
@Override public char charAt(int index) {
64+
return str.charAt(index);
65+
}
66+
67+
@Override public CharSequence subSequence(int start, int end) {
68+
return str.subSequence(start, end);
69+
}
70+
5871
/**
5972
* Reads a single character.
6073
*
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.util.lucene.all;
21+
22+
import org.apache.lucene.analysis.Analyzer;
23+
import org.apache.lucene.analysis.TokenStream;
24+
import org.apache.lucene.document.Fieldable;
25+
26+
import java.io.IOException;
27+
import java.io.Reader;
28+
29+
/**
30+
* An all analyzer.
31+
*
32+
* @author kimchy (shay.banon)
33+
*/
34+
public class AllAnalyzer extends Analyzer {
35+
36+
private final Analyzer analyzer;
37+
38+
public AllAnalyzer(Analyzer analyzer) {
39+
this.analyzer = analyzer;
40+
}
41+
42+
@Override public TokenStream tokenStream(String fieldName, Reader reader) {
43+
AllEntries allEntries = (AllEntries) reader;
44+
return new AllTokenFilter(analyzer.tokenStream(fieldName, reader), allEntries);
45+
}
46+
47+
@Override public TokenStream reusableTokenStream(String fieldName, Reader reader) throws IOException {
48+
AllEntries allEntries = (AllEntries) reader;
49+
return new AllTokenFilter(analyzer.reusableTokenStream(fieldName, reader), allEntries);
50+
}
51+
52+
@Override public int getPositionIncrementGap(String fieldName) {
53+
return analyzer.getPositionIncrementGap(fieldName);
54+
}
55+
56+
@Override public int getOffsetGap(Fieldable field) {
57+
return analyzer.getOffsetGap(field);
58+
}
59+
}

0 commit comments

Comments
 (0)