This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Merge pull request #1237 from zanata/ZNTA-1253-lucene-parse

ZNTA-1253 - escape lucene key words
  • Loading branch information...
huangp committed Jul 18, 2016
2 parents df22ee6 + faf8e59 commit 621af3118c542ece9817edd5522c53a0169d567d
@@ -45,6 +45,17 @@
LocaleId targetLocaleId, LocaleId sourceLocaleId,
boolean checkContext, boolean checkDocument, boolean checkProject);
/**
* This is used by TM merge
* @param textFlow
* @param targetLocaleId
* @param sourceLocaleId
* @param checkContext
* @param checkDocument
* @param checkProject
* @param thresholdPercent
* @return
*/
Optional<TransMemoryResultItem> searchBestMatchTransMemory(
HTextFlow textFlow, LocaleId targetLocaleId,
LocaleId sourceLocaleId, boolean checkContext,
@@ -60,7 +60,6 @@
import org.zanata.model.HTextFlow;
import org.zanata.model.HTextFlowTarget;
import org.zanata.model.tm.TransMemoryUnit;
import org.zanata.model.tm.TransMemoryUnitVariant;
import org.zanata.rest.editor.dto.suggestion.Suggestion;
import org.zanata.rest.editor.dto.suggestion.SuggestionDetail;
import org.zanata.rest.editor.dto.suggestion.TextFlowSuggestionDetail;
@@ -74,6 +73,7 @@
import org.zanata.webtrans.shared.model.TransMemoryResultItem;
import org.zanata.webtrans.shared.rpc.HasSearchType;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
@@ -125,6 +125,8 @@
private static final double MINIMUM_SIMILARITY = 1.0;
private static final String LUCENE_KEY_WORDS = "(\\s*)(AND|OR|NOT)(\\s+)";
@Inject @FullText
private FullTextEntityManager entityManager;
@@ -192,10 +194,10 @@
Collection<Object[]> matches =
findMatchingTranslation(targetLocaleId, sourceLocaleId, query,
0, Optional.<Long>absent(), HTextFlowTarget.class);
0, Optional.absent(), HTextFlowTarget.class);
if (matches.isEmpty()) {
return Optional.<HTextFlowTarget> absent();
return Optional.absent();
}
return Optional.of((HTextFlowTarget) matches.iterator().next()[1]);
@@ -232,9 +234,9 @@
thresholdPercent));
if (aboveThreshold.isEmpty()) {
return Optional.<TransMemoryResultItem> absent();
return Optional.absent();
}
return Optional.of((TransMemoryResultItem) aboveThreshold.iterator()
return Optional.of(aboveThreshold.iterator()
.next());
}
@@ -572,7 +574,7 @@ public int hashCode() {
// 'Fuzzy' in the editor
case FUZZY:
queryText = QueryParser.escape(query.getQueries().get(0));
queryText = escape(query.getQueries().get(0));
if (StringUtils.isBlank(queryText)) {
return Lists.newArrayList();
}
@@ -581,7 +583,7 @@ public int hashCode() {
// 'Phrase' in the editor
case EXACT:
queryText =
"\"" + QueryParser.escape(query.getQueries().get(0)) + "\"";
"\"" + escape(query.getQueries().get(0)) + "\"";
if (StringUtils.isBlank(queryText)) {
return Lists.newArrayList();
}
@@ -592,7 +594,7 @@ public int hashCode() {
multiQueryText = new String[query.getQueries().size()];
for (int i = 0; i < query.getQueries().size(); i++) {
multiQueryText[i] =
QueryParser.escape(query.getQueries().get(i));
escape(query.getQueries().get(i));
if (StringUtils.isBlank(multiQueryText[i])) {
return Lists.newArrayList();
}
@@ -640,6 +642,11 @@ public int hashCode() {
return resultList;
}
@VisibleForTesting
protected static String escape(String string) {
return QueryParser.escape(string).replaceAll(LUCENE_KEY_WORDS, "$1\"$2\"$3");
}
private void logQueryResults(List<Object[]> resultList) {
if (log.isTraceEnabled()) {
// resultList.get() could be a little slow if resultList is a
@@ -59,6 +59,7 @@ public GetTranslationMemoryResult execute(GetTranslationMemory action,
TransMemoryQuery transMemoryQuery = action.getQuery();
log.debug("Fetching matches for {}", transMemoryQuery);
// TODO if the client is requesting a TM for a textflow, it should just pass the text flow id not the text. And also some other context info like docId, version. project etc
List<TransMemoryResultItem> results =
translationMemoryServiceImpl.searchTransMemory(
action.getLocaleId(), action.getSourceLocaleId(),
@@ -0,0 +1,38 @@
/*
* Copyright 2016, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.service.impl;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class TranslationMemoryServiceImplUnitTest {
@Test
public void escapeText() throws Exception {
String escapedText = TranslationMemoryServiceImpl.escape(
"AND one to two or three OR - (must) + be - true");
assertThat(escapedText).isEqualTo(
"\"AND\" one to two or three \"OR\" \\- \\(must\\) \\+ be \\- true");
}
}

0 comments on commit 621af31

Please sign in to comment.