按字段和值过滤弹性搜索结果

发布于 2025-01-10 23:19:37 字数 1521 浏览 0 评论 0原文

我有一个样本数据,其中包括来自大学、学校和宿舍的数据。 我想在 Elasticsearch 中使用这些数据。在进行过滤时,我按如下方式发送正文:

示例数据:

[
    {
        name: 'Maisha school of sience',
        school_type: 'public',
        location: 'A',
    },
    {
        name: 'Maisha elementry school',
        school_type: 'private',
        location: 'B',
    },
    {
        name: 'istanbul university',
        university_type: 'public',
        location: 'C',
    },
    {
        name: 'Maisha university',
        university_type: 'private',
        location: 'D',
    },
    {
        name: 'kbb center of Maisha',
        dormitory_type: 'public',
        location: 'E',
    },
    {
        name: 'high Maisha dorm',
        dormitory_type: 'private',
        location: 'F',
    },
]

我的查询:

"query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "*Maisha*",
                        "fields": [
                            "name"
                        ]
                    }
                },
                {
                    "match": {
                        "school_type": "public"
                    }
                }
            ]
        }
    }

结果:

{
   name: 'Maisha school of sience',
   school_type: 'public',
   location: 'A',
}

这个结果实际上是有道理的,因为它给出了准确的结果,但没有显示宿舍和大学数据,这是主要问题。换句话说,我只希望school_typepublic,但为什么它会过滤掉并且不显示大学和宿舍数据?

I have a sample data that includes data from univerities, schools and dormitories.
I want to use this data in Elasticsearch. I send the body as follows when making filteration:

Sample Data:

[
    {
        name: 'Maisha school of sience',
        school_type: 'public',
        location: 'A',
    },
    {
        name: 'Maisha elementry school',
        school_type: 'private',
        location: 'B',
    },
    {
        name: 'istanbul university',
        university_type: 'public',
        location: 'C',
    },
    {
        name: 'Maisha university',
        university_type: 'private',
        location: 'D',
    },
    {
        name: 'kbb center of Maisha',
        dormitory_type: 'public',
        location: 'E',
    },
    {
        name: 'high Maisha dorm',
        dormitory_type: 'private',
        location: 'F',
    },
]

My Query:

"query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "*Maisha*",
                        "fields": [
                            "name"
                        ]
                    }
                },
                {
                    "match": {
                        "school_type": "public"
                    }
                }
            ]
        }
    }

Result:

{
   name: 'Maisha school of sience',
   school_type: 'public',
   location: 'A',
}

This result actually makes sence because it gives the exact result but dormitory and university data are not shown which is the main problem. In other words, I only want the school_type to be public but why it filters out and not showing university and dormitory data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你丑哭了我 2025-01-17 23:19:37

您几乎是正确的,只需将 bool 中的 must 更改为 should 即可。我刚刚索引了您的示例数据并使用下面的查询。

示例文档

{
        "name": "Maisha school of sience",  // doc-1
        "school_type": "public",
        "location": "A"
}
{
        "name": "istanbul university", // doc-2
        "university_type": "public",
        "location": "C"
}
{
        "name": "Maisha university", // so on
        "university_type": "private",
        "location": "D"
}
{
        "name": "kbb center of Maisha",
        "dormitory_type": "public",
        "location": "E"
}
{
        "name": "high Maisha dorm",
        "dormitory_type": "private",
        "location": "F"
}
{
    "query": {
        "bool": {
            "should": [. ->> notice should clause here
                {
                    "query_string": {
                        "query": "*Maisha*",
                        "fields": [
                            "name"
                        ]
                    }
                },
                {
                    "match": {
                        "school_type": "public"
                    }
                }
            ]
        }
    }
}

它返回以下结果,希望您正在寻找相同的输出。

{
    "took": 12,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.287682,
        "hits": [
            {
                "_index": "71296703",
                "_id": "1",
                "_score": 1.287682,
                "_source": {
                    "name": "Maisha school of sience",
                    "school_type": "public",
                    "location": "A"
                }
            },
            {
                "_index": "71296703",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "name": "Maisha university",
                    "university_type": "private",
                    "location": "D"
                }
            },
            {
                "_index": "71296703",
                "_id": "4",
                "_score": 1.0,
                "_source": {
                    "name": "kbb center of Maisha",
                    "dormitory_type": "public",
                    "location": "E"
                }
            },
            {
                "_index": "71296703",
                "_id": "5",
                "_score": 1.0,
                "_source": {
                    "name": "high Maisha dorm",
                    "dormitory_type": "private",
                    "location": "F"
                }
            }
        ]
    }
}

You are almost correct, you just need to change must in bool to should. I just indexed your sample data and using below query.

Sample documents

{
        "name": "Maisha school of sience",  // doc-1
        "school_type": "public",
        "location": "A"
}
{
        "name": "istanbul university", // doc-2
        "university_type": "public",
        "location": "C"
}
{
        "name": "Maisha university", // so on
        "university_type": "private",
        "location": "D"
}
{
        "name": "kbb center of Maisha",
        "dormitory_type": "public",
        "location": "E"
}
{
        "name": "high Maisha dorm",
        "dormitory_type": "private",
        "location": "F"
}
{
    "query": {
        "bool": {
            "should": [. ->> notice should clause here
                {
                    "query_string": {
                        "query": "*Maisha*",
                        "fields": [
                            "name"
                        ]
                    }
                },
                {
                    "match": {
                        "school_type": "public"
                    }
                }
            ]
        }
    }
}

it returns below result, hope you are looking for the same output.

{
    "took": 12,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.287682,
        "hits": [
            {
                "_index": "71296703",
                "_id": "1",
                "_score": 1.287682,
                "_source": {
                    "name": "Maisha school of sience",
                    "school_type": "public",
                    "location": "A"
                }
            },
            {
                "_index": "71296703",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "name": "Maisha university",
                    "university_type": "private",
                    "location": "D"
                }
            },
            {
                "_index": "71296703",
                "_id": "4",
                "_score": 1.0,
                "_source": {
                    "name": "kbb center of Maisha",
                    "dormitory_type": "public",
                    "location": "E"
                }
            },
            {
                "_index": "71296703",
                "_id": "5",
                "_score": 1.0,
                "_source": {
                    "name": "high Maisha dorm",
                    "dormitory_type": "private",
                    "location": "F"
                }
            }
        ]
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文