Dynamic mapping for an object field that expands the parent path

I evaluate whether ElasticSearch can satisfy the needs of the new system that I am creating. It looks amazing, so I really hope I can find a matching strategy that works.

On this system, administrators can define fields associated with documents dynamically. Thus, this type (in the sense of the word elasticsearch) can have any number of fields that I do not know the name in advance. And each field can be of any type: int, date, string, etc.

An example document might look like this:

{
    "name": "bob",
    "age": 22,
    "title": "Vice Intern",
    "tagline": "Ask not what your company can do for you, but..."
}

Please note that there are 2 string fields. Awesome. My problem is that I want the "tagline" to be parsed, but I don't want to parse the "title".

, . . , 10 , 3 , 7 .

, , , , , , . , , -, , lucene :

+title:"Vice Intern" +tagline:"company"

, , . , . , , , , !

, , , , :

{
    "name": "bob",
    "age": 22,
    "title": "Vice Intern",
    "textfields": {
        "tagline": "Ask not what your company can do for you, but...",
        "somethingelse": "lorem ipsum",
    }
}    

, , -:

{
    "mytype": {
        "dynamic_templates": {
            "nested_textfields": {
                "match": "textfields",
                "match_mapping_type": "string",
                "mapping": {
                    "index": "analyzed",
                    "analyzer": "default"
                }
            }
        }
    }
}

, , - , , , , , ( lucene ) :

+title:"Vice Intern" +textfields.tagline:"company"

" ". , textfields, , , - .

. , "textfields", , "textfield_tagline". {name} , "*".

, , . ( ? - , ...).

EDIT:

. index_name " ". . , , , , .

    {
        "mytype" : {
            "dynamic_templates":
            [
            {
                "textfields": {
                    "path_match": "textfields.*",
                    "match_mapping_type" : "string",
                    "mapping": {
                        "type": "string",
                        "index": "analyzed",
                        "analyzer": "default",
                        "index_name": "{name}",
                        "fields": {                 
                            "sort": {
                                "type": "string",
                                "index": "not_analyzed",
                                "index_name": "{name}_sort"
                            }
                        }
                    }
                }
            }
            ]
        }
    }
+4
1

, , ( ), . , .

PUT http://localhost:9200/sandbox

{
 "settings": { 
   "index": {
     "number_of_shards": 1,
     "number_of_replicas": 0
   }
 },
 "mappings": {
   "mytype": {
     "dynamic_templates": [
        {
            "indexedfields": {
                "path_match": "indexedfields.*",
                "match_mapping_type" : "string",
                "mapping": {
                    "type": "string",
                    "index": "analyzed",
                    "analyzer": "default",
                    "index_name": "{name}",
                    "fields": {                 
                        "sort": {
                            "type": "string",
                            "index": "not_analyzed",
                            "index_name": "{name}_sort"
                        }
                    }
                }
            }
        },
        {
            "textfields": {
                "path_match": "textfields.*",
                "match_mapping_type" : "string",
                "mapping": {
                    "type": "string",
                    "index": "not_analyzed",
                    "index_name": "{name}"
                }
            }
        },
        {
            "strings": {
                "path_match": "*",
                "match_mapping_type" : "string",
                "mapping": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }

        }
     ]
   }
 }
}

PUT http://localhost:9200/sandbox/mytype/1
{
   "indexedfields":{
      "hello":"Hello world",
      "message":"The great balls of the world are on fire"
   },
   "textfields":{
      "username":"User Name",
      "projectname":"Project Name"
   }
}

POST http://localhost:9200/sandbox/mytype/_search
{
  "query": {
    "query_string": {
      "query": "message:\"great balls\""
    }
  },
   "filter":{
      "query":{
         "query_string":{
            "query":"username:\"User Name\""
         }
      }
   },
   "from":0,
   "size":10,
   "sort":[

   ]
}

:

{
   "took":2,
   "timed_out":false,
   "_shards":{
      "total":1,
      "successful":1,
      "failed":0
   },
   "hits":{
      "total":1,
      "max_score":0.19178301,
      "hits":[
         {
            "_index":"sandbox",
            "_type":"mytype",
            "_id":"1",
            "_score":0.19178301,
            "_source":{
               "indexedfields":{
                  "hello":"Hello world",
                  "message":"The great balls of the world are on fire"
               },
               "textfields":{
                  "username":"User Name",
                  "projectname":"Project Name"
               }
            }
         }
      ]
   }
}
+1

Source: https://habr.com/ru/post/1541383/


All Articles