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;
}
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;
}
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;
}
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);
}
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;
}
Aggregations