2014-10-24 2 views
0

나는 파이썬 requests 라이브러리를 통해 Elasticsearch를 사용하고 있습니다. 나는 그렇게처럼 내 분석기를 설정 한 :탄성 검색 전체 텍스트 자동 완성

"analysis" : { 
     "analyzer": { 
      "my_basic_search": { 
       "type": "standard", 
       "stopwords": [] 
      }, 
      "my_autocomplete": { 
       "type": "custom", 
       "tokenizer": "keyword", 
       "filter": ["lowercase", "autocomplete"] 
      } 
     }, 
     "filter": { 
      "autocomplete": { 
       "type": "edge_ngram", 
       "min_gram": 1, 
       "max_gram": 20, 
      } 
     } 
    } 

은 내가 자동 완성을 사용하여 검색하고 싶은 예술가의 목록을 가지고 : 나의 현재의 테스트 케이스가 일치해야합니다 'w 법안'이다 ' - 법안 감정 '등은 artist 매핑 (이 GET http://localhost:9200/my_index/artist/_mapping의 출력) 다음과 같습니다

{ 
    "my_index" : { 
    "mappings" : { 
     "artist" : { 
     "properties" : { 
      "clean_artist_name" : { 
      "type" : "string", 
      "analyzer" : "my_basic_search", 
      "fields" : { 
       "autocomplete" : { 
       "type" : "string", 
       "index_analyzer" : "my_autocomplete", 
       "search_analyzer" : "my_basic_search" 
       } 
      } 
      }, 
      "submitted_date" : { 
      "type" : "date", 
      "format" : "basic_date_time" 
      }, 
      "total_count" : { 
      "type" : "integer" 
      } 
     } 
     } 
    } 
    } 
} 

... 그리고 나는 자동 완성 할이 쿼리를 실행 :

"query": { 
     "function_score": { 
      "query": { 
       "bool": { 
        "must" : { "match": { "clean_artist_name.autocomplete": "bill w" } }, 
        "should" : { "match": { "clean_artist_name": "bill w" } }, 
       } 
      }, 
      "functions": [ 
      { 
       "script_score": { 
        "script": "artist-score" 
       } 
      } 
      ] 
     } 
    } 

이 보인다을 매트에 'bill withers'뿐만 아니라 'bill'이나 'w'가 포함 된 ch 아티스트 : 정확한 문자열이 포함 된 아티스트와 일치시키고 싶었습니다. 분석기는 여기에, 잘 동작하는 것 http://localhost:9200/my_index/_analyze?analyzer=my_autocomplete&text=bill%20w의 출력은 다음과 같습니다

{ 
    "tokens" : [ { 
    "token" : "b", 
    "start_offset" : 0, 
    "end_offset" : 6, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "bi", 
    "start_offset" : 0, 
    "end_offset" : 6, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "bil", 
    "start_offset" : 0, 
    "end_offset" : 6, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "bill", 
    "start_offset" : 0, 
    "end_offset" : 6, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "bill ", 
    "start_offset" : 0, 
    "end_offset" : 6, 
    "type" : "word", 
    "position" : 1 
    }, { 
    "token" : "bill w", 
    "start_offset" : 0, 
    "end_offset" : 6, 
    "type" : "word", 
    "position" : 1 
    } ] 
} 

왜 이것은 단지 '법안'또는 거기에 'w'와 일치하는 항목을 제외되지 않는 이유는 무엇입니까? 쿼리에 my_basic_search 분석기와 만 일치하는 결과를 허용하는 항목이 있습니까?

답변

1

"필수 사항"대신 "용어"필터가 필요하다고 생각합니다. 아티스트 이름을 ngram으로 분할 했으므로 검색 텍스트는 정확히 ngram 중 하나와 일치해야합니다. 이 문제가 발생하려면 정확히 ngrams와 일치하는 "용어"가 필요합니다.

"query": { 
    "function_score": { 
     "query": { 
      "bool": { 
       "must" : { "term": { "clean_artist_name.autocomplete": "bill w" } }, 
       "should" : { "match": { "clean_artist_name": "bill w" } }, 
      } 
     }, 
     "functions": [ 
     { 
      "script_score": { 
       "script": "artist-score" 
      } 
     } 
     ] 
    } 
} 
+0

고마워요! 그랬어. 또한 분석기를 변경하여 스톱 어는 제거 할 수 있지만 공백으로 구분하지는 못합니다. 예를 들어 '악명 높은 B'는 'The Notorious BIG'와 일치 할 수 있습니다. – benwad

+0

[this] (http://elasticsearch-users.115913.n3.nabble.com/Stop-words-and-Keyword-tokenizer-tp4062615p4062637 .html). 이렇게하는 것이 쉬운 방법이 아닌 것 같습니다. –