Skip to content
This repository was archived by the owner on Oct 29, 2023. It is now read-only.

Commit de9665a

Browse files
committed
Ignore empty string on language detection
Relates #6
1 parent 091b801 commit de9665a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/main/java/org/elasticsearch/plugin/ingest/langdetect/LangDetectProcessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.cybozu.labs.langdetect.Detector;
2121
import com.cybozu.labs.langdetect.DetectorFactory;
22+
import org.elasticsearch.common.Strings;
2223
import org.elasticsearch.common.unit.ByteSizeUnit;
2324
import org.elasticsearch.common.unit.ByteSizeValue;
2425
import org.elasticsearch.ingest.AbstractProcessor;
@@ -64,6 +65,10 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
6465
}
6566
throw e;
6667
}
68+
if (Strings.isEmpty(content)) {
69+
return ingestDocument;
70+
}
71+
6772
detector.append(content);
6873
String language = detector.detect();
6974

src/test/java/org/elasticsearch/plugin/ingest/langdetect/LangDetectProcessorTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ public void testIgnoreMissingConfiguration() throws Exception {
8686
assertThat(data, not(hasEntry("language", "en")));
8787
}
8888

89+
public void testEmptyString() throws Exception {
90+
Map<String, Object> config = new HashMap<>();
91+
config.put("field", "source_field");
92+
config.put("target_field", "language");
93+
config.put("ignore_missing", randomBoolean());
94+
95+
Map<String, Object> data = ingestDocument(config,"source_field", "");
96+
97+
assertThat(data, not(hasEntry("language", "en")));
98+
99+
}
100+
89101
private Map<String, Object> ingestDocument(Map<String, Object> config, String field, String value) throws Exception {
90102
Map<String, Object> document = new HashMap<>();
91103
document.put(field, value);

0 commit comments

Comments
 (0)