2014-01-08 4 views
0

여러 단어가 포함 된 문구로 자동 완성 기능을 구현하려고합니다.탄성 검색이 여러 단어로 시작됩니다.

단어의 시작 부분 (edgeNGram?)과 일치하지만 모든 단어를 검색 할 수 있기를 원합니다.

예를 들어 "monitor"를 검색하는 경우 monitor라는 단어가 포함 된 모든 문구를 받아야하지만 "onitor"를 검색하면 일치하는 문구가 표시되지 않습니다 (아래 데이터 집합에서). 또한 "mon ap"를 검색하면 "APNEA MONITOR- SCHULTE Vital Signs Monitor"가 표시되고 "mon rrr"은 결과가 나타나지 않습니다.

제 질문은 어떻게 구현해야합니까?

간단히 말해 일치하는 구문에는 검색된 용어로 시작하는 단어가 포함되어야합니다.

query: { 
    match: { 
     phrase: { 
      query: term, 
      operator: 'and' 
     } 
} 

일부 샘플 데이터 :

{ 
    "took" : 133, 
    "timed_out" : false, 
    "_shards" : { 
     "total" : 1, 
     "successful" : 1, 
     "failed" : 0 
    }, 
    "hits" : { 
     "total" : 6197, 
     "max_score" : 1.491863, 
     "hits" : [ { 
      "_index" : "quicksearch2", 
      "_type" : "results", 
      "_id" : "emCydgTfQwuKkl4sSZoosQ", 
      "_score" : 1.491863, 
      "fields" : { 
       "phrase" : "APNEA MONITOR- SCHULTE Apnea Monitor" 
      } 
     }, { 
      "_index" : "quicksearch2", 
      "_type" : "results", 
      "_id" : "AXCO5rUxRwC9SebXcQxXeQ", 
      "_score" : 1.491863, 
      "fields" : { 
       "phrase" : "APNEA MONITOR- SCHULTE Apnea Monitor, Neonatal" 
      } 
     }, { 
      "_index" : "quicksearch2", 
      "_type" : "results", 
      "_id" : "tjJq3klPTsmP8akOc18Htw", 
      "_score" : 1.491863, 
      "fields" : { 
       "phrase" : "APNEA MONITOR- SCHULTE Apnea Monitor, Recording" 
      } 
     }, { 
      "_index" : "quicksearch2", 
      "_type" : "results", 
      "_id" : "-FjKWxl9Rm6-byn-wlpoIw", 
      "_score" : 1.491863, 
      "fields" : { 
       "phrase" : "APNEA MONITOR- SCHULTE Cardiorespiratory Monitor" 
      } 
     }, { 
      "_index" : "quicksearch2", 
      "_type" : "results", 
      "_id" : "Q19k6V6VQ6ulZOLCfESQ6w", 
      "_score" : 1.491863, 
      "fields" : { 
       "phrase" : "APNEA MONITOR- SCHULTE Impedance Pneumograph Bedside Monitor" 
      } 
     }, { 
      "_index" : "quicksearch2", 
      "_type" : "results", 
      "_id" : "YLI1er3cRjSyGumWNVi0pg", 
      "_score" : 1.491863, 
      "fields" : { 
       "phrase" : "APNEA MONITOR- SCHULTE Impedance Pneumograph Monitor" 
      } 
     }, { 
      "_index" : "quicksearch2", 
      "_type" : "results", 
      "_id" : "n5j1SaXeS2W6NymaYAYD6A", 
      "_score" : 1.491863, 
      "fields" : { 
       "phrase" : "APNEA MONITOR- SCHULTE Neonatal Monitor" 
      } 
     }, { 
      "_index" : "quicksearch2", 
      "_type" : "results", 
      "_id" : "U7Q5XrrHRbKOIwfRWO6RTQ", 
      "_score" : 1.491863, 
      "fields" : { 
       "phrase" : "APNEA MONITOR- SCHULTE Pulmonary Function Monitor" 
      } 
     }, { 
      "_index" : "quicksearch2", 
      "_type" : "results", 
      "_id" : "aF_THiCKRIyzunCbBxJTEg", 
      "_score" : 1.491863, 
      "fields" : { 
       "phrase" : "APNEA MONITOR- SCHULTE Vital Signs Monitor" 
      } 
     }, { 
      "_index" : "quicksearch2", 
      "_type" : "results", 
      "_id" : "8BAjZfwMQjWmrkqCO7o6gg", 
      "_score" : 1.491863, 
      "fields" : { 
       "phrase" : "P.P.M. - PORTABLE PRECISION MONITOR Gas Monitor, Atmospheric" 
      } 
     } ] 
    } 
} 
다음
{ 
    "quicksearch2" : { 
     "settings" : { 
      "index.analysis.analyzer.quicksearch_index_analyzer.filter.4" : "left_ngram", 
      "index.analysis.analyzer.quicksearch_search_analyzer.filter.3" : "unique", 
      "index.analysis.analyzer.quicksearch_index_analyzer.filter.3" : "unique", 
      "index.analysis.filter.left_ngram.max_gram" : "20", 
      "index.analysis.analyzer.quicksearch_search_analyzer.filter.2" : "asciifolding", 
      "index.analysis.analyzer.quicksearch_search_analyzer.tokenizer" : "keyword", 
      "index.analysis.analyzer.quicksearch_search_analyzer.filter.1" : "lowercase", 
      "index.number_of_replicas" : "0", 
      "index.analysis.analyzer.quicksearch_search_analyzer.filter.0" : "trim", 
      "index.analysis.filter.left_ngram.type" : "edgeNGram", 
      "index.analysis.analyzer.quicksearch_search_analyzer.type" : "custom", 
      "index.analysis.analyzer.quicksearch_index_analyzer.filter.0" : "trim", 
      "index.analysis.analyzer.quicksearch_index_analyzer.filter.2" : "asciifolding", 
      "index.analysis.analyzer.quicksearch_index_analyzer.filter.1" : "lowercase", 
      "index.analysis.analyzer.quicksearch_index_analyzer.type" : "custom", 
      "index.analysis.filter.left_ngram.side" : "front", 
      "index.analysis.analyzer.quicksearch_index_analyzer.tokenizer" : "keyword", 
      "index.number_of_shards" : "1", 
      "index.version.created" : "900899", 
      "index.uuid" : "Lb7vC-eHQB-u_Okm3ERLow" 
     } 
    } 
} 

내 쿼리입니다 :

{ 
    "quicksearch2" : { 
     "results" : { 
      "properties" : {  
       "phrase" : { 
        "type" : "string", 
        "index_analyzer" : "quicksearch_index_analyzer", 
        "search_analyzer" : "quicksearch_search_analyzer" 
       }   
      } 
     } 
    } 
} 

그리고 여기 내 설정입니다 : 여기

내 매핑
+0

읽었습니까? http://jontai.me/blog/2013/02/adding-autocomplete-to-an-elasticsearch-search-application/ – phoet

답변

0

표준 키워드에서 tokenizers (색인 및 검색 둘 다) 변경을 트릭을 한 것으로 보인다.

1

당신이하고있는 일이 왜 효과가 없는지 잘 모르겠습니다 만, 여기 당신이 원하는 것을하는 방법이 있습니다.

curl -XPOST "http://localhost:9200/test_index/_bulk " -d' 
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : "1" } } 
{ "phrase" : "APNEA MONITOR- SCHULTE Apnea Monitor" } 
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : "2" } } 
{ "phrase" : "APNEA MONITOR- SCHULTE Apnea Monitor, Neonatal" } 
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : "3" } } 
{ "phrase" : "APNEA MONITOR- SCHULTE Apnea Monitor, Recording" } 
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : "4" } } 
{ "phrase" : "APNEA MONITOR- SCHULTE Cardiorespiratory Monitor" } 
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : "5" } } 
{ "phrase" : "APNEA MONITOR- SCHULTE Impedance Pneumograph Bedside Monitor" } 
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : "6" } } 
{ "phrase" : "APNEA MONITOR- SCHULTE Impedance Pneumograph Monitor" } 
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : "7" } } 
{ "phrase" : "APNEA MONITOR- SCHULTE Neonatal Monitor" } 
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : "8" } } 
{ "phrase" : "APNEA MONITOR- SCHULTE Pulmonary Function Monitor" } 
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : "9" } } 
{ "phrase" : "APNEA MONITOR- SCHULTE Vital Signs Monitor" } 
{ "index" : { "_index" : "test_index", "_type" : "docs", "_id" : "10" } } 
{ "phrase" : "P.P.M. - PORTABLE PRECISION MONITOR Gas Monitor, Atmospheric" } 
' 

