无法让通配符查询在 elasticsearch 中的多个字段上工作



我正在尝试让通配符在弹性搜索中的多个字段上工作,但它似乎不起作用。当我使用它时,它只返回与提交空查询相同的结果。这是我的查询:

{
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"ratingCount": {
"order": "desc"
}
},
{
"avgRating": {
"order": "desc"
}
}
],
"from": 20,
"size": 20,
"query": {
"bool": {
"filter": {
"term": {
"cities": "59c95a090338d4fe4aea6af8"
}
},
"should": [
{
"wildcard": {
"firstName": "Mich*"
}
},
{
"wildcard": {
"lastName": "Mich*"
}
}
]
}
}
}

也许from属性是你的问题?查看起始/大小文档。从 20 表示与第一个结果的偏移量为 20。这意味着您总共至少需要 21 个结果才能在查询中看到一条记录。

举个例子。先放三个记录:

PUT index/_doc/1
{
"cities": "59c95a090338d4fe4aea6af8",
"firstName": "Michael",
"lastName": "Jordan"
}
PUT index/_doc/2
{
"cities": "59c95a090338d4fe4aea6af8",
"firstName": "Tomasz",
"lastName": "Michalowicz"
}
PUT index/_doc/3
{
"cities": "59c95a090338d4fe4aea6af8",
"firstName": "Bartosz",
"lastName": "Michalski"
}

然后搜索,将查询和from设置为 3:

GET _search
{
"from": 3,
"query": {
"bool": {
"filter": {
"term": {
"cities": "59c95a090338d4fe4aea6af8"
}
},
"should": [
{
"wildcard": {
"firstName": "Mich*"
}
},
{
"wildcard": {
"lastName": "Mich*"
}
}
]
}
}
}

响应将是:

{
"took": 15,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
}
}

您可以看到没有可见的hitstotal等于 3。

然后将from更改为 2 并再次查询:

GET _search
{
"from": 2,
"query": {
"bool": {
"filter": {
"term": {
"cities": "59c95a090338d4fe4aea6af8"
}
},
"should": [
{
"wildcard": {
"firstName": "Mich*"
}
},
{
"wildcard": {
"lastName": "Mich*"
}
}
]
}
}
}

答案是:

{
"took": 13,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": [
{
"_index": "twitter",
"_type": "_doc",
"_id": "3",
"_score": 0,
"_source": {
"cities": "59c95a090338d4fe4aea6af8",
"firstName": "Bartosz",
"lastName": "Michalski"
}
}
]
}
}

然后将from更改为 0 并再次查询:

GET _search
{
"from": 0,
"query": {
"bool": {
"filter": {
"term": {
"cities": "59c95a090338d4fe4aea6af8"
}
},
"should": [
{
"wildcard": {
"firstName": "Mich*"
}
},
{
"wildcard": {
"lastName": "Mich*"
}
}
]
}
}
}

响应:

{
"took": 8,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": [
{
"_index": "twitter",
"_type": "_doc",
"_id": "2",
"_score": 0,
"_source": {
"cities": "59c95a090338d4fe4aea6af8",
"firstName": "Tomasz",
"lastName": "Michalowicz"
}
},
{
"_index": "twitter",
"_type": "_doc",
"_id": "1",
"_score": 0,
"_source": {
"cities": "59c95a090338d4fe4aea6af8",
"firstName": "Michael",
"lastName": "Jordan"
}
},
{
"_index": "twitter",
"_type": "_doc",
"_id": "3",
"_score": 0,
"_source": {
"cities": "59c95a090338d4fe4aea6af8",
"firstName": "Bartosz",
"lastName": "Michalski"
}
}
]
}
}

最新更新