【问题标题】:Nested Term ElasticSearch 7 problem / Elastica Don't work - BEGINNER嵌套术语 ElasticSearch 7 问题/Elastica 不起作用 - 初学者
【发布时间】:2021-07-05 21:12:20
【问题描述】:

我正在尝试在 ElasticSearch 的嵌套元素中使用 slug 进行精确搜索,但它似乎不起作用。

因此,当我尝试使用 "my-slug" 进行简单的嵌套 ma​​tch 时,我会得到 "my" 的结果>“蛞蝓”,普通...

GET my-index/_search
  
  {
      "query": {
        "nested": {
          "path": "productTranslation",
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "productTranslation.slug": "my-slug"
                  }
                }
              ]
            }
          }
        }
      }
    }

但是当我尝试使用术语或过滤器搜索时没有结果。

GET my-index/_search
{
  "query": {
    "nested": {
      "path": "productTranslation",
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "productTranslation.slug": "my-slug"
              }
            }
          ]
        }
      }
    }
  }
}

知道错误在哪里吗?

感谢您的帮助。

【问题讨论】:

    标签: elasticsearch elasticsearch-dsl elastica


    【解决方案1】:

    词条查询不对词条进行任何分析。因此,在term query 中,您需要完全匹配。

    如果您没有明确定义任何映射,则需要将.keyword 添加到productTranslation.slug 字段。这使用关键字分析器而不是标准分析器(注意productTranslation.slug 字段后面的“.keyword”)。

    {
      "query": {
        "nested": {
          "path": "productTranslation",
          "query": {
            "bool": {
              "filter": [
                {
                  "term": {
                    "productTranslation.slug.keyword": "my-slug"  // note this
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    或者您可以将productTranslation.slug字段的数据类型更改为关键字类型

    {
      "mappings": {
        "properties": {
          "productTranslation": {
            "properties": {
              "slug": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • @Titeufggg 请仔细阅读答案,如果这能解决您的问题,请告诉我?
    • 感谢您的帮助。我在映射中添加了 "type": "keyword" 并且它有效!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 2021-02-19
    • 2018-11-09
    • 2020-07-19
    相关资源
    最近更新 更多