그리고 다음 검색 당신이 기대하는 결과를 반환하는 것 :

curl -XPOST "http://localhost:9200/test_index/_search" -d' 
{ 
    "query": { 
     "match": { 
      "phrase" : { 
       "query": "monitor", 
       "operator": "and" 
      } 
     } 
    } 
}' 
curl -XPUT "http://localhost:9200/test_index " -d' 
{ 
    "settings": { 
     "analysis": { 
     "filter": { 
      "my_edge_ngram_filter": { 
       "type": "edgeNGram", 
       "min_gram": 2, 
       "max_gram": 20, 
       "token_chars": [ 
        "letter", 
        "digit" 
       ] 
      } 
     }, 
     "analyzer": { 
      "my_ngram_analyzer": { 
       "type": "custom", 
       "tokenizer": "whitespace", 
       "filter": [ 
        "lowercase", 
        "asciifolding", 
        "my_edge_ngram_filter" 
       ] 
      }, 
      "my_whitespace_analyzer": { 
       "type": "custom", 
       "tokenizer": "whitespace", 
       "filter": [ 
        "lowercase", 
        "asciifolding" 
       ] 
      } 
     } 
     } 
    }, 
    "mappings": { 
     "docs": { 
     "properties": { 
      "phrase": { 
       "type": "string", 
       "index_analyzer": "my_ngram_analyzer", 
       "search_analyzer": "my_whitespace_analyzer" 
      } 
     } 
     } 
    } 
}' 

는 다음 나열된 문서를 추가 :

나는 이러한 설정으로 인덱스를 생성

은 모든 문서를 반환합니다.

curl -XPOST "http://localhost:9200/test_index/_search" -d' 
{ 
    "query": { 
     "match": { 
      "phrase" : { 
       "query": "onitor", 
       "operator": "and" 
      } 
     } 
    } 
}' 
하나를 반환하지 않습니다

curl -XPOST "http://localhost:9200/test_index/_search" -d' 
{ 
    "query": { 
     "match": { 
      "phrase" : { 
       "query": "mon ap", 
       "operator": "and" 
      } 
     } 
    } 
}' 

반환 문서 "10"하지만 모든. 여기

은 당신이 재생할 수있는 실행 가능한 예이다 (당신은 설치하고 로컬 호스트에서 실행 ES가 필요합니다 : 다른 엔드 포인트 9200을, 또는 공급) : http://sense.qbox.io/gist/19fdcdb20c24436c64b7656c3b8002fe78667b12

관련 문제