2013-09-02 3 views
1

Solr과 일치하는 일부 동의어를 수행해야합니다.복잡한 동의어 일치

예를 들어 스웨덴의 길 이름은 일반적으로 Foogatan의 형식입니다. 여기서 gatan은 영어로 된 이름입니다. 이 거리의 이름은 내가 어떻게 synonyms.txt 작품에 대해 잘 알고 있어요 (당신이 영어로 street에 대한 st. 쓰기 좀처럼)

Foog.처럼 축약 기입 할 수 있지만 나는 그것이 포함되어 있는지 확인합니다 동의어를 생성하는 방법을 모른다 gatan 전 또는 g. 전의 일부 문자.

*g.*gatan과 일치하는 동의어가 필요합니다.

내가이 일을 결국은

public boolean incrementToken() throws IOException { 

    // See http://solr.pl/en/2012/05/14/developing-your-own-solr-filter/ 

    if (!input.incrementToken()) return false; 

    String string = charTermAttr.toString(); 

    boolean containsGatan = string.contains("gatan"); 
    boolean containsG = string.contains("g."); 

    if (containsGatan) { 

     string = string.replace("gatan", "g."); 

     char[] newBuffer = string.toCharArray(); 

     charTermAttr.setEmpty(); 
     charTermAttr.copyBuffer(newBuffer, 0, newBuffer.length); 

     return true; 
    } 

    if (containsG) { 

     string = string.replace("g.", "gatan"); 

     char[] newBuffer = string.toCharArray(); 

     charTermAttr.setEmpty(); 
     charTermAttr.copyBuffer(newBuffer, 0, newBuffer.length); 

     return true; 
    } 

    return false; 
} 

이 또한 내가 가진 유사한 문제가 당신이 031-123456의 형태로 전화 번호를 쓸 수 있다는 것입니다 (난 후 무엇을 거친 초안으로 작동하는 것 같다) 및 031123456. 031123456과 같은 전화 번호를 검색 할 때 찾기 031-123456

Solr에서 어떻게 구현할 수 있습니까?

답변

0

처음에는 사용자 정의 TokenFilter을 작성하고 분석기에 연결할 수 있습니다 (간단한 예는 org.apache.lucene.analysis.ASCIIFoldingFilter을 참조하십시오). 숫자와 숫자 만에 대한 색인/검색 문자 '-' http://docs.lucidworks.com/display/solr/CharFilterFactories

제거해야 할 것입니다 :

두 번째는 아마도 PatternReplaceCharFilterFactory를 사용하여 해결할 수 있습니다. 비슷한 질문 : 각 토큰의 끝에서 Solr PatternReplaceCharFilterFactory not replacing with specified pattern

간단한 예를 제거 gatan :

public class Gatanizer extends TokenFilter { 

    private final CharTermAttribute termAttribute = addAttribute(CharTermAttribute.class); 

    /** 
    * Construct a token stream filtering the given input. 
    */ 
    protected Gatanizer(TokenStream input) { 
     super(input); 
    } 

    @Override 
    public boolean incrementToken() throws IOException { 
     if (input.incrementToken()) { 

      final char[] buffer = termAttribute.buffer(); 
      final int length = termAttribute.length(); 

      String tokenString = new String(buffer, 0, length); 
      tokenString = StringUtils.removeEnd(tokenString, "gatan"); 

      termAttribute.setEmpty(); 
      termAttribute.append(tokenString); 

      return true; 
     } 

     return false; 
    } 

} 

그리고 내 TokenFilter 일부 SOLR 필드에 등록했습니다

<fieldtype name="gatan" stored="false" indexed="false" multiValued="true" class="solr.TextField"> 
     <analyzer> 
      <tokenizer class="solr.StandardTokenizerFactory"/> 
      <filter class="solr.LowerCaseFilterFactory"/> 
      <filter class="solr.ASCIIFoldingFilterFactory"/> 
      <filter class="gatanizer.GatanizerFactory"/> 
     </analyzer> 
    </fieldtype> 

또한거야 몇 가지 간단한 GatanizerFactory을 반환해야합니다. Gatanizer

+0

감사합니다. 대답! 두 번째 경우 전화 번호는 "그의 전화 번호가 031-123456입니다."와 같이 문자열에 포함되므로이 전체 문자열은 색인이 생성됩니다 (구조화되지 않은 정보를 작성할 수있는 설명 필드와 같습니다). 'PatternReplaceCharFilterFactory'를 사용할 수 있습니까? 아니면 필드가 "phonenumber"유형이라는 것을 알고있는 경우에만 사용할 수 있습니까? –

+0

모든 필드에서 사용할 수 있어야합니다. 텍스트에서 다른 토큰을 손상시키지 않도록 정규 표현식을 준비해야합니다. 필드에 많은 텍스트가 포함되어 있다면'MaxBlockChars' 버퍼에 문제가있을 수 있습니다. 그런 다음 맞춤형 토크 나이저를 사용해야 할 수도 있습니다. –

+0

처음 발행하는 방법에 대해 좀 더 자세히 설명 할 수 있습니까? 내가 시도한 것에 대한 몇 가지 코드로 업데이트되었지만 ... 불행히도 작동하지 않습니다 ... –