gpt4 book ai didi

json - 进行JSON查询以检索ElasticSearch中最相关的结果

转载 作者:行者123 更新时间:2023-12-02 22:20:53 25 4
gpt4 key购买 nike

我有这个映射

{
"users":
{
"_all" : {"enabled" : false},
"properties":
{
"user":
{
"type":"multi_field",
"fields":
{
"user":{"type":"string"},
"original":{"type":"string","index":"not_analyzed"}
}
}
"school":{"type":"string","index": "not_analyzed"},
"college":{"type":"string","index":"not_analyzed"},
"uni":{"type":"string","index":"not_analyzed"}
}
}
}

有了这样的数据
{
"user": "Abdul Jabbar",
"school": "Aisha Bawany Academy",
"college": "Pakistan Air Force",
"uni": "Aptech Computer Education"
}

{
"user": "Abdul Jabbar WebBestow",
"school": "Sadaya Academy",
"college": "Pakistan Air Force",
"uni": "WebBestow"
}

{
"user":"Abdul Jabbar Leopard",
"school":"Innocent Public School",
"college":"Lucene College of Science",
"uni":"IBM"
}

{
"user":"Abdul Jabbar Loharwada",
"school":"Aisha Bawany Academy",
"college":"Behria College",
"uni":"Karachi University"
}

{
"user":"Abdul Raheem Loharwada",
"school":"Aisha Bawany Academy",
"college":"Indus College",
"uni":"Preston University"
}

我想让所有用户使用包含单词“Jabb”的用户,并且在所有学校,学院或uni Realm 中,在那些结果中都带有“Aisha Bawany Academy”或“Pakistan Air Force”或“Aptech Computer Education”的用户,以及那些包含单词“Jabb”但在其学校,学院或uni Realm 中没有任何相关数据的文档,应放在顶部结果下方。

即使用户的全名是“Abdul Jabbar *”,依此类推,但是当我们仅键入“Jabb”时,任何带有单词“Jabb”的用户都应携带上面的文档。

如何进行这样的JSON查询?请帮助我们。

最佳答案

使用新的映射和数据,以下查询将为我返回结果。

curl -XPOST "http://localhost:9200/test_index/_search" -d'
{
"query": {
"bool": {
"must": [
{
"prefix": {
"user": "jabb"
}
}
],
"should": [
{
"match": {
"school": {
"query": "Aisha Bawany Academy"
}
}
},
{
"match": {
"college": {
"query": "Aisha Bawany Academy"
}
}
},
{
"match": {
"uni": {
"query": "Aisha Bawany Academy"
}
}
}
]
}
}
}'

问题可能是您将“学院”更改为“学院”,依此类推,因此查询也必须更新。通常,ES区分大小写。

还要注意,在我的示例中,查询“jabb”是小写的。这是必需的,因为前缀查询不会对查询文本进行任何分析,并且已使用标准分析器(因为在映射中未指定分析器)对“用户”字段进行了分析,从而将标记修改为小写。即使已索引的某些文档具有文本“Jabbar”,它也会被标记为“jabbar”,因此前缀查询“Jabb”与任何标记都不匹配。

您可以在此处阅读有关前缀查询的信息: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html

此处是标准分析仪: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-standard-analyzer.html

另外,由于您希望将多个值匹配到多个字段,因此以下查询结构可能会更好地工作:
curl -XPOST "http://localhost:9200/test_index/_search" -d'
{
"query": {
"bool": {
"must": [
{
"prefix": {
"user": "jabb"
}
}
],
"should": [
{
"multi_match": {
"query": "Aisha Bawany Academy",
"fields": ["school", "college", "uni"]
}
},
{
"multi_match": {
"query": "Pakistan Air Force",
"fields": ["school", "college", "uni"]
}
},
{
"multi_match": {
"query": "Aptech Computer Education",
"fields": ["school", "college", "uni"]
}
}
]
}
}
}'

关于json - 进行JSON查询以检索ElasticSearch中最相关的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20818087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com