2016-06-10 68 views
1

我想在查詢時使用不同的分析器來組成我的查詢。如何使用Elasticsearch在查詢時指定不同的分析器?

我讀的是可能從文檔 「Controlling Analysis」:

[...]在搜索時的全序列:

  • 在查詢本身定義的分析,否則
  • 在字段映射中定義的search_analyzer,否則
  • 在字段映射中定義的分析器,否則
  • 在指標設置命名default_search分析儀,默認爲
  • 分析儀命名默認的索引設置,默認爲
  • 標準分析器

但我不知道如何構成查詢,以便爲不同的條文指定不同的分析儀:

"query" => [ 
    "bool" => [ 
     "must" => [ 
      { 
       "match": ["my_field": "My query"] 
       "<ANALYZER>": <ANALYZER_1> 
      } 
     ], 
     "should" => [ 
      { 
       "match": ["my_field": "My query"] 
       "<ANALYZER>": <ANALYZER_2>  
      } 
     ] 
    ] 
] 

我知道我可以指數兩個或多個不同的領域,但我有較強的輔助存儲器的約束和我無法將相同的信息索引N次。

謝謝

回答

1

如果你還沒有,你首先需要自定義分析器映射到索引設置終點。

注意:如果索引存在並且正在運行,請確保先關閉它。

POST /my_index/_close

然後映射自定義分析儀的設置終點。

PUT /my_index/_settings 
{ 
    "settings": { 
    "analysis": { 
     "analyzer": { 
     "custom_analyzer1": { 
      "type": "standard", 
      "stopwords_path": "stopwords/stopwords.txt" 
     }, 
     "custom_analyzer2": { 
      "type": "standard", 
      "stopwords": ["stop", "words"] 
     } 
     } 
    } 
    } 
} 

再次打開索引。

POST /my_index/_open

現在你可以使用新的分析器查詢索引。

GET /my_index/_search 
{ 
    "query": { 
    "bool": { 
     "should": [{ 
     "match": { 
      "field_1": { 
      "query": "Hello world", 
      "analyzer": "custom_analyzer1" 
      } 
     } 
     }], 
     "must": [{ 
     "match": { 
      "field_2": { 
      "query": "Stop words can be tough", 
      "analyzer": "custom_analyzer2" 
      } 
     } 
     }] 
    } 
    } 
} 
相關問題