Lupus
Lupus

Reputation: 1518

elasticsearch usage of terms

I'm fairly new to elasticsearch and query DSL. I'm trying to build a fuzzy search supported filter endpoint with node.js and @elastic/elasticsearch.

I have a data structure like below;

{
  name: "product 1",
  price: 43,
  categories: [5, 6],
  description: "lorem...",
  product_attribute_values: [{
    attribute_id: 1,
    attribute_value_id: 1
  }]
},
{
  name: "product 2",
  price: 45,
  categories: [4, 6],
  description: "lorem...",
  product_attribute_values: [{
    attribute_id: 1,
    attribute_value_id: 2
  }]
},
{
  name: "product 3",
  price: 57,
  categories: [1, 6, 7],
  description: "lorem...",
  product_attribute_values: [{
    attribute_id: 2,
    attribute_value_id: 3
  }]
},
{
  name: "product 4",
  price: 79,
  categories: [5],
  description: "lorem...",
  product_attribute_values: [{
    attribute_id: 2,
    attribute_value_id: 4
  }]
},

I'm trying to get hits with categories 4 or 5 (product 1, 2 and 4)

In a separate or in the same query I also try to create a set of product_attribute_values from those hits. Which should be;

  product_attribute_values: [{
    attribute_id: 1,
    attribute_value_id: 1
  }, {
    attribute_id: 1,
    attribute_value_id: 2
  }, {
    attribute_id: 2,
    attribute_value_id: 4
  }]

Until now I created this query;

  index: 'products',
  size: 60,
  body: {
    query: {
      bool : { 
        must : [{
            terms : { 
              "categories" : [4, 5] }}
        ]
      }
    }
  }

But I'm not sure if this is the right way to go through. PS: Also another small thing that confuses me is how to change the index.max_result_window value programmatically. I'm going to use pagination on these results and I need all of the possible attributes including the ones that filtered by size limit.

Upvotes: 0

Views: 58

Answers (1)

littledaxter
littledaxter

Reputation: 121

You could use update_by_query (instead of _search) to directly update the documents returned by your first query if the value you try to update is static or not based on the documents returned by the query itself (in which case it might be easier to do in you nodeJS code).

for index.max_result_window you can set its value using the index setting api:

PUT /products/_settings
{
    "index" : {
        "max_result_window" : 50000
    }
}

the linked doc of max_result_windows contains links to other methods of handling pagination of results; you might want to explore that too. The basic way to handle pagination (and the size limit) is to use the "from" parameter in your query. from is the position of the document in your results from which you start receiving the values until the size.

ex: from:0 size:1000 will return the first 1k docs of the results of the query (1st page) from:1000 size:1000 is page 2 (the next 1k, starting at 1k to 2k) from:2000 size:1000 is page 3 and so on and so on... sniff

Upvotes: 1

Related Questions