弹性搜索索引创建与映射 [英] Elasticsearch index creation with mapping

查看:183
本文介绍了弹性搜索索引创建与映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力创建索引的简单任务,目标是使用分析器和字段映射创建索引。当我使用分析器创建索引时,我可以通过分析api调用与分析器通信,但是当我添加映射信息时,创建索引调用失败,并且找不到用于字段[$ field]]的分析器[analyzer1],我创建一个脚本来显示问题:

I'm struggling with the simple task of index creation, the goal is to create an index with an analyzer and a field mapping. When I create a index with an analyzer i can talk to the analyzer via the analyze api calls, but when I add the mapping information the create index calls fails with "Analyzer [analyzer1] not found for field [$field]]", I created a script to show the problem:

    #!/bin/bash

    INDEX_NAME="test1"

    echo "delete index just to be sure"
    curl -XDELETE "http://localhost:9200/$INDEX_NAME/"; echo

    echo "create new index"
    curl -X PUT "http://localhost:9200/$INDEX_NAME/" -d '{
        "index":{
            "analysis":{
                "analyzer":{
                    "analyzer1":{
                        "type":"custom",
                        "tokenizer":"standard",
                        "filter":[ "standard", "lowercase", "stop", "kstem", "ngram" ]
                    }
                },
                "filter":{
                    "ngram":{
                        "type":"ngram",
                        "min_gram":2,
                        "max_gram":15
                    }
                }
            }
        }
    }'; echo

    echo "analyze something with our shiny new analyzer"
    curl -XGET "localhost:9200/$INDEX_NAME/_analyze?analyzer=analyzer1&pretty=true" -d 'abcd'

    echo "remove the created index"
    curl -XDELETE "http://localhost:9200/$INDEX_NAME/"; echo

    echo "create new index again with mapping"
    curl -X PUT "http://localhost:9200/$INDEX_NAME/" -d '{
        "index":{
            "analysis":{
                "analyzer":{
                    "analyzer1":{
                        "type":"custom",
                        "tokenizer":"standard",
                        "filter":[ "standard", "lowercase", "stop", "kstem", "ngram" ]
                    }
                },
                "filter":{
                    "ngram":{
                        "type":"ngram",
                        "min_gram":2,
                        "max_gram":15
                    }
                }
            }
        },
        "mappings": {
            "product": {
                "properties": {
                    "title": {
                        "type": "string",
                        "search_analyzer" : "analyzer1",
                        "index_analyzer" : "analyzer1"
                    }
                }
            }
        }
    }'; echo


推荐答案

我相信你的问题是分析设置需要嵌套在JSON中的设置节点中,而不在索引节点。请参阅弹性搜索创建索引API ,了解有关构建JSON。

I believe your issue is that the analysis settings need to be nested within a settings node in your JSON, not within an index node as you have it. Please reference the Elasticsearch Create Index API for details on constructing the JSON.

因此,您的创建索引调用应如下所示:

Therefore, your create index call should look like the the following:

curl -X PUT "http://localhost:9200/$INDEX_NAME/" -d '{
    "settings":{
        "analysis":{
            "analyzer":{
                "analyzer1":{
                    "type":"custom",
                    "tokenizer":"standard",
                    "filter":[ "standard", "lowercase", "stop", "kstem", "ngram" ]
                }
            },
            "filter":{
                "ngram":{
                    "type":"ngram",
                    "min_gram":2,
                    "max_gram":15
                }
            }
        }
    },
    "mappings": {
        "product": {
            "properties": {
                "title": {
                    "type": "string",
                    "search_analyzer" : "analyzer1",
                    "index_analyzer" : "analyzer1"
                }
            }
        }
    }
}';

这篇关于弹性搜索索引创建与映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