arrays - 在elasticsearch中搜索字符串数组中的确切字段
问题描述
弹性搜索版本:7.1.1
嗨,我尝试了很多,但在我的索引中找不到任何解决方案,我有一个包含字符串的字段。
因此,例如,我有两个文档在位置数组中包含不同的值。
文件 1:
"doc" : {
"locations" : [
"Cloppenburg",
"Berlin"
]
}
文件 2:
"doc" : {
"locations" : [
"Landkreis Cloppenburg",
"Berlin"
]
}
用户请求搜索术语Cloppenburg 并且我只想返回那些包含术语Cloppenburg 而不是Landkreis Cloppenburg的文档。结果应仅包含Document-1。但我的查询返回两个文件。
我正在使用以下查询并取回两个文档。有人可以帮我解决这个问题。
GET /my_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"doc.locations": {
"query": "cloppenburg",
"operator": "and"
}
}
}
]
}
}
}
解决方案
问题是由于您正在使用该text
字段和match
查询。
匹配查询被分析并使用在索引时使用的相同搜索词分析器,这是字段情况下的标准分析器。text
在您的情况下,它会在空格上打断文本,这Landkreis Cloppenburg
将创建两个标记landkreis
以及cloppenburg
索引和搜索时间,甚至cloppenburg
会匹配文档。
解决方案:使用keyword field
.
索引定义
{
"mappings": {
"properties": {
"location": {
"type": "keyword"
}
}
}
}
索引您的两个文档,然后使用相同的搜索查询
{
"query": {
"bool": {
"must": [
{
"match": {
"location": {
"query": "Cloppenburg"
}
}
}
]
}
}
}
结果
"hits": [
{
"_index": "location",
"_type": "_doc",
"_id": "2",
"_score": 0.6931471,
"_source": {
"location": "Cloppenburg"
}
}
]
推荐阅读
- javascript - Ember JS 组件 - 无法读取未定义的属性“currentHandlerInfos”
- python - 将通配符掩码转换为网络掩码的 Python 程序
- angular - 不使用服务人员更新应用程序
- c++ - 有没有像 lower_bound 这样返回最后一个值而不是第一个值的函数?
- javascript - ES6 返回数组值的数组
- windows - windows在关闭tomcat后保持端口锁定
- java - 分布式设置的 Tibco EMS 服务器容错不适用于 java 应用程序
- c# - 根据sql的结果生成多个pdf
- python - 双索引中的Python列表如何拆分它 ety.origin 列表
- javascript - jQuery / Javascript 循环增量值不会通过 .height 比较重置(无限滚动脚本的一部分)