|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.countedkeyword; |
9 | 9 |
|
10 | | -import org.apache.lucene.document.BinaryDocValuesField; |
11 | 10 | import org.apache.lucene.document.FieldType; |
12 | 11 | import org.apache.lucene.index.BinaryDocValues; |
13 | 12 | import org.apache.lucene.index.DocValues; |
|
19 | 18 | import org.apache.lucene.index.TermsEnum; |
20 | 19 | import org.apache.lucene.search.SortField; |
21 | 20 | import org.apache.lucene.util.BytesRef; |
| 21 | +import org.elasticsearch.ElasticsearchException; |
22 | 22 | import org.elasticsearch.common.bytes.BytesArray; |
23 | 23 | import org.elasticsearch.common.io.stream.ByteArrayStreamInput; |
24 | 24 | import org.elasticsearch.common.io.stream.BytesStreamOutput; |
|
31 | 31 | import org.elasticsearch.index.fielddata.plain.AbstractIndexOrdinalsFieldData; |
32 | 32 | import org.elasticsearch.index.fielddata.plain.AbstractLeafOrdinalsFieldData; |
33 | 33 | import org.elasticsearch.index.mapper.BinaryFieldMapper; |
| 34 | +import org.elasticsearch.index.mapper.CustomDocValuesField; |
34 | 35 | import org.elasticsearch.index.mapper.DocumentParserContext; |
35 | 36 | import org.elasticsearch.index.mapper.FieldMapper; |
36 | 37 | import org.elasticsearch.index.mapper.KeywordFieldMapper; |
@@ -434,15 +435,17 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio |
434 | 435 | return; |
435 | 436 | } |
436 | 437 |
|
437 | | - int i = 0; |
438 | | - int[] counts = new int[values.size()]; |
439 | | - for (Map.Entry<String, Integer> value : values.entrySet()) { |
440 | | - context.doc().add(new KeywordFieldMapper.KeywordField(fullPath(), new BytesRef(value.getKey()), fieldType)); |
441 | | - counts[i++] = value.getValue(); |
| 438 | + for (String value : values.keySet()) { |
| 439 | + context.doc().add(new KeywordFieldMapper.KeywordField(fullPath(), new BytesRef(value), fieldType)); |
| 440 | + } |
| 441 | + CountsBinaryDocValuesField field = (CountsBinaryDocValuesField) context.doc().getByKey(countFieldMapper.fieldType().name()); |
| 442 | + if (field == null) { |
| 443 | + field = new CountsBinaryDocValuesField(countFieldMapper.fieldType().name()); |
| 444 | + field.add(values); |
| 445 | + context.doc().addWithKey(countFieldMapper.fieldType().name(), field); |
| 446 | + } else { |
| 447 | + field.add(values); |
442 | 448 | } |
443 | | - BytesStreamOutput streamOutput = new BytesStreamOutput(); |
444 | | - streamOutput.writeVIntArray(counts); |
445 | | - context.doc().add(new BinaryDocValuesField(countFieldMapper.fullPath(), streamOutput.bytes().toBytesRef())); |
446 | 449 | } |
447 | 450 |
|
448 | 451 | private void parseArray(DocumentParserContext context, SortedMap<String, Integer> values) throws IOException { |
@@ -509,4 +512,37 @@ protected SyntheticSourceSupport syntheticSourceSupport() { |
509 | 512 | ); |
510 | 513 | } |
511 | 514 |
|
| 515 | + private class CountsBinaryDocValuesField extends CustomDocValuesField { |
| 516 | + private final SortedMap<String, Integer> counts; |
| 517 | + |
| 518 | + CountsBinaryDocValuesField(String name) { |
| 519 | + super(name); |
| 520 | + counts = new TreeMap<>(); |
| 521 | + } |
| 522 | + |
| 523 | + public void add(SortedMap<String, Integer> newCounts) { |
| 524 | + for (Map.Entry<String, Integer> currCount : newCounts.entrySet()) { |
| 525 | + this.counts.put(currCount.getKey(), this.counts.getOrDefault(currCount.getKey(), 0) + currCount.getValue()); |
| 526 | + } |
| 527 | + } |
| 528 | + |
| 529 | + @Override |
| 530 | + public BytesRef binaryValue() { |
| 531 | + try { |
| 532 | + int maxBytesPerVInt = 5; |
| 533 | + int bytesSize = (counts.size() + 1) * maxBytesPerVInt; |
| 534 | + BytesStreamOutput out = new BytesStreamOutput(bytesSize); |
| 535 | + int countsArr[] = new int[counts.size()]; |
| 536 | + int i = 0; |
| 537 | + for (Integer currCount : counts.values()) { |
| 538 | + countsArr[i++] = currCount; |
| 539 | + } |
| 540 | + out.writeVIntArray(countsArr); |
| 541 | + return out.bytes().toBytesRef(); |
| 542 | + } catch (IOException e) { |
| 543 | + throw new ElasticsearchException("Failed to get binary value", e); |
| 544 | + } |
| 545 | + } |
| 546 | + } |
| 547 | + |
512 | 548 | } |
0 commit comments