Search in sources :

Example 1 with ParseResult

use of com.qlangtech.tis.solrdao.impl.ParseResult in project tis by qlangtech.

the class TestSolrFieldParser method testSchemaFields.

public void testSchemaFields() throws Exception {
    // SolrFieldsParser solrFieldsParser = new SolrFieldsParser();
    try (InputStream reader = this.getClass().getResourceAsStream("schema-sample.txt")) {
        assertNotNull(reader);
        IIndexMetaData meta = SolrFieldsParser.parse(() -> IOUtils.toByteArray(reader), (typeName) -> {
            throw new UnsupportedOperationException();
        });
        ParseResult parseResult = meta.getSchemaParseResult();
        Assert.assertTrue("isValid shall be true", parseResult.isValid());
        Assert.assertEquals(20, parseResult.dFields.size());
        System.out.println("dFields.size:" + parseResult.dFields.size());
        Assert.assertEquals("parseResult.getFieldNameSet() can not be empty", 20, parseResult.getFieldNameSet().size());
        System.out.println(parseResult.getFieldNameSet().size());
    }
}
Also used : ParseResult(com.qlangtech.tis.solrdao.impl.ParseResult) IIndexMetaData(com.qlangtech.tis.exec.IIndexMetaData) InputStream(java.io.InputStream)

Example 2 with ParseResult

use of com.qlangtech.tis.solrdao.impl.ParseResult in project tis by qlangtech.

the class TestSchemaResult method testParseSchemaResultAlreadyContainFieldPlugin.

/**
 * 在schema配置文件中已经添加了 <br/>
 * <fieldType name="test" class="plugin:test" precisionStep="0" positionIncrementGap="0"/>
 * 该类型也要能自动加入到傻瓜模式下,string类型的联动下拉框 <br/>
 *
 * @throws Exception
 */
public void testParseSchemaResultAlreadyContainFieldPlugin() throws Exception {
    IMessageHandler msgHandler = EasyMock.createMock("msgHandler", IMessageHandler.class);
    Context context = EasyMock.createMock("context", Context.class);
    assertEquals(FieldTypeFactory.class, HeteroEnum.SOLR_FIELD_TYPE.extensionPoint);
    IPluginStore<FieldTypeFactory> fieldTypePluginStore = TIS.getPluginStore(collection, FieldTypeFactory.class);
    assertNotNull(fieldTypePluginStore);
    final String testFieldTypeName = "test";
    final List<FieldTypeFactory> plugins = fieldTypePluginStore.getPlugins();
    assertTrue(plugins.size() > 0);
    FieldTypeFactory fieldType = fieldTypePluginStore.find(testFieldTypeName);
    assertNotNull("fieldType can not be null", fieldType);
    try (InputStream schema = this.getClass().getResourceAsStream("s4totalpay-schema-already-contain-fieldtype-plugin.xml")) {
        EasyMock.replay(msgHandler, context);
        SchemaResult schemaResult = SchemaAction.parseSchemaResultWithPluginCfg(collection, msgHandler, context, IOUtils.toByteArray(schema));
        Collection<SolrFieldsParser.SolrType> fieldTypes = ((ParseResult) schemaResult.getParseResult()).getFieldTypes();
        assertEquals("fieldTypes size", 2, fieldTypes.size());
        // String content = (com.alibaba.fastjson.JSON.toJSONString(schemaResult.toJSON()
        // , SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.PrettyFormat));
        // try (InputStream assertSchemaResultInput = this.getClass().getResourceAsStream("s4totalpay-schema-already-contain-fieldtype-plugin-schema-result.json")) {
        // assertNotNull(assertSchemaResultInput);
        // FileUtils.write(new File("test.json"), content, TisUTF8.get());
        // assertEquals(StringUtils.trim(IOUtils.toString(assertSchemaResultInput, TisUTF8.get())), content);
        // }
        JsonUtil.assertJSONEqual(this.getClass(), "s4totalpay-schema-already-contain-fieldtype-plugin-schema-result.json", schemaResult.toJSON(), (m, e, a) -> {
            assertEquals(m, e, a);
        });
    }
    EasyMock.verify(msgHandler, context);
}
Also used : Context(com.alibaba.citrus.turbine.Context) ParseResult(com.qlangtech.tis.solrdao.impl.ParseResult) IMessageHandler(com.qlangtech.tis.runtime.module.misc.IMessageHandler) InputStream(java.io.InputStream) FieldTypeFactory(com.qlangtech.tis.plugin.solr.schema.FieldTypeFactory)

Example 3 with ParseResult

use of com.qlangtech.tis.solrdao.impl.ParseResult in project tis by qlangtech.

the class TestSchemaResult method testParseSchemaResult.

/**
 * 模拟用户刚添加fieldtype plugin,则用户第一次家在schema中要自动加入 fieldtype
 *
 * @throws Exception
 */
public void testParseSchemaResult() throws Exception {
    IMessageHandler msgHandler = EasyMock.createMock("msgHandler", IMessageHandler.class);
    Context context = EasyMock.createMock("context", Context.class);
    assertEquals(FieldTypeFactory.class, HeteroEnum.SOLR_FIELD_TYPE.extensionPoint);
    final String collection = "search4totalpay";
    IPluginStore<FieldTypeFactory> fieldTypePluginStore = TIS.getPluginStore(collection, FieldTypeFactory.class);
    assertNotNull(fieldTypePluginStore);
    final String testFieldTypeName = "test";
    final List<FieldTypeFactory> plugins = fieldTypePluginStore.getPlugins();
    assertTrue(plugins.size() > 0);
    FieldTypeFactory fieldType = fieldTypePluginStore.find(testFieldTypeName);
    assertNotNull("fieldType can not be null", fieldType);
    try (InputStream schema = this.getClass().getResourceAsStream("s4totalpay-schema.xml")) {
        EasyMock.replay(msgHandler, context);
        SchemaResult schemaResult = SchemaAction.parseSchemaResultWithPluginCfg(collection, msgHandler, context, IOUtils.toByteArray(schema));
        Collection<SolrFieldsParser.SolrType> fieldTypes = ((ParseResult) schemaResult.getParseResult()).getFieldTypes();
        assertEquals("fieldTypes size", 16, fieldTypes.size());
        // String content = (com.alibaba.fastjson.JSON.toJSONString(schemaResult.toJSON()
        // , SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.PrettyFormat));
        // 
        // try (InputStream assertSchemaResultInput = this.getClass().getResourceAsStream("assertSchemaResult.json")) {
        // assertNotNull(assertSchemaResultInput);
        // assertEquals(IOUtils.toString(assertSchemaResultInput, TisUTF8.get()), content);
        // }
        JsonUtil.assertJSONEqual(this.getClass(), "assertSchemaResult.json", schemaResult.toJSON(), (m, e, a) -> {
            assertEquals(m, e, a);
        });
    }
    EasyMock.verify(msgHandler, context);
}
Also used : Context(com.alibaba.citrus.turbine.Context) ParseResult(com.qlangtech.tis.solrdao.impl.ParseResult) IMessageHandler(com.qlangtech.tis.runtime.module.misc.IMessageHandler) InputStream(java.io.InputStream) FieldTypeFactory(com.qlangtech.tis.plugin.solr.schema.FieldTypeFactory)

Example 4 with ParseResult

use of com.qlangtech.tis.solrdao.impl.ParseResult in project tis by qlangtech.

the class CollectionAction method doGetIndexTopology.

/**
 * @param context
 * @throws Exception
 */
public void doGetIndexTopology(Context context) throws Exception {
    this.getIndexWithPost();
    JSONObject biz = new JSONObject();
    CollectionTopology topology = CoreAction.getCollectionTopology(this);
    JSONArray shards = new JSONArray();
    JSONObject shard = null;
    JSONArray replics = null;
    JSONObject replic = null;
    for (CollectionTopology.Shared s : topology.getShareds()) {
        shard = new JSONObject();
        replics = new JSONArray();
        shard.put(KEY_SHARD_NAME, s.getName());
        for (Replica r : s.getReplics()) {
            replic = new JSONObject();
            replic.put(KEY_CORE_URL, r.getCoreUrl());
            replic.put(KEY_IS_ACTIVE, r.getState() == Replica.State.ACTIVE);
            replics.add(replic);
        }
        shard.put(KEY_REPLICS, replics);
        shards.add(shard);
    }
    biz.put(SqlTaskNodeMeta.KEY_PROFILE_TOPOLOGY, shards);
    SnapshotDomain snapshot = ViewPojo.getSnapshotDoamin(this, this.getAppDomain());
    ParseResult parseResult = SolrFieldsParser.parse(() -> {
        return snapshot.getSolrSchema().getContent();
    }).getSchemaParseResult();
    biz.put(KEY_PK, parseResult.getUniqueKey());
    QueryResutStrategy queryStrategy = QueryIndexServlet.createQueryResutStrategy(this.getAppDomain(), new IndexQuery.QueryRequestWrapper(getRequest(), context), getResponse(), getDaoContext());
    List<ServerJoinGroup> nodes = queryStrategy.queryProcess();
    List<PSchemaField> sfields = IndexQuery.getSfields(this.getRequest(), queryStrategy, nodes);
    JSONArray colsMeta = new JSONArray();
    JSONObject colmeta = null;
    for (PSchemaField field : sfields) {
        colmeta = new JSONObject();
        colmeta.put("name", field.getName());
        colmeta.put("typeName", field.getType().getJavaType().getSimpleName());
        colmeta.put("typeCode", field.getType().getJavaType().getTypeCode());
        colmeta.put("docval", field.isDocValue());
        colmeta.put("indexd", field.isIndexed());
        colmeta.put("stored", field.isStored());
        colmeta.put("dynamic", field.isDynamic());
        colsMeta.add(colmeta);
    }
    biz.put(KEY_COLS_META, colsMeta);
    this.setBizResult(context, biz);
}
Also used : QueryResutStrategy(com.qlangtech.tis.manage.servlet.QueryResutStrategy) IndexQuery(com.qlangtech.tis.runtime.module.screen.IndexQuery) ParseResult(com.qlangtech.tis.solrdao.impl.ParseResult) PSchemaField(com.qlangtech.tis.solrdao.pojo.PSchemaField) JSONArray(com.alibaba.fastjson.JSONArray) ServerJoinGroup(com.qlangtech.tis.manage.servlet.ServerJoinGroup) Replica(org.apache.solr.common.cloud.Replica) JSONObject(com.alibaba.fastjson.JSONObject)

