我正在尝试获取具有以下字段的项目的列表:resellable: true
。
以下是我的数据集,我可以通过访问:my-domain.com/_all/listings/_search?
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "collectibles",
"_type" : "listing",
"_id" : "SseZfNbpdpBxc3O",
"_score" : 1.0,
"_source" : {
"data" : {
"resellable" : true,
"active" : true,
"description" : "<p>1234 123 123 123</p>",
"title" : "2134",
"has_store" : true,
"createdAt" : "Wed May 27 2020 04:23:18 GMT+0000 (Coordinated Universal Time)",
"apiURL" : "kJ9zsIsdQG8TZrRfPco4",
"id" : "SseZfNbpdpBxc3O",
"state" : "PENDING",
"amount" : "21",
}
}
}
}
我的查询正在使用NodeJS和ElasticSearch.js
let results = await client.search({
index: params.category,
type: "listing",
body: {
query: {
multi_match: {
query: true,
fields: ['resellable', 'active'],
type: 'best_fields',
}
}
}
})
响应总是没有成功。我尝试过匹配,没有尝试过“ best_fields”,但似乎与值不匹配。
我在这里做错了什么?您还需要执行其他操作来查询_source.data级别的项目吗?
ElasticSearch版本:7.4
[使用multi_match时以字符串格式使用"true"
,而您以布尔格式true
为数据建立索引,这就是未获得任何匹配的原因。
[我刚刚注意到您的multi_match
字段(resellable
和active
)都是布尔值,那么为什么要使用multi_match查询,您应该改用boolean filter query,它也要缓存并提供更好的性能。
样本索引映射
{
"mappings": {
"properties": {
"active": {
"type": "boolean"
},
"resellable" :{
"type" : "boolean"
}
}
}
}
索引各种示例文件
{
"active" : true,
"resellable" : true
}
{
"active" : true,
"resellable" : false
}
{
"active" : false,
"resellable" : false
}
{
"active" : false,
"resellable" : true
}
搜索查询以获取两个值都为真的文档
{
"query": {
"bool": {
"filter": [
{
"match": {
"active": true
}
},
{
"match": {
"resellable": true
}
}
]
}
}
}
结果
"hits": [
{
"_index": "filterbool",
"_type": "_doc",
"_id": "1",
"_score": 0.0,
"_source": {
"active": true,
"resellable": true
}
}
]