我对Elasticsearch和查询DSL相当陌生。我正在尝试使用node.js和@elastic/elasticsearch
构建模糊搜索支持的过滤器端点。
我的数据结构如下:
{
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
}]
},
我正在尝试获得类别4或5(产品1、2和4)的匹配
在单独或相同的查询中,我还尝试从这些匹配中创建一组
product_attribute_values
。应该是; product_attribute_values: [{
attribute_id: 1,
attribute_value_id: 1
}, {
attribute_id: 1,
attribute_value_id: 2
}, {
attribute_id: 2,
attribute_value_id: 4
}]
到目前为止,我创建了该查询;
index: 'products',
size: 60,
body: {
query: {
bool : {
must : [{
terms : {
"categories" : [4, 5] }}
]
}
}
}
但是我不确定这是否是正确的方法。
PS:另一个令我困惑的小事情是如何以编程方式更改
index.max_result_window
值。我将对这些结果使用分页,并且我需要所有可能的属性,包括那些通过size
限制过滤的属性。
最佳答案
如果您尝试更新的值是静态的还是不是基于查询本身返回的文档,则可以使用update_by_query(而不是_search)直接更新您的第一个查询返回的文档(在这种情况下,这样做可能会更容易您的nodeJS代码)。
对于index.max_result_window,您可以使用index setting api设置其值:
PUT /products/_settings
{
"index" : {
"max_result_window" : 50000
}
}
max_result_windows的链接文档包含指向其他处理结果分页的方法的链接;您可能也想探索一下。处理分页(和大小限制)的基本方法是在查询中使用“from”参数。 from是结果中文档的位置,从该位置开始接收值直到大小。
例如:
from:0 size:1000将返回查询结果的前1k文档(第一页)
from:1000 size:1000 is page 2(接下来的1k,从1k到2k开始)
从:2000大小:1000是第3页
等等等等...嗅探
关于elasticsearch - 术语的 Elasticsearch ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57710564/