简体   繁体   English

使用elasticsearch-rails将多个字段映射到一个索引

[英]Mapping multiple fields to one index with elasticsearch-rails

How might I map the following three fields to one index called "entity"? 如何将以下三个字段映射到一个称为“实体”的索引? The following code only results in an index of the first of the three. 以下代码仅导致三个索引中的第一个索引。 The goal is to be able to query any of these three fields as an "entity". 目标是能够将这三个字段中的任何一个作为“实体”进行查询。

  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

Take a look at Multi-field mapping 看一下多字段映射

"title": {
    "type": "string",
    "fields": {
        "raw":   { "type": "string", "index": "not_analyzed" }
    }
}

I hope I understand your question. 希望我能理解您的问题。

If what you want is a single index, called "entity", into which you will index three document types, "thing1", "thing2", and "thing3"; 如果只需要一个索引,称为“实体”,则将三个文档类型(thing1,thing2和thing3)索引到其中; and each of those document types is mapped to have a property called "name", then your setup is probably correct (I don't know the ruby interface). 并且每种文档类型都映射为具有一个名为“名称”的属性,那么您的设置可能是正确的(我不知道ruby界面)。 You would just post your search queries to the index "entity", either without specifying the document type, or specifying all types you want to search. 您可以将搜索查询发布到索引“实体”,而无需指定文档类型或指定要搜索的所有类型。

The body of your query would specify the "name" field as the term/match/whatever target. 查询的正文将指定“名称”字段作为术语/匹配/任何目标。

{ 
    "query": {
        "match" : {
            "name" : "Happy the Client"
        }
    }
}

See here for specifying multiple indices/types in the search url: http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/multi-index-multi-type.html 请参阅此处以在搜索URL中指定多个索引/类型: http : //www.elasticsearch.org/guide/zh-CN/elasticsearch/guide/current/multi-index-multi-type.html

Search all types in all indices 在所有索引中搜索所有类型

http://localhost:9200/_search

Search all types in the index "entity": 在索引“实体”中搜索所有类型:

http://localhost:9200/entity/_search

Search specified types in the index "entity": 在索引“实体”中搜索指定的类型:

http://localhost:9200/entity/thing1,thing2,thing3/_search

or maybe (provided you don't have a thing4 you want to omit): 也许(如果您没有要忽略的东西4):

http://localhost:9200/entity/thing*/_search

You'll notice in the above link that you can accomplish the goal of searching multiple document types with a single query even if you put them in different indices. 您会在上面的链接中注意到,即使将它们放在不同的索引中,也可以通过单个查询完成搜索多种文档类型的目标。 If each document type was actually indexed exclusively in an index with a name that matched the type name, you could send the same "name" query to these URLs: 如果每个文档类型实际上都是在索引中排他地索引,且其名称与类型名称匹配,则可以将相同的“名称”查询发送到以下URL:

Search all types in all indices: 搜索所有索引中的所有类型:

http://localhost:9200/_search

Search all types in specified indices: 搜索指定索引中的所有类型:

http://localhost:9200/thing1,thing2,thing3/_search

Search for specified types in specified indices: 在指定索引中搜索指定类型:

http://localhost:9200/thing1,thing2,thing3/thing1,thing2,thing3/_search

Search for specified types in all indices: 在所有索引中搜索指定的类型:

http://localhost:9200/_all/thing1,thing2,thing3/_search

In the scenario I think you described, you have the convenient fact that each of your document types have a common property name "name". 在我认为您描述过的场景中,您有一个方便的事实,即每种文档类型都有一个通用的属性名称“名称”。

In case you have artificially provided that constraint to simplify the question, I want to touch on another feature. 如果您是人为地提供了该约束以简化问题,那么我想谈谈另一个功能。 Let's say that the property name is different on each of the document types. 假设每种文档类型的属性名称都不同。 Type "thing1" has a string property "name1", type "thing2" has a string property "name2", and type "thing3" has a string property "name3". 类型“ thing1”具有字符串属性“ name1”,类型“ thing2”具有字符串属性“ name2”,类型“ thing3”具有字符串属性“ name3”。 We wish to have a common query like the one above search all of these types. 我们希望有一个像上面的查询这样的通用查询,可以搜索所有这些类型。

{ 
    "query": {
        "match" : {
            "name" : "Happy the Client"
        }
    }
}

For this, there is the feature "copy_to": http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#copy-to 为此,具有“ copy_to”功能: http ://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html#copy-to

The mappings in scenario would look like this: 场景中的映射如下所示:

{
  "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" }
    }
}

The search URLs would be formed following the same guidelines as above. 搜索网址将遵循与上述相同的指导原则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM