【问题标题】:Elastic Search Wildcard query with space failing 7.11空间失败的弹性搜索通配符查询 7.11
【发布时间】:2021-03-17 11:27:52
【问题描述】:

我在 7.11 版的弹性搜索中索引了我的数据。这是我直接将文档添加到索引时得到的映射。

{"properties":{"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}

我没有添加关键字部分,但不知道它来自哪里。 我正在运行通配符查询。但无法获取带有空格的关键字的数据。

{
    
"query": {
    "bool":{
        "should":[
 {"wildcard": {"name":"*hello world*"}}
 
 ]
}
}

}

看过很多与 not_analyzed 相关的答案。而且我尝试在映射中更新 {"index":"true"} 但没有帮助。如何使通配符搜索在此版本的弹性搜索中工作

尝试添加通配符字段

PUT http://localhost:9001/indexname/_mapping
{
  "properties": {
    "name": {
    "type" :"wildcard"
  }
  }
}

得到以下回复

{
"error": {
    "root_cause": [
        {
            "type": "illegal_argument_exception",
            "reason": "mapper [name] cannot be changed from type [text] to [wildcard]"
        }
    ],
    "type": "illegal_argument_exception",
    "reason": "mapper [name] cannot be changed from type [text] to [wildcard]"
},
"status": 400

}

添加要匹配的示例文档

{
            "_index": "accelerators",
            "_type": "_doc",
            "_id": "602ec047a70f7f30bcf75dec",
            "_score": 1.0,
            "_source": {
                "acc_id": "602ec047a70f7f30bcf75dec",
                "name": "hello world example",
                "type": "Accelerator",
                "description": "khdkhfk ldsjl klsdkl",
                "teamMembers": [
                    {
                        "userId": "karthik.r@gmail.com",
                        "name": "Karthik Ganesh R",
                        "shortName": "KR",
                        "isOwner": true
                    },
                    {
                        "userId": "anand.sajan@gmail.com",
                        "name": "Anand Sajan",
                        "shortName": "AS",
                        "isOwner": false
                    }
                    
                ],
                "sectorObj": [
                    {
                        "item_id": 14,
                        "item_text": "Cross-sector"
                    }
                ],
                "geographyObj": [
                    {
                        "item_id": 4,
                        "item_text": "Global"
                    }
                ],
                "technologyObj": [
                    {
                        "item_id": 1,
                        "item_text": "Artificial Intelligence"
                    }
                ],
                "themeColor": 1,
                "mainImage": "assets/images/Graphics/Asset 35.svg",
                "features": [
                    {
                        "name": "Ideation",
                        "icon": "Asset 1007.svg"
                    },
                    {
                        "name": "Innovation",
                        "icon": "Asset 1044.svg"
                    },
                    {
                        "name": "Strategy",
                        "icon": "Asset 1129.svg"
                    },
                    {
                        "name": "Intuitive",
                        "icon": "Asset 964.svg"
                    },
                    
                    
                ],
                "logo": {
                    "actualFileName": "",
                    "fileExtension": "",
                    "fileName": "",
                    "fileSize": 0,
                    "fileUrl": ""
                },
                "customLogo": {
                    "logoColor": "#B9241C",
                    "logoText": "EC",
                    "logoTextColor": "#F6F6FA"
                },
                
                
                "collaborators": [
                    {
                        "userId": "muhammed.arif@gmail.com",
                        "name": "muhammed Arif P T",
                        "shortName": "MA"
                    },
                    {
                        "userId": "anand.sajan@gmail.com",
                        "name": "Anand Sajan",
                        "shortName": "AS"
                    }
                    
                ],
                "created_date": "2021-02-18T19:30:15.238000Z",
                "modified_date": "2021-03-11T11:45:49.583000Z"
            }
        }

【问题讨论】:

  • 这个答案可能会有所帮助:stackoverflow.com/a/66210784/4604579(提示:使用 wildcard 字段类型)
  • @Val 尝试使用通配符并得到错误。请查看更新的答案

标签: elasticsearch wildcard


【解决方案1】:

字段映射一经创建便无法修改。但是,您可以创建另一个 wildcard 类型的子字段,如下所示:

PUT http://localhost:9001/indexname/_mapping
{
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "wildcard": {
          "type" :"wildcard"
        },
        "keyword": {
          "type" :"keyword",
          "ignore_above":256
        }
      }
    }
  }
}

更新映射后,您需要重新索引数据,以便为新字段建立索引,如下所示:

POST http://localhost:9001/indexname/_update_by_query

然后,当这完成后,您将能够像这样查询这个新字段:

{
  "query": {
    "bool": {
      "should": [
        {
          "wildcard": {
            "name.wildcard": "*hello world*"
          }
        }
      ]
    }
  }
}

【讨论】:

  • { "type": "mapper_parsing_exception", "reason": "没有为字段 [name] 指定类型" }
  • { "error": { "root_cause": [ { "type": "mapper_parsing_exception", "reason": "没有为字段 [name] 指定类型" } ], "type": " mapper_parsing_exception", "reason": "没有为字段 [name] 指定类型" }, "status": 400 }
  • 这行得通,我更新了调用 POST API 的索引。但响应是空的,即使尝试使用 *hello
  • upfdte 工作但输出没有变化
  • 你能显示一个应该匹配的文档吗?
猜你喜欢
  • 2021-08-16
  • 2019-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多