Search in sources :

Example 6 with ParseResult

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

the class QueryIndexServlet method createQueryResutStrategy.

public static // , boolean queryResultAware
QueryResutStrategy createQueryResutStrategy(// , boolean queryResultAware
AppDomainInfo domain, // , boolean queryResultAware
final HttpServletRequest request, final HttpServletResponse resp, RunContext runContext) {
    final SolrQueryModuleCreatorAdapter creatorAdapter = new SolrQueryModuleCreatorAdapter() {

        @Override
        public boolean schemaAware() {
            return false;
        }

        @Override
        public SolrParams build(IParamGetter params, final String querystr, final String sort, final String[] fqs, final Integer shownumf, final List<String> showFields) {
            SolrQuery query = new SolrQuery();
            // 增加排序字段
            if (StringUtils.isNotBlank(sort)) {
                query.add(CommonParams.SORT, sort);
            }
            // query.add(CommonParams.Q, querystr);
            query.setQuery(querystr);
            if (fqs != null) {
                for (String fq : fqs) {
                    query.add(CommonParams.FQ, fq);
                }
            }
            boolean facet = params.getBoolean(FacetParams.FACET);
            if (facet) {
                String facetField = params.getString(FacetParams.FACET_FIELD);
                if (StringUtils.isNotEmpty(facetField)) {
                    query.addFacetField(facetField);
                }
                String facetQuery = params.getString(FacetParams.FACET_QUERY);
                if (StringUtils.isNotEmpty(facetQuery)) {
                    query.addFacetQuery(facetQuery);
                }
                String facetPrefix = params.getString(FacetParams.FACET_PREFIX);
                if (StringUtils.isNotEmpty(facetPrefix)) {
                    query.setFacetPrefix(facetPrefix);
                }
            }
            query.add(CommonParams.START, params.getString(CommonParams.START, "0"));
            // 默认显示前三行
            query.setRows(shownumf);
            // query.add(CommonParams.ROWS, String.valueOf(shownumf));
            query.add(CommonParams.VERSION, "2.2");
            query.add(CommonParams.WT, "xml");
            boolean distrib = params.getBoolean(CommonParams.DISTRIB);
            query.add(CommonParams.DISTRIB, String.valueOf(distrib));
            if (showFields.isEmpty()) {
                if (distrib) {
                    query.setFields("*", FIELD_SHARD);
                }
            } else {
                for (String field : showFields) {
                    query.addField(field);
                }
                if (distrib) {
                    query.addField(FIELD_SHARD);
                }
            }
            query.setShowDebugInfo(params.getBoolean(CommonParams.DEBUG));
            return query;
        }

        @Override
        public ParseResult processSchema(InputStream schemaStream) {
            try {
                // SolrFieldsParser schemaParser = new SolrFieldsParser();
                ParseResult parseResult = SolrFieldsParser.parse(() -> IOUtils.toByteArray(schemaStream)).getSchemaParseResult();
                return parseResult;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public String[] getParameterValues(String keyname) {
            return request.getParameterValues(keyname);
        }

        @Override
        public void handleError(String url, ServerJoinGroup server, long allcount, Exception e) {
            synchronized (resp) {
                try {
                    String serverInfo = "[" + server.getGroupIndex() + "]" + server.getIp();
                    flushResult(resp, request, Arrays.asList(convert2Html("url:" + url + "<br/>" + getErrorContent(e), serverInfo)), allcount);
                } catch (IOException e1) {
                }
            }
        }

        @Override
        public void processResult(QueryRequestContext qrequest, QueryResponse result, ServerJoinGroup server) throws Exception {
            flushResult(resp, qrequest.request, convert2Html(qrequest, result, server, false), qrequest.resultCount.get());
        }

        @Override
        public void processResult(QueryRequestContext qrequest, List<Row> rows) throws Exception {
            flushResult(resp, qrequest.request, rows, qrequest.resultCount.get());
        }

        @Override
        public void setQuerySelectServerCandiate(Map<String, List<ServerJoinGroup>> servers) {
            request.setAttribute("querySelectServerCandiate", servers);
        }
    };
    final QueryResutStrategy queryResutStrategy = SolrCloudQueryResutStrategy.create(domain, creatorAdapter, runContext);
    return queryResutStrategy;
}
Also used : ParseResult(com.qlangtech.tis.solrdao.impl.ParseResult) InputStream(java.io.InputStream) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ServletException(javax.servlet.ServletException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) QueryRequestContext(com.qlangtech.tis.runtime.module.screen.IndexQuery.QueryRequestContext) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IParamGetter(com.qlangtech.tis.runtime.module.action.IParamGetter) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap)

Example 7 with ParseResult

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

the class SchemaAction method mergeWfColsWithTplCollection.

/**
 * 通过解析workflow的最终导出的字段,来生成Schema配置
 *
 * @param module
 * @param context
 * @param appSource
 * @return
 * @throws Exception
 */
public static SchemaResult mergeWfColsWithTplCollection(BasicModule module, Context context, ISolrAppSource appSource, final ISchemaPluginContext schemaPlugin, SolrFieldsParser.ParseResultCallback... parseResultCallback) throws Exception {
    // 通过version取默认模板
    Application tplApp = getTemplateApp(module);
    SchemaResult tplSchema = getTemplateSchema(module, context, tplApp);
    if (!tplSchema.isSuccess()) {
        return null;
    }
    ParseResult parseResult = (ParseResult) tplSchema.getParseResult();
    SolrType strType = parseResult.getTisType(ReflectSchemaFieldType.STRING.literia);
    List<ColumnMetaData> cols = appSource.reflectCols();
    for (ColumnMetaData colName : cols) {
        PSchemaField f = new PSchemaField();
        f.setName(colName.getKey());
        f.setType(strType);
        f.setStored(true);
        f.setIndexed(false);
        f.setMltiValued(false);
        f.setDocValue(false);
        parseResult.dFields.add(f);
    }
    parseResult.setUniqueKey(null);
    for (SolrFieldsParser.ParseResultCallback c : parseResultCallback) {
        c.process(cols, parseResult);
    }
    parseResult.addReservedFields();
    tplSchema.content = XModifier.modifySchemaContent(tplSchema.content, (document2, modifier) -> {
        modifier.addModify("/fields/field(:delete)");
        modifier.addModify("/sharedKey(:delete)");
        modifier.deleteUniqueKey();
        updateSchemaXML(parseResult.types, schemaPlugin, parseResult, document2, modifier);
    });
    parseResultCallback4test.process(cols, parseResult);
    return tplSchema;
}
Also used : WorkFlow(com.qlangtech.tis.workflow.pojo.WorkFlow) StringUtils(org.apache.commons.lang.StringUtils) PluginStore(com.qlangtech.tis.plugin.PluginStore) PSchemaField(com.qlangtech.tis.solrdao.pojo.PSchemaField) TIS(com.qlangtech.tis.TIS) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) WorkFlowCriteria(com.qlangtech.tis.workflow.pojo.WorkFlowCriteria) IServerGroupDAO(com.qlangtech.tis.manage.biz.dal.dao.IServerGroupDAO) SaveFileContentAction(com.qlangtech.tis.runtime.module.action.jarcontent.SaveFileContentAction) Document(org.jdom2.Document) com.qlangtech.tis.solrdao(com.qlangtech.tis.solrdao) ByteArrayInputStream(java.io.ByteArrayInputStream) PermissionConstant(com.qlangtech.tis.manage.PermissionConstant) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) IPluginStore(com.qlangtech.tis.plugin.IPluginStore) EntityResolver(org.xml.sax.EntityResolver) Savefilecontent(com.qlangtech.tis.manage.Savefilecontent) com.qlangtech.tis.manage.biz.dal.pojo(com.qlangtech.tis.manage.biz.dal.pojo) Validator(com.qlangtech.tis.plugin.annotation.Validator) ISolrAppSource(com.qlangtech.tis.manage.ISolrAppSource) JSONException(com.alibaba.fastjson.JSONException) com.qlangtech.tis.manage.common(com.qlangtech.tis.manage.common) Collectors(java.util.stream.Collectors) IOUtils(org.apache.commons.io.IOUtils) ISearchEngineTypeTransfer(com.qlangtech.tis.datax.ISearchEngineTypeTransfer) DataxReader(com.qlangtech.tis.datax.impl.DataxReader) SAXException(org.xml.sax.SAXException) RunEnvironment(com.qlangtech.tis.pubhook.common.RunEnvironment) JSONObject(com.alibaba.fastjson.JSONObject) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LuceneVersion(com.qlangtech.tis.fullbuild.indexbuild.LuceneVersion) ParseResult(com.qlangtech.tis.solrdao.impl.ParseResult) java.util(java.util) BeanUtils(org.apache.commons.beanutils.BeanUtils) DocType(org.jdom2.DocType) Func(com.qlangtech.tis.manage.spring.aop.Func) ColumnMetaData(com.qlangtech.tis.plugin.ds.ColumnMetaData) SolrType(com.qlangtech.tis.solrdao.SolrFieldsParser.SolrType) Context(com.alibaba.citrus.turbine.Context) JsonUtil(com.qlangtech.tis.trigger.util.JsonUtil) JSONArray(com.alibaba.fastjson.JSONArray) com.qlangtech.tis.runtime.module.misc(com.qlangtech.tis.runtime.module.misc) DataxProcessor(com.qlangtech.tis.datax.impl.DataxProcessor) Charset(java.nio.charset.Charset) XModifier(com.yushu.tis.xmodifier.XModifier) DataxUtils(com.qlangtech.tis.offline.DataxUtils) InputSource(org.xml.sax.InputSource) ReflectSchemaFieldType(com.qlangtech.tis.plugin.ds.ReflectSchemaFieldType) FieldTypeFactory(com.qlangtech.tis.plugin.solr.schema.FieldTypeFactory) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) JSON(com.alibaba.fastjson.JSON) StringReader(java.io.StringReader) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Assert(com.qlangtech.tis.common.utils.Assert) SolrType(com.qlangtech.tis.solrdao.SolrFieldsParser.SolrType) ParseResult(com.qlangtech.tis.solrdao.impl.ParseResult) PSchemaField(com.qlangtech.tis.solrdao.pojo.PSchemaField) ColumnMetaData(com.qlangtech.tis.plugin.ds.ColumnMetaData)

