gpt4 book ai didi

java - Elasticsearch 索引包含并以搜索开头

转载 作者:行者123 更新时间:2023-12-04 13:31:04 26 4
gpt4 key购买 nike

我们正在使用 Elasticsearch 来更快地搜索我们的组织数据。数据模型具有组织 ID、地址、组织名称、业务开始日期和组织联系人数组。
我们已要求执行包含搜索的字符串,并且字符串以搜索组织 ID 和/或组织名称字段开头
例如,
organization.name:”abc*” or organization.id:”abc

organization.name:”abc*” and organization.id:”*abc*”
organization.name:”*abc*” and organization.id:”abc*”
由于我们需要使用 Ngram 分析器在同一字段上同时使用两者不起作用
请指教

最佳答案

据我所知,你需要找到那些文件,其中organization.nameabc 开头和 organization.id包含 abc (不是一开始)。
为此,您可以使用 multi-field ,这对于出于不同目的以不同方式索引同一字段以及 n-gram tokenizer 很有用
添加包含索引数据、映射、搜索查询和搜索结果的工作示例
索引映射:

    {
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "ngram",
"min_gram": 3,
"max_gram": 20,
"token_chars": [
"letter",
"digit"
]
}
}
},
"max_ngram_diff": 20
},
"mappings": {
"properties": {
"organization": {
"properties": {
"name": {
"type": "keyword",
"fields": {
"raw": {
"type": "text",
"analyzer": "my_analyzer"
}
}
},
"id": {
"type": "keyword",
"fields": {
"raw": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
}
}
}
}
索引数据:
{
"organization": {
"id": "abc def",
"name": "Aspect abc Technology"
}
}
{
"organization": {
"id": "defabc",
"name": "abc Aspect Technology"
}
}
{
"organization": {
"id": "abcef",
"name": "abc Aspect Technology"
}
}
{
"organization": {
"id": "abc",
"name": "Aspect Technology"
}
}
查询查询:
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"organization.id.raw": "abc"
}
},
{
"prefix": {
"organization.name": "abc"
}
}
],
"must_not": {
"prefix": {
"organization.id": "abc"
}
}
}
},
{
"bool": {
"must": [
{
"prefix": {
"organization.id": "abc"
}
},
{
"match": {
"organization.name.raw": "abc"
}
}
],
"must_not": {
"prefix": {
"organization.name": "abc"
}
}
}
}
],
"minimum_should_match": 1
}
}
}
搜索结果:
"hits": [
{
"_index": "65054994",
"_type": "_doc",
"_id": "1",
"_score": 1.3590312,
"_source": {
"organization": {
"id": "abc def",
"name": "Aspect abc Technology"
}
}
},
{
"_index": "65054994",
"_type": "_doc",
"_id": "2",
"_score": 1.0725547,
"_source": {
"organization": {
"id": "defabc",
"name": "abc Aspect Technology"
}
}
}
]

关于java - Elasticsearch 索引包含并以搜索开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65054994/

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