if (featureEnum == null) {
throw new QueryParsingException(parseContext.index(), "No feature specified for image query");
}
String luceneFieldName = fieldName + "." + featureEnum.name();
LireFeature feature = null;
if (image != null) {
try {
feature = featureEnum.getFeatureClass().newInstance();
BufferedImage img = ImageIO.read(new BytesStreamInput(image, false));
if (Math.max(img.getHeight(), img.getWidth()) > ImageMapper.MAX_IMAGE_DIMENSION) {
img = ImageUtils.scaleImage(img, ImageMapper.MAX_IMAGE_DIMENSION);
}
feature.extract(img);
} catch (Exception e) {
throw new ElasticsearchImageProcessException("Failed to parse image", e);
}
} else if (lookupIndex != null && lookupType != null && lookupId != null && lookupPath != null) {
String lookupFieldName = lookupPath + "." + featureEnum.name();
GetResponse getResponse = client.get(new GetRequest(lookupIndex, lookupType, lookupId).preference("_local").routing(lookupRouting).fields(lookupFieldName).realtime(false)).actionGet();
if (getResponse.isExists()) {
GetField getField = getResponse.getField(lookupFieldName);
if (getField != null) {
BytesReference bytesReference = (BytesReference) getField.getValue();
try {
feature = featureEnum.getFeatureClass().newInstance();
feature.setByteArrayRepresentation(bytesReference.array(), bytesReference.arrayOffset(), bytesReference.length());
} catch (Exception e) {
throw new ElasticsearchImageProcessException("Failed to parse image", e);
}
}
}
}
if (feature == null) {
throw new QueryParsingException(parseContext.index(), "No image specified for image query");
}
if (hashEnum == null) { // no hash, need to scan all documents
return new ImageQuery(luceneFieldName, feature, boost);
} else { // query by hash first
int[] hash = null;
if (hashEnum.equals(HashEnum.BIT_SAMPLING)) {
hash = BitSampling.generateHashes(feature.getDoubleHistogram());
} else if (hashEnum.equals(HashEnum.LSH)) {
hash = LocalitySensitiveHashing.generateHashes(feature.getDoubleHistogram());
}
String hashFieldName = luceneFieldName + "." + ImageMapper.HASH + "." + hashEnum.name();
if (limit > 0) { // has max result limit, use ImageHashLimitQuery
return new ImageHashLimitQuery(hashFieldName, hash, limit, luceneFieldName, feature, boost);