elasticsearch - Elasticsearch匹配类型数组字段中的确切单词

标签 elasticsearch

我已经咨询了几篇文章和stackoverflow问题,但是恐怕我找不到像我的场景那样的东西,如果重复出现,请原谅我。
问题:
我想将单词“Tennis”匹配到具有运动["Football", "Tennis", "Table Tennis", "Basketball"]数组的文件。现在,单词应该完全匹配。
映射:

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

DOC:
// DOC1
{
   "clubname": "Arena 51",
   "sports: ["Cricket","Football", "Tennis"]
}
// DOC2
{
   "clubname": "Play You",
   "sports: ["Cricket","Football", "Table Tennis"]
}
查询:
"query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": [                
                {
                    "match": {
                        "sports": "tennis"
                    }
                }
            ]
        }
    }
通过此查询,我得到了两个文档,我认为这是正确的行为。所以当我只搜索Doc1时如何进行 flex 搜索以仅返回Tennis

最佳答案

如果您搜索Tennis,则@Opster给出的答案可以很好地工作,但是如果您希望进行不区分大小写的搜索,则需要通过以下方式为索引创建自定义规范化:
索引映射:

{
  "settings": {
    "analysis": {
      "normalizer": {
        "lowercase_normalizer": {
          "type": "custom",
          "char_filter": [],
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "sports": {
        "type": "keyword",
        "normalizer": "lowercase_normalizer"
      }
    }
  }
}
搜索查询:
{
    "query": {
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": [                
                {
                    "match": {
                        "sports": "tennis"
                    }
                }
            ]
        }
    }
}
搜索结果:
"hits": [
      {
        "_index": "fd_cb2",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "clubname": "Arena 51",
          "sports": [
            "Cricket",
            "Football",
            "Tennis"
          ]
        }
      }
    ]

关于elasticsearch - Elasticsearch匹配类型数组字段中的确切单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63512123/

相关文章:

java - Elasticsearch 2+ JAVA API 或过滤器

elasticsearch - 超过阈值后的电子邮件警报,logstash?

elasticsearch - 如何使用 ElasticSearch 在字符串字段中搜索精确短语?

elasticsearch - 删除/var/lib/elasticsearch后,Elasticsearch服务失败

elasticsearch - 使用Elasticsearch的Logstash:我正在使用Logstash将数据提取到ES索引中。但是现在我希望logstash运行24/7

elasticsearch - 运行测试时服务 elasticsearch 不可见

elasticsearch - elasticsearch/轮胎:如何定义必须始终匹配的基本搜索条件?

curl - elasticsearch REST API转义序列

elasticsearch - 将二进制类型与 NEST (elasticsearch) 一起使用

elasticsearch - 如何使用elasticsearch-ruby gem在Elasticsearch中创建摄取管道