@Override protected Field parseCreateField(ParseContext context) throws IOException { if (context.parser().currentName() != null && context.parser().currentName().equals(Defaults.NAME)) { // we are in the parsing of _parent phase String parentId = context.parser().text(); context.sourceToParse().parent(parentId); return new Field( names.indexName(), Uid.createUid(context.stringBuilder(), type, parentId), fieldType); } // otherwise, we are running it post processing of the xcontent String parsedParentId = context.doc().get(Defaults.NAME); if (context.sourceToParse().parent() != null) { String parentId = context.sourceToParse().parent(); if (parsedParentId == null) { if (parentId == null) { throw new MapperParsingException( "No parent id provided, not within the document, and not externally"); } // we did not add it in the parsing phase, add it now return new Field( names.indexName(), Uid.createUid(context.stringBuilder(), type, parentId), fieldType); } else if (parentId != null && !parsedParentId.equals(Uid.createUid(context.stringBuilder(), type, parentId))) { throw new MapperParsingException( "Parent id mismatch, document value is [" + Uid.createUid(parsedParentId).id() + "], while external value is [" + parentId + "]"); } } // we have parent mapping, yet no value was set, ignore it... return null; }
private Uid doUpdate() throws IOException, JSchException { if (uid == null || StringUtil.isBlank(uid.getUidValue())) { throw new IllegalArgumentException("No Uid attribute provided in the attributes"); } LOG.info("Update user: "******"Wrong object class"); } if (objectClass.equals(ObjectClass.ACCOUNT)) { if (!EvaluateCommandsResultOutput.evaluateUserOrGroupExists( unixConnection.execute( UnixConnector.getCommandGenerator().userExists(uid.getUidValue())))) { throw new ConnectorException("User " + uid + " do not exists"); } for (Attribute attr : attrs) { if (attr.is(Name.NAME) || attr.is(Uid.NAME)) { newUserName = (String) attr.getValue().get(0); } else if (attr.is(OperationalAttributes.PASSWORD_NAME)) { password = Utilities.getPlainPassword((GuardedString) attr.getValue().get(0)); } else if (attr.is(OperationalAttributes.ENABLE_NAME)) { status = Boolean.parseBoolean(attr.getValue().get(0).toString()); } else if (attr.is(configuration.getCommentAttribute())) { comment = attr.getValue().get(0).toString(); } else if (attr.is(configuration.getShellAttribute())) { shell = (String) attr.getValue().get(0).toString(); } else if (attr.is(configuration.getHomeDirectoryAttribute())) { homeDirectory = (String) attr.getValue().get(0).toString(); } } unixConnection.execute( UnixConnector.getCommandGenerator() .updateUser( uid.getUidValue(), newUserName, password, status, comment, shell, homeDirectory)); // unixConnection.execute("mv /home/" + uid.getUidValue() + " /home/" + // newUserName); unixConnection.execute( UnixConnector.getCommandGenerator().moveHomeDirectory(uid.getUidValue(), newUserName)); if (!status) { unixConnection.execute(UnixConnector.getCommandGenerator().lockUser(uid.getUidValue())); } else { unixConnection.execute(UnixConnector.getCommandGenerator().unlockUser(uid.getUidValue())); } if (StringUtil.isNotBlank(newUserName) && StringUtil.isNotEmpty(newUserName)) { unixConnection.execute( UnixConnector.getCommandGenerator().updateGroup(uid.getUidValue(), newUserName)); } } else if (objectClass.equals(ObjectClass.GROUP)) { if (!EvaluateCommandsResultOutput.evaluateUserOrGroupExists( unixConnection.execute(UnixConnector.getCommandGenerator().groupExists(newUserName)))) { throw new ConnectorException("Group do not exists"); } unixConnection.execute( UnixConnector.getCommandGenerator().updateGroup(uid.getUidValue(), newUserName)); } return uid; }
@Test public void testCreateAndSplitId() { BytesRef createUid = Uid.createUidAsBytes("foo", "bar"); BytesRef[] splitUidIntoTypeAndId = Uid.splitUidIntoTypeAndId(createUid); assertThat("foo", equalTo(splitUidIntoTypeAndId[0].utf8ToString())); assertThat("bar", equalTo(splitUidIntoTypeAndId[1].utf8ToString())); // split also with an offset BytesRef ref = new BytesRef(createUid.length + 10); ref.offset = 9; ref.length = createUid.length; System.arraycopy(createUid.bytes, createUid.offset, ref.bytes, ref.offset, ref.length); splitUidIntoTypeAndId = Uid.splitUidIntoTypeAndId(ref); assertThat("foo", equalTo(splitUidIntoTypeAndId[0].utf8ToString())); assertThat("bar", equalTo(splitUidIntoTypeAndId[1].utf8ToString())); }
@Override public BytesRef indexedValueForSearch(Object value) { if (value instanceof BytesRef) { BytesRef bytesRef = (BytesRef) value; if (Uid.hasDelimiter(bytesRef)) { return bytesRef; } return Uid.createUidAsBytes(typeAsBytes, bytesRef); } String sValue = value.toString(); if (sValue.indexOf(Uid.DELIMITER) == -1) { return Uid.createUidAsBytes(type, sValue); } return super.indexedValueForSearch(value); }
@Override public Uid value(Object value) { if (value == null) { return null; } return Uid.createUid(value.toString()); }
@Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { if (!fieldType.indexed()) { return new PrefixFilter( new Term(UidFieldMapper.NAME, Uid.typePrefixAsBytes(BytesRefs.toBytesRef(value)))); } return new TermFilter(names().createIndexNameTerm(BytesRefs.toBytesRef(value))); }
@Override public void stringField(FieldInfo fieldInfo, String value) throws IOException { if (UidFieldMapper.NAME.equals(fieldInfo.name)) { uid = Uid.createUid(value); } else { addValue(fieldInfo.name, value); } }
@Override protected Field parseCreateField(ParseContext context) throws IOException { // so, caching uid stream and field is fine // since we don't do any mapping parsing without immediate indexing // and, when percolating, we don't index the uid UidField field = fieldCache.get(); field.setUid(Uid.createUid(context.stringBuilder(), context.type(), context.id())); context.uid(field); return field; // version get updated by the engine }
private Uid doUpdate() throws IOException { if (!objectClass.equals(ObjectClass.ACCOUNT) && (!objectClass.equals(ObjectClass.GROUP))) { throw new IllegalStateException("Wrong object class"); } if (!userExists( uid.getUidValue(), configuration.getOpenamRealm(), adminToken.getToken(), connection)) { LOG.error("User " + uid.getUidValue() + " do not exists"); throw new ConnectorException("User " + uid.getUidValue() + " do not exists"); } try { connection.update(createUpdateQueryString(uid, attrs, adminToken).toString()); LOG.ok("User " + uid.getUidValue() + " updated"); } catch (HttpClientErrorException hcee) { throw hcee; } return uid; }
@Override public void collect(int doc) throws IOException { // the _source is the query Document document = reader.document(doc, new UidAndSourceFieldSelector()); String id = Uid.createUid(document.get(UidFieldMapper.NAME)).id(); byte[] source = document.getBinaryValue(SourceFieldMapper.NAME); try { queries.put(id, percolator.parseQuery(id, source, 0, source.length)); } catch (Exception e) { logger.warn("failed to add query [{}]", e, id); } }
@Override public Filter termFilter(Object value, @Nullable QueryParseContext context) { if (context == null) { return super.termFilter(value, context); } BytesRef bValue = BytesRefs.toBytesRef(value); // we use all types, cause we don't know if its exact or not... BytesRef[] typesValues = new BytesRef[context.mapperService().types().size()]; int i = 0; for (String type : context.mapperService().types()) { typesValues[i++] = Uid.createUidAsBytes(type, bValue); } return new TermsFilter(names.indexName(), typesValues); }
@Override public Filter termsFilter(List values, @Nullable QueryParseContext context) { if (context == null) { return super.termsFilter(values, context); } List<BytesRef> bValues = new ArrayList<BytesRef>(values.size()); for (Object value : values) { BytesRef bValue = BytesRefs.toBytesRef(value); // we use all types, cause we don't know if its exact or not... for (String type : context.mapperService().types()) { bValues.add(Uid.createUidAsBytes(type, bValue)); } } return new TermsFilter(names.indexName(), bValues); }
public void postProcess(MapperService mapperService) { if (uid != null) { DocumentMapper documentMapper = mapperService.documentMapper(uid.type()); if (documentMapper != null) { // we can derive the exact type for the mapping postProcess(documentMapper); return; } } // can't derive exact mapping type for (Map.Entry<String, List<Object>> entry : fields().entrySet()) { FieldMappers fieldMappers = mapperService.indexName(entry.getKey()); if (fieldMappers == null) { continue; } List<Object> fieldValues = entry.getValue(); for (int i = 0; i < fieldValues.size(); i++) { fieldValues.set(i, fieldMappers.mapper().valueForSearch(fieldValues.get(i))); } } }
@Override public Uid valueFromString(String value) { return Uid.createUid(value); }
public TermVectorResponse getTermVector(TermVectorRequest request, String concreteIndex) { final Engine.Searcher searcher = indexShard.acquireSearcher("term_vector"); IndexReader topLevelReader = searcher.reader(); final TermVectorResponse termVectorResponse = new TermVectorResponse(concreteIndex, request.type(), request.id()); final Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id())); Engine.GetResult get = indexShard.get(new Engine.Get(request.realtime(), uidTerm)); boolean docFromTranslog = get.source() != null; AggregatedDfs dfs = null; /* fetched from translog is treated as an artificial document */ if (docFromTranslog) { request.doc(get.source().source, false); termVectorResponse.setDocVersion(get.version()); } /* handle potential wildcards in fields */ if (request.selectedFields() != null) { handleFieldWildcards(request); } try { Fields topLevelFields = MultiFields.getFields(topLevelReader); Versions.DocIdAndVersion docIdAndVersion = get.docIdAndVersion(); /* from an artificial document */ if (request.doc() != null) { Fields termVectorsByField = generateTermVectorsFromDoc(request, !docFromTranslog); // if no document indexed in shard, take the queried document itself for stats if (topLevelFields == null) { topLevelFields = termVectorsByField; } if (termVectorsByField != null && useDfs(request)) { dfs = getAggregatedDfs(termVectorsByField, request); } termVectorResponse.setFields( termVectorsByField, request.selectedFields(), request.getFlags(), topLevelFields, dfs); termVectorResponse.setExists(true); termVectorResponse.setArtificial(!docFromTranslog); } /* or from an existing document */ else if (docIdAndVersion != null) { // fields with stored term vectors Fields termVectorsByField = docIdAndVersion.context.reader().getTermVectors(docIdAndVersion.docId); Set<String> selectedFields = request.selectedFields(); // generate tvs for fields where analyzer is overridden if (selectedFields == null && request.perFieldAnalyzer() != null) { selectedFields = getFieldsToGenerate(request.perFieldAnalyzer(), termVectorsByField); } // fields without term vectors if (selectedFields != null) { termVectorsByField = addGeneratedTermVectors(get, termVectorsByField, request, selectedFields); } if (termVectorsByField != null && useDfs(request)) { dfs = getAggregatedDfs(termVectorsByField, request); } termVectorResponse.setFields( termVectorsByField, request.selectedFields(), request.getFlags(), topLevelFields, dfs); termVectorResponse.setDocVersion(docIdAndVersion.version); termVectorResponse.setExists(true); } else { termVectorResponse.setExists(false); } } catch (Throwable ex) { throw new ElasticsearchException("failed to execute term vector request", ex); } finally { searcher.close(); get.release(); } return termVectorResponse; }
public Term term(String type, String id) { return term(Uid.createUid(type, id)); }
@Override public Uid value(Object value) { return Uid.createUid(String.valueOf(value)); }