gpt4 book ai didi

elasticsearch - Elasticsearch部分查询

转载 作者:行者123 更新时间:2023-12-02 22:54:04 24 4
gpt4 key购买 nike

我正在使用Elasticsearch v 7.3.1并尝试实现部分搜索。所有搜索都进行得很好,但是当我查询“ John Oxford Oxford ”时,“John”与文档匹配,但是整个文档中没有“ Oxford ”。但仍显示给我文档而不显示空结果。

我该怎么做,以便在我们查询 John Oxford 时不返回文档?

我的映射,设置,示例文档和学生数据查询如下。

映射

PUT student
{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
}, "mappings" : {
"properties" : {
"DOB" : {
"type" : "text"
},
"email" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"first_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"home_phone" : {
"type" : "text"
},
"last_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"student_id" : {
"type" : "text"
}
}
}
}


示例文档
POST student/_doc
{
"DOB": "1983-12-04",
"email": "johndoe@gmail.fr",
"first_name": "john",
"home_phone": 1242432,
"last_name": "doe",
"student_id": 28

}

查询
GET student/_search
{
"query": {
"multi_match": {
"query": "john oxford",
"type": "bool_prefix",
"analyzer": "standard",
"fields": [
"first_name",
"last_name",
"email",
"DOB",
"home_phone",
"student_id"
]
}
}
}

以下是我想要的结果
  • 1242-部分匹配home_phone
  • joh do-与“John”和“Doe”的部分匹配
  • 1983-12-04-与DOB
  • 匹配
  • johndoe-电子邮件
  • 上的部分匹配
  • doe-匹配姓氏
  • 最佳答案

    要实现部分搜索,您应该在所需的文本字段中添加特定的autocomplete analyzer并实现特定的search_analyzer,因为您使用的是edgengram过滤器-请阅读herehere进行解释。与您在查询期间指定分析器相比,这样做更舒服。尝试:

    PUT student
    {
    "settings": {
    "analysis": {
    "filter": {
    "autocomplete_filter": {
    "type": "edge_ngram",
    "min_gram": 1,
    "max_gram": 20
    }
    },
    "analyzer": {
    "autocomplete": {
    "type": "custom",
    "tokenizer": "standard",
    "filter": [
    "lowercase",
    "autocomplete_filter"
    ]
    }
    }
    }
    }, "mappings" : {
    "properties" : {
    "DOB" : {
    "type" : "text",
    "analyzer": "autocomplete",
    "search_analyzer": "standard"
    },
    "email" : {
    "type" : "text",
    "analyzer": "autocomplete",
    "search_analyzer": "standard",
    "fields" : {
    "keyword" : {
    "type" : "keyword",
    "ignore_above" : 256
    }
    }
    },
    "first_name" : {
    "type" : "text",
    "analyzer": "autocomplete",
    "search_analyzer": "standard",
    "fields" : {
    "keyword" : {
    "type" : "keyword",
    "ignore_above" : 256
    }
    }
    },
    "home_phone" : {
    "type" : "text",
    "analyzer": "autocomplete",
    "search_analyzer": "standard"
    },
    "last_name" : {
    "type" : "text",
    "analyzer": "autocomplete",
    "search_analyzer": "standard",
    "fields" : {
    "keyword" : {
    "type" : "keyword",
    "ignore_above" : 256
    }
    }
    },
    "student_id" : {
    "type" : "text"
    }
    }
    }
    }

    然后,当您查询两个词的自动完成时,应将它们与 and运算符连接起来。对于您的用例, cross-field类型应该是最好的:
    GET student/_search
    {
    "query": {
    "multi_match" : {
    "query": "John Oxford",
    "type": "cross_fields",
    "fields": [
    "first_name",
    "last_name",
    "email",
    "DOB",
    "home_phone",
    "student_id"
    ],
    "operator": "and"
    }
    }
    }

    关于elasticsearch - Elasticsearch部分查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61245239/

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