fix #2404 copy lang fiels

This commit is contained in:
Shinsuke Sugaya 2020-02-15 07:16:24 +09:00
parent 24e1a0af77
commit 300d096002
2 changed files with 64 additions and 6 deletions

View file

@ -15,20 +15,21 @@
*/
package org.codelibs.fess.job;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.fess.es.client.FessEsClient;
import org.codelibs.fess.helper.LabelTypeHelper;
import org.codelibs.fess.helper.LanguageHelper;
import org.codelibs.fess.mylasta.direction.FessConfig;
import org.codelibs.fess.util.ComponentUtil;
import org.codelibs.fess.util.DocumentUtil;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.script.Script;
public class UpdateLabelJob {
@ -40,6 +41,7 @@ public class UpdateLabelJob {
final FessEsClient fessEsClient = ComponentUtil.getFessEsClient();
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final LabelTypeHelper labelTypeHelper = ComponentUtil.getLabelTypeHelper();
final LanguageHelper languageHelper = ComponentUtil.getLanguageHelper();
final StringBuilder resultBuf = new StringBuilder();
@ -51,7 +53,8 @@ public class UpdateLabelJob {
if (queryBuilder != null) {
option.setQuery(queryBuilder);
}
return option.setFetchSource(new String[] { fessConfig.getIndexFieldUrl() }, null);
return option.setFetchSource(
new String[] { fessConfig.getIndexFieldUrl(), fessConfig.getIndexFieldLang() }, null);
},
(builder, hit) -> {
try {
@ -59,10 +62,17 @@ public class UpdateLabelJob {
final String url = DocumentUtil.getValue(doc, fessConfig.getIndexFieldUrl(), String.class);
if (StringUtil.isNotBlank(url)) {
final Set<String> labelSet = labelTypeHelper.getMatchedLabelValueSet(url);
return builder.setDoc(XContentFactory.jsonBuilder().startObject()
.field(fessConfig.getIndexFieldLabel(), labelSet.toArray(n -> new String[n])).endObject());
final Script script =
languageHelper.createScript(
doc,
"ctx._source."
+ fessConfig.getIndexFieldLabel()
+ "=new String[]{"
+ labelSet.stream().map(s -> "\"" + s + "\"")
.collect(Collectors.joining(",")) + "}");
return builder.setScript(script);
}
} catch (final IOException e) {
} catch (final Exception e) {
logger.warn("Failed to process " + hit, e);
}
return null;

View file

@ -0,0 +1,48 @@
/*
* Copyright 2012-2020 CodeLibs Project and the Others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.codelibs.fess.helper;
import java.util.HashMap;
import java.util.Map;
import org.codelibs.fess.unit.UnitFessTestCase;
public class LanguageHelperTest extends UnitFessTestCase {
private LanguageHelper languageHelper;
@Override
public void setUp() throws Exception {
super.setUp();
languageHelper = new LanguageHelper();
languageHelper.langFields = new String[] { "title", "content" };
}
public void test_createScript() {
Map<String, Object> doc = new HashMap<>();
assertEquals("aaa", languageHelper.createScript(doc, "aaa").getIdOrCode());
doc.put("lang", "ja");
assertEquals("aaa;ctx._source.title_ja=ctx._source.title;ctx._source.content_ja=ctx._source.content",
languageHelper.createScript(doc, "aaa").getIdOrCode());
}
public void test_getReindexScriptSource() {
assertEquals(
"if(ctx._source.lang!=null){ctx._source['title_'+ctx._source.lang]=ctx._source.title;ctx._source['content_'+ctx._source.lang]=ctx._source.content}",
languageHelper.getReindexScriptSource());
}
}