ElasticSearch - Depth Gain in a Recursive Structure

I am using Elastic search 2.4.4 (compatible with spring boot 1.5.2).

I have a document object that has the following structure:

{
    id : 1,
    title : Doc title
    //some more metadata
    sections :[
        {
           "id" : 2,
           "title: Sec title 1,
           sections:[...]
        },{
           id : 3,
           title: Sec title 2,
           sections:[...]
        }

     ]
}

Basically I want to make the headings in the document searchable (all document names, section headings and section headings at any level), and I want to be able to evaluate documents based on the level at which they match in the tree hierarchy.

My initial thought was that we used this structure:

 {
    titles:[
          {
           title : doc title,
           depth : 0
          },
          {
           title : sec title 1,
           depth : 1
          },
          {
           title : sec title 2,
           depth : 1
          },
          ......
   ] 
 }

I would like to rank documents based on the depth at which there is a match (higher depth, lower - rating).

I know the basic field-based gain, but

is there any way to do this in elastic search?

OR

Can this be done by changing the structure?

+4
1

, , ( ), Nested datatype mapping :

PUT someindex
{
    "mappings": {"sometype":{"properties": {"titles":{"type": "nested"}}}}
}

POST someindex/sometype/0
{
   "titles": [
      { "title": "doc title", "depth": 0 },
      { "title": "sec title 1", "depth": 1 },
      { "title": "sec title 2", "depth": 1 }
   ]
}

POST someindex/sometype/1
{
   "titles": [
      { "title": "sec doc title", "depth": 0 }
   ]
}

GET someindex/sometype/_search
{
   "query": {
      "nested": {
         "path": "titles",
         "score_mode": "max",
         "query": {
            "function_score": {
               "query": {
                  "match": {
                     "titles.title": "sec"
                  }
               },
               "functions": [
                  {
                     "exp": {
                        "titles.depth": {
                           "origin": 0,
                           "scale": 1
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

1 , , sec 0, 2 , sec 1.

, function_score , exp .

0

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


All Articles