2017-01-23 88 views
4

我有copy_to正確匹配工作,但我無法正確設置與部分匹配。以下是我的映射/設置和查詢與預期和實際結果。Elasticsearch | copy_to部分搜索

設置:

{ 
    "test": { 
     "settings": { 
     "index": { 
      "analysis": { 
       "filter": { 
        "ngram_filter": { 
        "type": "edge_ngram", 
        "min_gram": "1", 
        "max_gram": "15" 
        } 
       }, 
       "analyzer": { 
        "ngram_analyzer": { 
        "filter": [ 
         "lowercase", 
         "ngram_filter" 
        ], 
        "type": "custom", 
        "tokenizer": "standard" 
        } 
       } 
      }, 
      "number_of_shards": "1", 
      "number_of_replicas": "1", 
      } 
     } 
    } 
} 

映射:

POST /test/_mapping/name 
{ 
    "name": { 
     "properties": { 
     "vital": { 
      "properties": { 
       "first": { 
        "type": "string", 
        "copy_to": "full_name", 
        "term_vector": "yes", 
        "analyzer": "ngram_analyzer", 
        "search_analyzer": "standard" 
       }, 
       "last": { 
        "type": "string", 
        "copy_to": "full_name", 
        "term_vector": "yes", 
        "analyzer": "ngram_analyzer", 
        "search_analyzer": "standard" 
       }, 
       "full_name": { 
        "type": "string", 
        "term_vector": "yes", 
        "analyzer": "ngram_analyzer", 
        "search_analyzer": "standard" 
       } 
      } 
     } 
     } 
    } 
} 

POST:

POST /test/name 
{ 
    "vital": { 
     "first": "Tom", 
     "last": "Doe" 
    } 
} 

現在,當我做搜索...

GET /test/name/_search 
{ 
    "query": { 
    "match": { 
     "full_name": { 
     "query": "Tom Doe", 
     "operator": "and" 
     } 
    } 
    } 
} 

...我找回結果!! Hurrraaaay,但如果我做了搜索....

GET /test/name/_search 
{ 
    "query": { 
    "match": { 
     "full_name": { 
     "query": "Tom Do", 
     "operator": "and" 
     } 
    } 
    } 
} 

...我回來沒有結果:(我想部分匹配爲FULL_NAME工作,以及作爲另一個不是我能夠成功地因部分匹配的名字和姓氏,只是full_name不起作用,我會怎麼做呢?

回答

3

你在映射中有一個小錯誤,你需要複製fir st和姓氏放到vital.full_name字段中,而不是full_name,否則會在映射的頂層創建一個名爲full_name的新字符串字段,並在標準分析器的頂層顯示該字段(如果運行GET test,則會看到映射中的新字段):

POST /test/_mapping/name 
{ 
    "name": { 
     "properties": { 
     "vital": { 
      "properties": { 
       "first": { 
        "type": "string", 
        "copy_to": "vital.full_name",  <--- fix this 
        "term_vector": "yes", 
        "analyzer": "ngram_analyzer", 
        "search_analyzer": "standard" 
       }, 
       "last": { 
        "type": "string", 
        "copy_to": "vital.full_name",  <--- fix this 
        "term_vector": "yes", 
        "analyzer": "ngram_analyzer", 
        "search_analyzer": "standard" 
       }, 
       "full_name": { 
        "type": "string", 
        "term_vector": "yes", 
        "analyzer": "ngram_analyzer", 
        "search_analyzer": "standard" 
       } 
      } 
     } 
     } 
    } 
} 

,然後修復您的疑問是這樣的:

POST /test/name/_search 
{ 
    "query": { 
    "match": { 
     "vital.full_name": {   <-- fix this 
     "query": "Tom Doe", 
     "operator": "and" 
     } 
    } 
    } 
} 

POST /test/name/_search 
{ 
    "query": { 
    "match": { 
     "vital.full_name": {   <-- fix this 
     "query": "Tom Do", 
     "operator": "and" 
     } 
    } 
    } 
} 

像您期望雙方會工作。

+0

還需要什麼? – Val

+0

不,不錯! – emarel