Elasticsearch:这个映射的样本文档是什么样的?

时间:2014-07-18 18:52:16

标签: elasticsearch

我是Elasticsearch的新手。我收到了这份映射文件:

{
    "book": {
        "properties": {
            "title": {
                "properties": {
                    "de": {
                        "type": "string",
                        "fields": {
                            "default": {
                                "type": "string",
                                "analyzer": "de_analyzer"
                            }
                        }
                    },
                    "fr": {
                        "type": "string",
                        "fields": {
                            "default": {
                                "type": "string",
                                "analyzer": "fr_analyzer"
                            }
                        }
                    }
                }
            }
        }
    }
}

我想知道符合此映射的示例文档是什么样的,以便我可以生成正确的json字符串。

谢谢和问候。

2 个答案:

答案 0 :(得分:2)

它看起来应该类似于:

{
    "book": {
        "title": {
            "de": "somestring",
            "fr": "somestring"
        }
    }
}

有关详细信息,请参阅this

这就是你的json的样子。我看到你感到困惑:

                   "fr": {
                        "type": "string",
                        "fields": {
                            "fr": {
                                "type": "string",
                                "analyzer": "fr_analyzer"
                            }
                        }
                    }

实际上,这用于以多种方式索引单个字段。有效的你告诉elasticsearch你想要以两种不同的方式索引相同的字段。在你的情况下,我不明白为什么你使用相同的名称“fr”和“de”索引多字段。如果您将使用相同的名称,ES将仅将其编入索引作为单个字段。所以,在你的情况下这样做是类似的事情:

            "fr": {
                "type": "string",
                "analyzer": "fr_analyzer"
             }

有关更多信息,请浏览以下链接: http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/most-fields.html http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/multi-fields.html

希望这有帮助。

答案 1 :(得分:1)

嗯,我在这里很慢,但是,正如dark_shadow指出的那样,这看起来像是在为一个场(以前称为多场映射)暴露多个分析器的复杂尝试。 我已经用两种方式编写了一个映射示例:给标题两个属性fr和de,需要在每个文档中提供显式字符串,并且只有一个title属性,带有fr和de analyzer'字段”。请注意,我将名称分析器替换为“标准”,以防止我必须在本地加载这些分析器。

这些之间的区别很明显。在示例一中,您可以在每本书上提供翻译的标题,索引/搜索分析器适用于相应属性上使用的语言。

在示例二中,您将提供单个标题字符串,将使用两个分析器对其进行索引,然后您可以选择要在查询中搜索的分析器。

#!/bin/sh
echo "--- delete index"
curl -X DELETE 'http://localhost:9200/so_multi_map/'
echo "--- create index and put mapping into place"
curl -XPUT http://localhost:9200/so_multi_map/?pretty -d '{
    "mappings": {
        "example_one": {
            "properties": {
                "title": {
                    "properties": {
                        "de": {
                            "type": "string",
                            "analyzer": "standard"
                        },
                        "fr": {
                            "type": "string",
                            "analyzer": "standard"
                        }
                    }
                }
            }
        },
        "example_two": {
            "properties": {
                "title": {
                    "type": "string",
                    "fields": {
                        "de": {
                            "type": "string",
                            "analyzer": "standard"
                        },
                        "fr": {
                            "type": "string",
                            "analyzer": "standard"
                        }
                    }
                }
            }
        }
    },
    "settings" : {
        "number_of_shards" : 1,
        "number_of_replicas" : 0
    }
}'

echo "--- index a book by example_one by POSTing"
curl -XPOST http://localhost:9200/so_multi_map/example_one -d '{
    "title": {
        "de": "Auf der Suche nach der verlorenen Zeit",
        "fr": "À la recherche du temps perdu"
    }
}'
echo "--- index a book by example_two by POSTing"
curl -XPOST http://localhost:9200/so_multi_map/example_two -d '{
    "title": "À la recherche du temps perdu"
}'
echo "\n --- flush indices"
curl -XPOST 'http://localhost:9200/_flush'
echo "--- show the books"
curl -XGET http://localhost:9200/so_multi_map/_search?pretty -d '
{
    "query": {
        "match_all": {}
    }
}'
echo "--- how to query just the fr title in example one:"
curl -XGET http://localhost:9200/so_multi_map/example_one/_search?pretty -d '
{
    "query": {
        "match": {
            "title.fr": "recherche"
        }
    }
}'
echo "--- how to query just the fr title in example two:"
curl -XGET http://localhost:9200/so_multi_map/example_two/_search?pretty -d '
{
    "query": {
        "match": {
            "title.fr": "recherche"
        }
    }
}'
相关问题