[英]Mapping multiple fields to one index with elasticsearch-rails
如何将以下三个字段映射到一个称为“实体”的索引? 以下代码仅导致三个索引中的第一个索引。 目标是能够将这三个字段中的任何一个作为“实体”进行查询。
indexes :thing1, index_name: "entity" do
indexes :name, type: 'string', boost: 1.0
end
indexes :thing2, index_name: "entity" do
indexes :name, type: 'string', boost: 2.0
end
indexes :thing3, index_name: "entity" do
indexes :name, type: 'string', boost: 0.2
end
看一下多字段映射
"title": {
"type": "string",
"fields": {
"raw": { "type": "string", "index": "not_analyzed" }
}
}
希望我能理解您的问题。
如果只需要一个索引,称为“实体”,则将三个文档类型(thing1,thing2和thing3)索引到其中; 并且每种文档类型都映射为具有一个名为“名称”的属性,那么您的设置可能是正确的(我不知道ruby界面)。 您可以将搜索查询发布到索引“实体”,而无需指定文档类型或指定要搜索的所有类型。
查询的正文将指定“名称”字段作为术语/匹配/任何目标。
{
"query": {
"match" : {
"name" : "Happy the Client"
}
}
}
请参阅此处以在搜索URL中指定多个索引/类型: http : //www.elasticsearch.org/guide/zh-CN/elasticsearch/guide/current/multi-index-multi-type.html
在所有索引中搜索所有类型
http://localhost:9200/_search
在索引“实体”中搜索所有类型:
http://localhost:9200/entity/_search
在索引“实体”中搜索指定的类型:
http://localhost:9200/entity/thing1,thing2,thing3/_search
也许(如果您没有要忽略的东西4):
http://localhost:9200/entity/thing*/_search
您会在上面的链接中注意到,即使将它们放在不同的索引中,也可以通过单个查询完成搜索多种文档类型的目标。 如果每个文档类型实际上都是在索引中排他地索引,且其名称与类型名称匹配,则可以将相同的“名称”查询发送到以下URL:
搜索所有索引中的所有类型:
http://localhost:9200/_search
搜索指定索引中的所有类型:
http://localhost:9200/thing1,thing2,thing3/_search
在指定索引中搜索指定类型:
http://localhost:9200/thing1,thing2,thing3/thing1,thing2,thing3/_search
在所有索引中搜索指定的类型:
http://localhost:9200/_all/thing1,thing2,thing3/_search
在我认为您描述过的场景中,您有一个方便的事实,即每种文档类型都有一个通用的属性名称“名称”。
如果您是人为地提供了该约束以简化问题,那么我想谈谈另一个功能。 假设每种文档类型的属性名称都不同。 类型“ thing1”具有字符串属性“ name1”,类型“ thing2”具有字符串属性“ name2”,类型“ thing3”具有字符串属性“ name3”。 我们希望有一个像上面的查询这样的通用查询,可以搜索所有这些类型。
{
"query": {
"match" : {
"name" : "Happy the Client"
}
}
}
为此,具有“ copy_to”功能: http ://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#copy-to
场景中的映射如下所示:
{
"thing1" : {
"properties" : {
"name1" : { "type" : "string", "copy_to" : "name" },
"name" : { "type" : "string" }
}
}
{
"thing2" : {
"properties" : {
"name2" : { "type" : "string", "copy_to" : "name" },
"name" : { "type" : "string" }
}
}
{
"thing3" : {
"properties" : {
"name3" : { "type" : "string", "copy_to" : "name" },
"name" : { "type" : "string" }
}
}
搜索网址将遵循与上述相同的指导原则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.