Search and get distenic result base on create_at in elasticsearch


(Mohammad) #1

I store data in the elastic search like this:

 {
    "_index": "my_index",
    "_type": "doc",
    "_id": "6lDquGEBFRQVe0x93eHk",
    "_version": 1,
    "_score": 1,
    "_source": {
    "Barcode": "6947503728601",
    "category":"pen",
    "name": "panter",
    "price":"4$",
    "created_at": "1519225855"
    }
    }

Each data set has a barcode. For example, all panter pen have "6947503728601" as a barcode but they have different created_at values.

I want to search for partial barcode number(69475) and get all matching docs that are unique in barcode field with the latest one of them based on the created_at value (I want to get latest document data for each barcode number)

  1. How can I implement the search?

  2. is my algorithm correct?


Find by query and push to array in Elastic search
(David Pilato) #2

Use a ngram based analyzer for partial matching and sort by created_at field?f


(Mohammad) #4

I think you miss understood my question.

my data is like this:

[   
  {   
      product_name:'pen',   
      product_category:'writing_supplies',   
      price:2,   
      quantity:1000,  
      created_at: "1519225855"  
  },   
  {   
      product_name:'pen',   
      product_category:'writing_supplies',   
      price:5,   
      quantity:150,  
      created_at: "1519281254"  
  },   
  {   
      product_name:'uni_pen',   
      product_category:'writing_supplies',   
      price:45,   
      quantity:10,  
      created_at: "1519281732"  
  },   
]  

i want to search products with 'product_name' and group by 'product_name' and get the last one based on their 'created_at'.

for example:

after searching for : 'pen' ,

I want following result:

[  
    {   
          product_name:'pen',   
          product_category:'writing_supplies',   
          price:5,   
          quantity:150,  
          created_at: "1519281254"  
      },  
      {   
          product_name:'uni_pen',   
          product_category:'writing_supplies',   
          price:45,   
          quantity:10,  
          created_at: "1519281732"  
    },   
]

(David Pilato) #5

May be a terms aggregation and then an inner top hits agg sorted by date with size: 1 is what you need.


(Mohammad) #6

what about size 1 ?
is it returns one for each product_category or it returns just one product?


(Mohammad) #7

could you give me an example query for this result?


(David Pilato) #8

Have a look at the documentation, give a try and share a full script if you don't succeed. Then we can help from here.


(Mohammad) #9

Thanks a lot
It was a great help
:ok_hand::+1::+1::+1::+1:


(system) #10

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.