我有以下映射
"mappings":{
"properties":{
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"customProps":{
"type" : "nested",
"properties": {
"key":{
"type": "keyword"
},
"value": {
"type" : "keyword"
}
}
}
}
}
示例資料
{
"name" : "person1",
"age" : 10,
"customProps":[
{"hairColor":"blue"},
{"height":"120"}
]
},
{
"name" : "person2",
"age" : 30,
"customProps":[
{"jobTitle" : "software engineer"},
{"salaryAccount" : "AvGhj90AAb"}
]
}
所以我希望能夠按工資帳戶不區分大小寫來搜索檔案,我也在使用通配符示例查詢進行搜索
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "customProps",
"query": {
"bool": {
"must": [
{ "match": { "customProps.key": "salaryAccount" } },
{ "wildcard": { "customProps.value": "*AvG*"
}
}
]}}}}]}}}
我嘗試使用以下語法添加帶有 PUT 的分析器
{
"settings":{
"index":{
"analysis":{
"analyzer":{
"analyzer_case_insensitive" : {
"tokenizer":"keyword",
"filter":"lowercase"
}
}
}
}
},
"mappings":{
"people":{
"properties":{
"customProps":{
"properties":{
"value":{
"type": "keyword",
"analyzer": "analyzer_case_insensitive"
}
}
}
}
}
}
}
我收到以下錯誤
"type" : "mapper_parsing_exception", "reason" : "根映射定義有不受支持的引數:[people: {properties={customProps={properties={value={analyzer=analyzer_case_insensitive, type=keyword}}}}}]"
知道如何在陣列中存在工資帳戶物件時對其進行分析嗎?
uj5u.com熱心網友回復:
您的用例非常清楚,您只想在陣列中salaryAccount存在此鍵時才搜索值。customProps
您的映射定義存在一些問題:
- 您不能為
keyword型別欄位定義自定義分析器,而是可以使用規范器 - 根據您在問題開頭添加的映射定義,您似乎使用的是 elasticsearch 7.x 版。但是您提供的第二個映射定義,因為您還添加了映射型別(即
people),這在 7.x 中已棄用 - 不需要在索引映射中添加
keyand欄位。value
添加具有索引映射、搜索查詢和搜索結果的作業示例
索引映射:
PUT myidx
{
"mappings": {
"properties": {
"customProps": {
"type": "nested"
}
}
}
}
搜索查詢:
您需要使用存在查詢來檢查欄位是否存在。并且通配符查詢中的case_insensitive引數自 elasticsearch 版本 7.10 起可用。如果您使用的是低于此的版本,則需要使用normalizer來實作不區分大小寫的場景。
POST myidx/_search
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "customProps",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "customProps.salaryAccount"
}
},
{
"wildcard": {
"customProps.salaryAccount.keyword": {
"value": "*aVg*",
"case_insensitive": true
}
}
}
]
}
}
}
}
]
}
}
}
搜索結果:
"hits" : [
{
"_index" : "myidx",
"_type" : "_doc",
"_id" : "2",
"_score" : 2.0,
"_source" : {
"name" : "person2",
"age" : 30,
"customProps" : [
{
"jobTitle" : "software engineer"
},
{
"salaryAccount" : "AvGhj90AAb"
}
]
}
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/414398.html
標籤:
