@@ -49,14 +49,20 @@ public function document($indexName, NodeInterface $node, ElasticSearchDocument
4949 ]
5050 ],
5151 // http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.7/docs-update.html
52- 'script ' => [
53- 'script_id ' => 'updateFulltextParts ' ,
54- 'lang ' => 'groovy ' ,
52+ [
53+ 'script ' => '
54+ fulltext = (ctx._source.containsKey("__fulltext") ? ctx._source.__fulltext : new LinkedHashMap());
55+ fulltextParts = (ctx._source.containsKey("__fulltextParts") ? ctx._source.__fulltextParts : new LinkedHashMap());
56+ ctx._source = newData;
57+ ctx._source.__fulltext = fulltext;
58+ ctx._source.__fulltextParts = fulltextParts
59+ ' ,
5560 'params ' => [
5661 'newData ' => $ documentData
57- ]
58- ],
59- 'upsert ' => $ documentData
62+ ],
63+ 'upsert ' => $ documentData ,
64+ 'lang ' => 'groovy '
65+ ]
6066 ];
6167 } else {
6268 // non-fulltext-root documents can be indexed as-they-are
@@ -112,22 +118,52 @@ public function fulltext(NodeInterface $node, array $fulltextIndexOfNode, $targe
112118 ]
113119 ],
114120 // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-update.html
115- // first, update the __fulltextParts, then re-generate the __fulltext from all __fulltextParts
116- 'script ' => [
117- 'script_id ' => 'regenerateFulltext ' ,
118- 'lang ' => 'groovy ' ,
121+ [
122+ // first, update the __fulltextParts, then re-generate the __fulltext from all __fulltextParts
123+ 'script ' => '
124+ if (!(ctx._source.containsKey("__fulltextParts") && ctx._source.__fulltextParts instanceof Map)) {
125+ ctx._source.__fulltextParts = new LinkedHashMap();
126+ }
127+
128+ if (nodeIsRemoved || nodeIsHidden || fulltext.size() == 0) {
129+ if (ctx._source.__fulltextParts.containsKey(identifier)) {
130+ ctx._source.__fulltextParts.remove(identifier);
131+ }
132+ } else {
133+ ctx._source.__fulltextParts[identifier] = fulltext;
134+ }
135+ ctx._source.__fulltext = new LinkedHashMap();
136+
137+ Iterator<LinkedHashMap.Entry<String, LinkedHashMap>> fulltextByNode = ctx._source.__fulltextParts.entrySet().iterator();
138+ for (fulltextByNode; fulltextByNode.hasNext();) {
139+ Iterator<LinkedHashMap.Entry<String, String>> elementIterator = fulltextByNode.next().getValue().entrySet().iterator();
140+ for (elementIterator; elementIterator.hasNext();) {
141+ Map.Entry<String, String> element = elementIterator.next();
142+ String value;
143+
144+ if (ctx._source.__fulltext.containsKey(element.key)) {
145+ value = ctx._source.__fulltext[element.key] + " " + element.value.trim();
146+ } else {
147+ value = element.value.trim();
148+ }
149+
150+ ctx._source.__fulltext[element.key] = value;
151+ }
152+ }
153+ ' ,
119154 'params ' => [
120155 'identifier ' => $ node ->getIdentifier (),
121156 'nodeIsRemoved ' => $ node ->isRemoved (),
122157 'nodeIsHidden ' => $ node ->isHidden (),
123158 'fulltext ' => $ fulltextIndexOfNode
124159 ],
125- ],
126- 'upsert ' => [
127- '__fulltext ' => $ fulltextIndexOfNode ,
128- '__fulltextParts ' => [
129- $ node ->getIdentifier () => $ fulltextIndexOfNode
130- ]
160+ 'upsert ' => [
161+ '__fulltext ' => $ fulltextIndexOfNode ,
162+ '__fulltextParts ' => [
163+ $ node ->getIdentifier () => $ fulltextIndexOfNode
164+ ]
165+ ],
166+ 'lang ' => 'groovy '
131167 ]
132168 ];
133169 }
0 commit comments