2017-01-02 32 views
0

在elasticSearch, 如何可以定義一個動態默認映射任何字段(字段不是預定義的),其與可搜索空間和不區分大小寫的值。搜索在所有領域,不區分大小寫和不分析

舉例來說,如果我有兩個文件:

PUT myindex/mytype/1 
{ 
    "transaction": "test" 
} 

PUT myindex/mytype/2 
{ 
    "transaction": "test SPACE" 
} 

我想執行以下查詢:

Querying: "test", Expected result: "test" 
Querying: "test space", Expected result "test SPACE" 

我試着用途:

PUT myindex 
{ 
"settings":{ 
    "index":{ 
     "analysis":{ 
      "analyzer":{ 
       "analyzer_keyword":{ 
       "tokenizer":"keyword", 
       "filter":"lowercase" 
       } 
      } 
     } 
    } 
    }, 
    "mappings":{ 
    "test":{ 
     "properties":{ 
      "title":{ 
       "analyzer":"analyzer_keyword", 
       "type":"string" 
      } 
     } 
    } 
    } 
} 

但找「測試」時,它給了我兩個文件作爲結果。

+0

你能也表明你正在做準確的查詢?確保使用「term」查詢而不是「match」 – Val

+0

另一件需要確認的事情是您正在使用'mytype'映射類型創建文檔,但映射定義被命名爲'test'。 – Val

回答

0

顯然有條我查詢了一個錯誤: 這裏有一個解決方案,我發現這個問題,採用多領域查詢時:

#any field mapping - not analyzed and case insensitive 
PUT /test_index 
{ 
    "settings": { 
    "index": { 
     "analysis": { 
     "analyzer": { 
      "analyzer_keyword": { 
      "tokenizer": "keyword", 
      "filter": ["lowercase"] 
      } 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "doc": { 
     "dynamic_templates": [ 
      { "notanalyzed": { 
        "match_mapping_type": "string", 
        "mapping": { 
         "type":  "string", 
         "analyzer":"analyzer_keyword" 
        } 
       } 
      } 
      ] 
     } 
    } 
} 


#index test data 
POST /test_index/doc/_bulk 
{"index":{"_id":3}} 
{"name":"Company Solutions", "a" : "a1"} 
{"index":{"_id":4}} 
{"name":"Company", "a" : "a2"} 


#search for document with name 「company」 and a 「a1」 
POST /test_index/doc/_search 
{ 
    "query" : { 
    "filtered" : { 
     "filter": { 
     "and": { 
      "filters": [ 
      { 
       "query": { 
       "match": { 
        "name": "company" 
       } 
       } 
      }, 
      { 
       "query": { 
       "match": { 
        "a": "a2" 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
}