3

I am getting some search results back from Elastic in this format

{
  "took": 267,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1911,
    "max_score": 29.118078,
    "hits": [
      {
        "_index": "myIndex",
        "_type": "doc",
        "_id": "27c9d14495f7732c6756f0d2688874f",
        "_score": 21.179974,
        "_source": {
          "file": {
            "filename": "file1.pdf"
          },
          "content": "samplecontent"
        }
      },
      {
        "_index": "myIndex",
        "_type": "doc",
        "_id": "27c9d14495f7732c6756f0d2688874f",
        "_score": 21.179974,
        "_source": {
          "file": {
            "filename": "file2.pdf"
          },
          "content": "samplecontent"
        }
      },
      {
        "_index": "myIndex",
        "_type": "doc",
        "_id": "27c9d14495f7732c6756f0d2688874f",
        "_score": 21.179974,
        "_source": {
          "file": {
            "filename": "file3.pdf"
          },
          "content": "samplecontent"
        }
      }
    ]
  }
}

and would like to display the hits.total (1911) in the header of my grid for example, and hits.hits._source.file.filename and hits.hits._source.content in my grid. I believe I need to setup a hasMany/belongsTo model but am a bit confused by any documentation/examples that I've found as to how to setup that relationship, as well as pick and choose those nested fields to display in the grid. Any help or clarification would be appreciated

My store is defined as

Ext.define('search.store.results', {
    extend: 'Ext.data.Store',

    alias: 'store.results',

    model: 'search.model.hits',

    autoLoad: false,

    proxy: {
        type: 'ajax',
        url: 'http://myServer:9200/_search',
        reader: {
            type: 'json',
            rootProperty: 'hits'
        }
    }
});

And my models are defined as

base

Ext.define('search.model.Base', {
    extend: 'Ext.data.Model',

    schema: {
        namespace: 'search.model'
    }
});

hits

Ext.define('search.model.hits', {
    extend: 'search.model.Base',

    fields: [
        {name: 'total', type: 'number'}
    ],
    hasMany:{
        model: 'hit',
        name: 'hit'
    }
});

hit

Ext.define('search.model.hit', {
    extend: 'search.model.Base',

    fields: [
        {name: '_source', type: 'source'}
    ]
});

source

Ext.define('search.model.source', {
    extend: 'search.model.Base',

    fields: [
        {name: 'file', type: 'file'},
        {name: 'content', type: 'string'}
    ]
});

and file

Ext.define('search.model.file', {
    extend: 'search.model.Base',

    fields: [
        {name: 'filename', type: 'string'}
    ]
});

however when I load my page I get these errors right off the bat

Uncaught Error: [Ext.createByAlias] Unrecognized alias: data.field.source

and

Uncaught Error: [Ext.createByAlias] Unrecognized alias: data.field.file

UPDATE: This is what the results look like if I change my models to use 'reference' instead of hasMany

Ext.define('search.model.hits', {
    extend: 'search.model.Base',

    fields: [
        {name: 'total', type: 'number'},
        {name: 'hits', reference: 'hit'}
    ]
});

Ext.define('search.model.hit', {
    extend: 'search.model.Base',

    fields: [
        {name: '_source', reference: 'source'}
    ]
});

Ext.define('search.model.source', {
    extend: 'search.model.Base',

    fields: [
        {name: 'file', reference: 'file'},
        {name: 'content', type: 'string'}
    ]
});

Ext.define('search.model.file', {
    extend: 'search.model.Base',

    fields: [
        {name: 'filename', type: 'string'}
    ]
});

My store 'load' listener shows this information, broken into separate data structures instead of all being under the 'data' structure where the arrow is (where I'm used to finding my store records)

enter image description here

UPDATE #2: If I modify my 'hits' model to use hasMany but leave the rest using 'reference' it appears to work as far as grouping my data properly in the records - now I just need to know how to display the nested results in a grid

Ext.define('search.model.hits', {
    extend: 'search.model.Base',

    fields: [
        {name: 'total', type: 'number'}
    ],
    hasMany:{
        model: 'hit',
        name: 'hit'
    }
});

produces

enter image description here

a344254
  • 571
  • 2
  • 9
  • 20
  • 1
    just wondering why you do not use ``reference: 'file'`` – Snehal Dangroshiya Jun 28 '18 at 05:52
  • No reason, other than when I searched for how to load nested JSON the examples seemed to indicate that I needed to use hasMany. I'll edit my post and show what it looks like when I use 'reference' - it doesn't put the array of 'hits' in the normal data structure where the arrow is, but instead separates each of the models/references into it's own structure which I think means it's not associating all of the data properly. – a344254 Jun 28 '18 at 11:43
  • oh yes, there are two different ways to achieve association. do u think it makes seans to use ``type: search.model.file`` instead of ``type:file`` – Snehal Dangroshiya Jun 28 '18 at 14:15

0 Answers0