简体   繁体   中英

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"; 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). 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

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):

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:

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". 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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