Example 5 with ParseResult

use of com.qlangtech.tis.solrdao.impl.ParseResult in project tis by qlangtech.

the class SchemaAction method createSchema.

/**
 * Schema XML模式 --> 专家模式 是一个数据结构投影,确保XML转专家模式,再专家模式转xml模式信息不会减少
 */
protected String createSchema(ISchemaPluginContext schemaPlugin, UploadSchemaWithRawContentForm schemaForm, boolean shallExecuteDelete) throws Exception {
    Assert.assertNotNull(schemaForm);
    // 先解析库中已经存在的模板
    if (StringUtils.isBlank(schemaForm.getSchemaXmlContent())) {
        throw new IllegalArgumentException("schemaXmlContent can not be null");
    }
    // 原始内容
    final byte[] originContent = schemaForm.getSchemaXmlContent().getBytes(getEncode());
    org.w3c.dom.Document document = createDocument(originContent);
    ParseResult parseResult = SolrFieldsParser.parseDocument(document, schemaPlugin, false);
    byte[] modifiedContent = XModifier.modifySchemaContent(originContent, (document2, modifier) -> {
        for (PSchemaField field : parseResult.dFields) {
            // 小白编辑模式下可能将字段删除,所以在高级模式下也要将字段删除
            if (schemaForm.containsField(field.getName())) {
                // intersectionKeys.add(field.getName());
                continue;
            }
            if (shallExecuteDelete) {
                if (field.isDynamic()) {
                    modifier.addModify("/fields/dynamicField[@name='" + field.getName() + "'](:delete)");
                } else {
                    modifier.addModify("/fields/field[@name='" + field.getName() + "'](:delete)");
                }
            }
        }
        updateSchemaXML(parseResult.types, schemaPlugin, schemaForm, document2, modifier);
        DocType docType = new DocType("schema", "solrres://tisrepository/dtd/solrschema.dtd");
        document2.setDocType(docType);
    });
    return new String(modifiedContent, TisUTF8.get());
}
Also used : ParseResult(com.qlangtech.tis.solrdao.impl.ParseResult) PSchemaField(com.qlangtech.tis.solrdao.pojo.PSchemaField) DocType(org.jdom2.DocType)

Aggregations

ParseResult (com.qlangtech.tis.solrdao.impl.ParseResult)11 PSchemaField (com.qlangtech.tis.solrdao.pojo.PSchemaField)5 InputStream (java.io.InputStream)5 Context (com.alibaba.citrus.turbine.Context)3 JSONArray (com.alibaba.fastjson.JSONArray)3 JSONObject (com.alibaba.fastjson.JSONObject)3 IOException (java.io.IOException)3 NamedNodeMap (org.w3c.dom.NamedNodeMap)3 Maps (com.google.common.collect.Maps)2 IIndexMetaData (com.qlangtech.tis.exec.IIndexMetaData)2 com.qlangtech.tis.manage.common (com.qlangtech.tis.manage.common)2 ReflectSchemaFieldType (com.qlangtech.tis.plugin.ds.ReflectSchemaFieldType)2 FieldTypeFactory (com.qlangtech.tis.plugin.solr.schema.FieldTypeFactory)2 IMessageHandler (com.qlangtech.tis.runtime.module.misc.IMessageHandler)2 WorkFlowCriteria (com.qlangtech.tis.workflow.pojo.WorkFlowCriteria)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StringReader (java.io.StringReader)2 Collectors (java.util.stream.Collectors)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2