Source:
<code>public void write(NutchDocument doc) throws IOException { String id = (String) doc.getFieldValue("id"); String type = doc.getDocumentMeta().get("type"); if (type == null) { type = defaultType; } Map<String, Object> source = new HashMap<String, Object>(); for (String fieldName : doc.getFieldNames()) { Set<String> allFieldValues = new HashSet<String>(); for (Object value : doc.getField(fieldName).getValues()) { allFieldValues.add(value.toString()); } String[] fieldValues = allFieldValues.toArray(new String[allFieldValues.size()]); if (fieldValues.length > 1) { for (String value : fieldValues) { bulkLength += value.length(); } source.put(fieldName, fieldValues); } else if(fieldValues.length == 1) { source.put(fieldName, fieldValues[0]); bulkLength += fieldValues[0].length(); } } String index; if (languages != null && languages.length > 0) { String language = (String) doc.getFieldValue("lang"); boolean exists = false; for (String lang : languages) { if (lang.equals(language)) { exists = true; break; } } if (exists) { <START> index = defaultIndex + "_" + language; <END> } else { index = defaultIndex + "_others"; } } else { index = defaultIndex; } Index indexRequest = new Index.Builder(source).index(index) .type(type).id(id).build(); bulkBuilder.addAction(indexRequest); indexedDocs++; bulkDocs++; if (bulkDocs >= maxBulkDocs || bulkLength >= maxBulkLength) { LOG.info( "Processing bulk request [docs = {}, length = {}, total docs = {}, last doc in bulk = '{}']", bulkDocs, bulkLength, indexedDocs, id); createNewBulk = true; commit(); } }</code><technical_language>Extract logic determine index name function reuse in delete action</technical_language>

Target:
public void write(NutchDocument doc) throws IOException { String id = (String) doc.getFieldValue("id"); String type = doc.getDocumentMeta().get("type"); if (type == null) { type = defaultType; } Map<String, Object> source = new HashMap<String, Object>(); for (String fieldName : doc.getFieldNames()) { Set<String> allFieldValues = new HashSet<String>(); for (Object value : doc.getField(fieldName).getValues()) { allFieldValues.add(value.toString()); } String[] fieldValues = allFieldValues.toArray(new String[allFieldValues.size()]); if (fieldValues.length > 1) { for (String value : fieldValues) { bulkLength += value.length(); } source.put(fieldName, fieldValues); } else if(fieldValues.length == 1) { source.put(fieldName, fieldValues[0]); bulkLength += fieldValues[0].length(); } } String index; if (languages != null && languages.length > 0) { String language = (String) doc.getFieldValue("lang"); boolean exists = false; for (String lang : languages) { if (lang.equals(language)) { exists = true; break; } } if (exists) { index = getLanguageIndexName(language); } else { index = getSinkIndexName(); } } else { index = defaultIndex; } Index indexRequest = new Index.Builder(source).index(index) .type(type).id(id).build(); bulkBuilder.addAction(indexRequest); indexedDocs++; bulkDocs++; if (bulkDocs >= maxBulkDocs || bulkLength >= maxBulkLength) { LOG.info( "Processing bulk request [docs = {}, length = {}, total docs = {}, last doc in bulk = '{}']", bulkDocs, bulkLength, indexedDocs, id); createNewBulk = true; commit(); } }