Example 8 with ParseResult

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

the class SchemaResult method parseSchemaResult.

/**
 * 解析提交的schemaxml 内容
 *
 * @param context
 * @return
 * @throws Exception
 */
public static SchemaResult parseSchemaResult(IMessageHandler module, Context context, byte[] schemaContent, boolean shallValidate, ISchemaFieldTypeContext schemaPlugin, SolrFieldsParser.ParseResultCallback... parseResultCallback) {
    if (schemaContent == null) {
        throw new IllegalStateException("schemaContent can not be null");
    }
    if (schemaPlugin == null) {
        throw new IllegalArgumentException("param schemaPlugin can not be null");
    }
    ParseResult parseResult;
    try {
        IIndexMetaData meta = SolrFieldsParser.parse(() -> schemaContent, schemaPlugin, shallValidate);
        parseResult = meta.getSchemaParseResult();
        for (SolrFieldsParser.ParseResultCallback process : parseResultCallback) {
            process.process(Collections.emptyList(), parseResult);
        }
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
        parseResult = new ParseResult(shallValidate);
        parseResult.errlist.add(e.getMessage());
    }
    if (!parseResult.isValid() || parseResult.errlist.size() > 0) {
        for (String err : parseResult.errlist) {
            module.addErrorMessage(context, err);
        }
        return create(null, schemaContent).faild();
    }
    return create(parseResult, schemaContent);
// // new String(, getEncode());
// result.content = schemaContent;
// result.success = true;
// result.parseResult = parseResult;
// return result;
}
Also used : ParseResult(com.qlangtech.tis.solrdao.impl.ParseResult) IIndexMetaData(com.qlangtech.tis.exec.IIndexMetaData)

