嵌套ElasticSearch-查询带空格的词

vaqhlq81  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(457)

对于包含空格的文本,我很难在elastic for displayname属性中实现自动完成,下面是字段的设置:

"DisplayName": {
    "type": "text",
    "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256,
            "normalizer": "caseinsensitive"
        },
        "prefix": {
            "type": "text",
            "analyzer": "startswith"
        }
    }
},
"id": {
    "type": "keyword"
}

这是你的名字 startwith 分析仪定义:

"analysis": {
    "analyzer": {
        "startswith": {
            "char_filter": [
                "html_strip"
            ],
            "filter": [
                "lowercase"
            ],
            "tokenizer": "keyword",
            "type": "custom"
        }
    },
    "normalizer": {
        "caseinsensitive": {
            "filter": [
                "lowercase",
                "asciifolding"
            ],
            "type": "custom"
        }
    }
},
"creation_date": "1565034410554",
"mapping": {
    "total_fields": {
        "limit": "5000"
    }
},
"number_of_shards": "5",
"provided_name": "streetsmart"

在我的查询生成器中,以下是尝试获取结果的查询:

_type:User AND (DisplayName.prefix:Joseph adam* OR UserPrincipalName.prefix:Joseph adam*)"

我得到的结果是所有包含 Adam 结果应该是什么 Joseph Adam Jr ,有人知道我该怎么做吗?

gdrx4gfi

gdrx4gfi1#

我不了解c#和.net语法,但添加了一个使用json格式的索引数据、搜索查询和搜索结果的工作示例。
也可以使用匹配短语前缀查询:
返回包含所提供文本的单词的文档,顺序与所提供的相同。所提供文本的最后一个术语被视为前缀,匹配以该术语开头的任何单词。
索引数据:

{
    "name": "Adam"
}
{
    "name": "Joseph Adam Sr"
}
{
    "name": "Joseph Adam Jr"
}

搜索查询:

{
    "query": {
        "multi_match": {
            "query": "Joseph Adam",
            "fields": [
                "name"
            ],
            "type": "phrase_prefix"
        }
    }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64163994",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.54037446,
        "_source": {
          "name": "Joseph Adam Jr"
        }
      },
      {
        "_index": "stof_64163994",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.54037446,
        "_source": {
          "name": "Joseph Adam Sr"
        }
      }
    ]

相关问题