弹性搜索

时间:2016-02-07 17:03:28

标签: elasticsearch

我正准备使用Elastic Search进行演示。

我已经设置了我的数据库,使用了Logstash的JDBC输入并开始索引数据库表。它在一定程度上都工作得很好。但部分单词搜索不起作用。在Elastic文档中快速搜索建议使用nGram过滤器和词干分析器。由于我没有得到词干分析器,所以我遵循了ngram过滤器的路径。

以下是我的索引映射,



{
 "settings" : {
	"analysis" : {
		"analyzer" : {
			"translation_index_analyzer" : {
				"type" : "custom",
				"tokenizer" : "standard",
				"filter" : "standard, lowercase, translation"
			},
			"translation_search_analyzer" : {
				"type" : "custom",
				"tokenizer" : "standard",
				"filter" : "standard, lowercase"
			}		
		},
		"filter" : {
			"translation" : {
				"type" : "nGram",
				"min-gram" : 3,
				"max-gram" : 12
			}
		}
    	}
 }, 
 "mappings" : {
        "type" : {
		"properties" : {
		     "myType" : {
			"type" : "string",
                        "index_analyzer" : "translation_index_analyzer",
                        "search_analyzer" : "translation_search_analyzer"
		     }	
		}
        }, 
        "employee" : {
            "properties" : {
                      "birth_date": {
                             "type": "date",
                            "format" : "yyyy-MM-dd HH:mm:ss ZZ"
          },
          "emp_no": {
                   "type": "long",
                    "index" : "not_analyzed"
          },
          "first_name": {
            "type": "myType"
          },
          "gender": {
            "type": "string",
              "index" : "not_analyzed"
          },
          "hire_date": { 
                             "type": "date",
                            "format" : "yyyy-MM-dd HH:mm:ss ZZ"
         },
          "last_name": {
            "type": "string"
          }
            }
        }
    }
   




我更倾向于猜测类型映射,因为我需要这样的东西。不确定那是否正确。

当我发布此消息时,我收到错误



{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "analyzer on field [myType] must be set when search_analyzer is set"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping [type]: analyzer on field [myType] must be set when search_analyzer is set",
    "caused_by": {
      "type": "mapper_parsing_exception",
      "reason": "analyzer on field [myType] must be set when search_analyzer is set"
    }
  },
  "status": 400
}




我有错的任何线索?

更新:我的弹性版本是2.2
从index_analyzer更改为分析器会产生不同的错误



{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "No handler for type [myType] declared on field [first_name]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping [employee]: No handler for type [myType] declared on field [first_name]",
    "caused_by": {
      "type": "mapper_parsing_exception",
      "reason": "No handler for type [myType] declared on field [first_name]"
    }
  },
  "status": 400
}




最终解决方案: 好吧,我的问题是定义一个类型。我暂时会忽略但部分搜索适用于以下设置



{
"settings" : {
            "analysis" : {
                "analyzer" : {
                    "my_ngram_analyzer" : {
                        "tokenizer" : "my_ngram_tokenizer"
                    }
                },
                "tokenizer" : {
                    "my_ngram_tokenizer" : {
                        "type" : "nGram",
                        "min_gram" : "3",
                        "max_gram" : "12",
                        "token_chars": [ "letter", "digit" ]
                    }
                }
            }
},
 
 "mappings" : {
 
        "employee" : {
            "properties" : {
                      "birth_date": {
                             "type": "date",
                            "format" : "yyyy-MM-dd HH:mm:ss ZZ"
          },
          "emp_no": {
                   "type": "long",
                    "index" : "not_analyzed"
          },
          "first_name": {
            "type": "string",
                        "analyzer" : "my_ngram_analyzer",
                        "search_analyzer" : "my_ngram_analyzer"
          },
          "gender": {
            "type": "string",
              "index" : "not_analyzed"
          },
          "hire_date": { 
                             "type": "date",
                            "format" : "yyyy-MM-dd HH:mm:ss ZZ"
          },
          "last_name": {
            "type": "string",
                        "analyzer" : "my_ngram_analyzer",
                        "search_analyzer" : "my_ngram_analyzer"
          }
            }
        }
    }
}




1 个答案:

答案 0 :(得分:0)

我的猜测,请index_analyzer

更改analyzer
  • 它依赖于您的弹性搜索版本

修改

我不熟悉创建类型,但您可以使用相关分析器初始化first_name

"first_name": {
  "type" : "string",
  "analyzer" : "translation_index_analyzer",
  "search_analyzer" : "translation_search_analyzer"
},