Example 9 with ParseResult

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

the class SolrFieldsParser method parseSchema.

public ParseResult parseSchema(InputStream is, ISchemaFieldTypeContext schemaPlugin, boolean shallValidate) throws Exception {
    DocumentBuilderFactory schemaDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    // 只是读取schema不作校验
    schemaDocumentBuilderFactory.setValidating(shallValidate);
    final ParseResult result = new ParseResult(shallValidate);
    DocumentBuilder builder = schemaDocumentBuilderFactory.newDocumentBuilder();
    InputSource input = new InputSource(is);
    if (!shallValidate) {
        builder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                InputSource source = new InputSource();
                source.setCharacterStream(new StringReader(""));
                return source;
            }
        });
    } else {
        final DefaultHandler mh = new DefaultHandler() {

            public void error(SAXParseException e) throws SAXException {
                result.errlist.add("行号:" + e.getLineNumber() + " " + e.getMessage() + "<br/>");
            }

            public void fatalError(SAXParseException e) throws SAXException {
                this.error(e);
            }
        };
        builder.setErrorHandler(mh);
        builder.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                // final String tisrepository = TSearcherConfigFetcher.get().getTisConsoleHostAddress();
                final String tisrepository = Config.getConfigRepositoryHost();
                final URL url = new URL(tisrepository + "/dtd/solrschema.dtd");
                return new InputSource(new ByteArrayInputStream(ConfigFileContext.processContent(url, new StreamProcess<byte[]>() {

                    @Override
                    public byte[] p(int status, InputStream stream, Map<String, List<String>> headerFields) {
                        try {
                            return IOUtils.toByteArray(stream);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                })));
            }
        });
    }
    Document document = null;
    try {
        document = builder.parse(input);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
    if (!result.isValid()) {
        return result;
    }
    return parse(document, schemaPlugin, shallValidate);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ParseResult(com.qlangtech.tis.solrdao.impl.ParseResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EntityResolver(org.xml.sax.EntityResolver) IOException(java.io.IOException) Document(org.w3c.dom.Document) URL(java.net.URL) SAXException(org.xml.sax.SAXException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) StreamProcess(com.qlangtech.tis.manage.common.ConfigFileContext.StreamProcess) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 10 with ParseResult

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

the class SolrFieldsParser method parse.

public ParseResult parse(Document document, ISchemaFieldTypeContext schemaPlugin, boolean shallValidate) throws Exception {
    // .newXPath();
    final XPath xpath = createXPath();
    ParseResult parseResult = new ParseResult(shallValidate);
    parseResult.dFieldsNames = new HashSet<>();
    final ArrayList<PSchemaField> dFields = parseResult.dFields;
    // 取得fields type
    String expression = "/schema/types/fieldType|/schema/types/fieldtype";
    NodeList nodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
    // Map<String, SolrType> types = new HashMap<String, SolrType>();
    String typeName = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        NamedNodeMap attrs = node.getAttributes();
        typeName = DOMUtil.getAttr(attrs, "name");
        if (shallValidate && parseResult.types.containsKey(typeName)) {
            parseResult.errlist.add("重复定义了字段类型‘" + typeName + "’");
            return parseResult;
        }
        parseResult.types.put(typeName, parseFieldType(typeName, DOMUtil.getAttr(attrs, "class", "class definition"), schemaPlugin.isTokenizer(typeName)));
    }
    fieldTypeVisitor.visit(nodes);
    addExtenionProcessor(parseResult, xpath, document);
    // addColumnProcessor(parseResult, xpath, document);
    // 取得fields
    expression = "/schema/fields/field";
    ParseResult result = parseField(document, shallValidate, xpath, parseResult, dFields, expression, false);
    if (!result.isValid()) {
        return result;
    }
    expression = "/schema/fields/dynamicField";
    result = parseField(document, shallValidate, xpath, parseResult, dFields, expression, true);
    if (!result.isValid()) {
        return result;
    }
    if (parseResult.dFieldsNames == null) {
        throw new IllegalStateException("parseResult.dFieldsNames can not be null");
    }
    parseResult.dFieldsNames = Collections.unmodifiableSet(parseResult.dFieldsNames);
    final String uniqueKey = (String) xpath.evaluate("/schema/uniqueKey", document, XPathConstants.STRING);
    final String sharedKey = (String) xpath.evaluate("/schema/sharedKey", document, XPathConstants.STRING);
    boolean uniqueKeyDefined = false;
    boolean shareKeyDefined = false;
    // final String defaultSearchField = (String) xpath.evaluate("/schema/defaultSearchField", document, XPathConstants.STRING);
    if (shallValidate) {
        for (PSchemaField f : dFields) {
            if (f.getName().equals(uniqueKey)) {
                uniqueKeyDefined = true;
                if (!f.isIndexed()) {
                    // parseResult.errlist.add(
                    // "defined uniqueKey:" + defaultSearchField + " field
                    // property 'indexed' shall be true");
                    parseResult.errlist.add("主键:‘" + uniqueKey + "’属性'indexed'必须为true");
                    return result;
                }
                // 主键不能为多值
                if (f.isMultiValue()) {
                    parseResult.errlist.add("主键:‘" + uniqueKey + "’属性'multiValued'必须为false");
                }
            }
            if (f.getName().equals(sharedKey)) {
                shareKeyDefined = true;
            }
        // if (f.getName().equals(defaultSearchField)) {
        // defaultSearchFieldDefined = true;
        // if (!f.isIndexed()) {
        // parseResult.errlist.add("默认查询键:‘" + defaultSearchField + "’属性'indexed'必须为true");
        // return result;
        // }
        // }
        }
        if (!shareKeyDefined) {
            // parseResult.errlist.add("shareKey have not been define in sub
            // element of schema/fields");
            parseResult.errlist.add("请设置分区键");
        }
        if (!uniqueKeyDefined) {
            // parseResult.errlist.add("uniqueKey have not been define in
            // sub element of schema/fields");
            parseResult.errlist.add("请设置主键");
            return result;
        }
    // 判断定义了defaultSearchField 但是没有在schema中找到
    // if (StringUtils.isNotBlank(defaultSearchField) && !defaultSearchFieldDefined) {
    // // result.errlist.add(
    // // "defined defaultSearchField:" + defaultSearchField + " can
    // // not be found in the fields list");
    // result.errlist.add("已定义的默认查询字段:" + defaultSearchField + "在fields中不存在");
    // return result;
    // }
    }
    parseResult.setUniqueKey(uniqueKey);
    parseResult.setSharedKey(sharedKey);
    final String indexBuilder = getIndexBuilder((NodeList) xpath.evaluate("/schema", document, XPathConstants.NODESET));
    if (!StringUtils.isBlank(indexBuilder)) {
        parseResult.setIndexBuilder(indexBuilder);
    }
    final NodeList schemaNodes = (NodeList) xpath.evaluate("/schema", document, XPathConstants.NODESET);
    final String indexMakerClassName = getIndexMakerClassName(schemaNodes);
    if (!StringUtils.isBlank(indexMakerClassName)) {
        parseResult.setIndexMakerClassName(indexMakerClassName);
    }
    // 构建全量索引doc构建工厂
    parseResult.setDocumentCreatorType(getDocMaker(schemaNodes));
    return parseResult;
}
Also used : XPath(javax.xml.xpath.XPath) NamedNodeMap(org.w3c.dom.NamedNodeMap) ParseResult(com.qlangtech.tis.solrdao.impl.ParseResult) PSchemaField(com.qlangtech.tis.solrdao.pojo.PSchemaField) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

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