起飞就起飞

Elasticsearch的嵌套结构

Posted on By baixiao

嵌套结构

针对嵌套结构的基础知识,可以参考文章嵌套对象

文章Elasticsearch Nested类型深入详解对其场景有较好描述:

问题

某个elasticsearch的索引有如下mapping:

"Types": {
  "type": "nested",
  "properties": {
    "FirstTypeName": {
      "type": "text",
      "fields": {
        "Raw": {
          "type": "keyword",
          "ignore_above": 256
        }
      },
      "analyzer": "ik_smart"
    },
    "Tags": {
      "type": "text",
      "fields": {
        "Raw": {
          "type": "keyword",
          "ignore_above": 256
        }
      },
      "analyzer": "ik_smart"
    }
  }
}

描述了商品和(一级分类加二级分类)的一对多关系,两点值得说明:

  • nested结构描述一对多关系
  • 特别定义了Raw属性,用于精准匹配

需要精准搜索时

在业务上需要精准匹配一级分类的时候,需要term查询并加强Raw属性:

{
  "query": {
    "bool": {
      "must": {
        "nested": {
          "path": "Types",
          "query": {
            "bool": {
              "must": {
                "term": {
                  "Types.FirstTypeName.Raw": "YJT的数码智能"
                }
              }
            }
          }
        }
      }
    }
  }
}

需要全文搜索时

在用户搜索场景,需要分类提供一定的score时,则只需要match查询:

{
  "query": {
    "bool": {
      "should": {
        "multi_match": {
          "fields": [
            "Types.FirstTypeName^4",
            "Types.Tags^4",
            "Brand^30",
            "Labels^2"
          ],
          "query": "华为",
          "tie_breaker": 0.1,
          "type": "best_fields"
        }
      }
    }
  }
}	

需要判断非空时

某些场景必须要求nested结构非空时,使用exists查询:

{
  "query": {
    "bool": {
      "must": {
        "nested": {
          "path": "Types",
          "query": {
            "bool": {
              "must": {
                "exists": {
                  "field": "Types.FirstTypeName"
                }
              }
            }
          }
        }
      }
    }
  }
}