use of org.janusgraph.diskstorage.indexing.KeyInformation in project janusgraph by JanusGraph.
the class LuceneCustomAnalyzer method getWrappedAnalyzer.
@Override
protected final Analyzer getWrappedAnalyzer(String fieldName) {
final KeyInformation keyInformation = informations.get(store, fieldName);
if (keyInformation == null || !String.class.isAssignableFrom(keyInformation.getDataType())) {
return analyzerFor(STANDARD_ANALYZER);
}
final Parameter[] parameters = keyInformation.getParameters();
// if mapping isn't present in parameters, we use Mapping.DEFAULT
final Mapping mapping = ParameterType.MAPPING.findParameter(parameters, Mapping.DEFAULT);
// everything else falls through a StandardAnalyzer as was the case before
return analyzerFor(analyzerNameFor(parameters, mapping, KEYWORD_ANALYZER, STANDARD_ANALYZER));
}
use of org.janusgraph.diskstorage.indexing.KeyInformation in project janusgraph by JanusGraph.
the class ElasticSearchIndex method query.
@Override
public Stream<String> query(IndexQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
final ElasticSearchRequest sr = new ElasticSearchRequest();
final Map<String, Object> esQuery = getFilter(query.getCondition(), informations.get(query.getStore()));
sr.setQuery(compat.prepareQuery(esQuery));
if (!query.getOrder().isEmpty()) {
final List<IndexQuery.OrderEntry> orders = query.getOrder();
for (final IndexQuery.OrderEntry orderEntry : orders) {
final String order = orderEntry.getOrder().name();
final KeyInformation information = informations.get(query.getStore()).get(orderEntry.getKey());
final Mapping mapping = Mapping.getMapping(information);
final Class<?> datatype = orderEntry.getDatatype();
sr.addSort(orderEntry.getKey(), order.toLowerCase(), convertToEsDataType(datatype, mapping));
}
}
sr.setFrom(0);
if (query.hasLimit()) {
sr.setSize(Math.min(query.getLimit(), batchSize));
} else {
sr.setSize(batchSize);
}
ElasticSearchResponse response;
try {
final String indexStoreName = getIndexStoreName(query.getStore());
final String indexType = useMultitypeIndex ? query.getStore() : null;
response = client.search(indexStoreName, indexType, compat.createRequestBody(sr, NULL_PARAMETERS), sr.getSize() >= batchSize);
log.debug("First Executed query [{}] in {} ms", query.getCondition(), response.getTook());
final ElasticSearchScroll resultIterator = new ElasticSearchScroll(client, response, sr.getSize());
final Stream<RawQuery.Result<String>> toReturn = StreamSupport.stream(Spliterators.spliteratorUnknownSize(resultIterator, Spliterator.ORDERED), false);
return (query.hasLimit() ? toReturn.limit(query.getLimit()) : toReturn).map(RawQuery.Result::getResult);
} catch (final IOException | UncheckedIOException e) {
throw new PermanentBackendException(e);
}
}
use of org.janusgraph.diskstorage.indexing.KeyInformation in project janusgraph by JanusGraph.
the class ElasticSearchIndex method getDeletionScript.
private String getDeletionScript(KeyInformation.IndexRetriever information, String storeName, IndexMutation mutation) throws PermanentBackendException {
final StringBuilder script = new StringBuilder();
final String INDEX_NAME = "index";
int i = 0;
for (final IndexEntry deletion : mutation.getDeletions()) {
final KeyInformation keyInformation = information.get(storeName).get(deletion.field);
switch(keyInformation.getCardinality()) {
case SINGLE:
script.append("ctx._source.remove(\"").append(deletion.field).append("\");");
if (hasDualStringMapping(information.get(storeName, deletion.field))) {
script.append("ctx._source.remove(\"").append(getDualMappingName(deletion.field)).append("\");");
}
break;
case SET:
case LIST:
final String jsValue = convertToJsType(deletion.value, compat.scriptLang(), Mapping.getMapping(keyInformation));
String index = INDEX_NAME + i++;
script.append("def ").append(index).append(" = ctx._source[\"").append(deletion.field).append("\"].indexOf(").append(jsValue).append("); ctx._source[\"").append(deletion.field).append("\"].remove(").append(index).append(");");
if (hasDualStringMapping(information.get(storeName, deletion.field))) {
index = INDEX_NAME + i++;
script.append("def ").append(index).append(" = ctx._source[\"").append(getDualMappingName(deletion.field)).append("\"].indexOf(").append(jsValue).append("); ctx._source[\"").append(getDualMappingName(deletion.field)).append("\"].remove(").append(index).append(");");
}
break;
}
}
return script.toString();
}
use of org.janusgraph.diskstorage.indexing.KeyInformation in project janusgraph by JanusGraph.
the class ElasticSearchIndex method getAdditionScript.
private String getAdditionScript(KeyInformation.IndexRetriever information, String storeName, IndexMutation mutation) throws PermanentBackendException {
final StringBuilder script = new StringBuilder();
for (final IndexEntry e : mutation.getAdditions()) {
final KeyInformation keyInformation = information.get(storeName).get(e.field);
switch(keyInformation.getCardinality()) {
case SET:
case LIST:
script.append("if(ctx._source[\"").append(e.field).append("\"] == null) ctx._source[\"").append(e.field).append("\"] = [];");
script.append("ctx._source[\"").append(e.field).append("\"].add(").append(convertToJsType(e.value, compat.scriptLang(), Mapping.getMapping(keyInformation))).append(");");
if (hasDualStringMapping(keyInformation)) {
script.append("if(ctx._source[\"").append(getDualMappingName(e.field)).append("\"] == null) ctx._source[\"").append(getDualMappingName(e.field)).append("\"] = [];");
script.append("ctx._source[\"").append(getDualMappingName(e.field)).append("\"].add(").append(convertToJsType(e.value, compat.scriptLang(), Mapping.getMapping(keyInformation))).append(");");
}
break;
default:
break;
}
}
return script.toString();
}
use of org.janusgraph.diskstorage.indexing.KeyInformation in project janusgraph by JanusGraph.
the class ElasticSearchIndex method getNewDocument.
public Map<String, Object> getNewDocument(final List<IndexEntry> additions, KeyInformation.StoreRetriever information) throws BackendException {
// JSON writes duplicate fields one after another, which forces us
// at this stage to make de-duplication on the IndexEntry list. We don't want to pay the
// price map storage on the Mutation level because none of other backends need that.
final Multimap<String, IndexEntry> unique = LinkedListMultimap.create();
for (final IndexEntry e : additions) {
unique.put(e.field, e);
}
final Map<String, Object> doc = new HashMap<>();
for (final Map.Entry<String, Collection<IndexEntry>> add : unique.asMap().entrySet()) {
final KeyInformation keyInformation = information.get(add.getKey());
final Object value;
switch(keyInformation.getCardinality()) {
case SINGLE:
value = convertToEsType(Iterators.getLast(add.getValue().iterator()).value, Mapping.getMapping(keyInformation));
break;
case SET:
case LIST:
value = add.getValue().stream().map(v -> convertToEsType(v.value, Mapping.getMapping(keyInformation))).filter(v -> {
Preconditions.checkArgument(!(v instanceof byte[]), "Collections not supported for " + add.getKey());
return true;
}).toArray();
break;
default:
value = null;
break;
}
doc.put(add.getKey(), value);
if (hasDualStringMapping(information.get(add.getKey())) && keyInformation.getDataType() == String.class) {
doc.put(getDualMappingName(add.getKey()), value);
}
}
return doc;
}
Aggregations