elasticsearch的嵌套结构

栏目: 后端 · 发布时间: 6年前

内容简介:针对嵌套结构的基础知识,可以参考文章文章

嵌套结构

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

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

elasticsearch的嵌套结构

问题

某个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 查询:

{
  "from": 0,
  "query": {
    "function_score": {
      "boost_mode": "multiply",
      "field_value_factor": {
        "factor": 2,
        "field": "RecentOrderCnt",
        "modifier": "log2p"
      },
      "max_boost": 4,
      "query": {
        "bool": {
          "must": {
            "match": {
              "GoodsName": {
                "operator": "and",
                "query": "华为"
              }
            }
          },
          "should": {
            "multi_match": {
              "fields": [
                "Types.FirstTypeName^4",
                "Types.Tags^4",
                "Brand^30",
                "Labels^2"
              ],
              "query": "华为",
              "tie_breaker": 0.1,
              "type": "best_fields"
            }
          }
        }
      },
      "score_mode": "sum"
    }
  },
  "size": 2,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ]
}

需要判断非空时

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

111


以上所述就是小编给大家介绍的《elasticsearch的嵌套结构》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms of the Intelligent Web

Algorithms of the Intelligent Web

Haralambos Marmanis、Dmitry Babenko / Manning Publications / 2009-7-8 / GBP 28.99

Web 2.0 applications provide a rich user experience, but the parts you can't see are just as important-and impressive. They use powerful techniques to process information intelligently and offer featu......一起来看看 《Algorithms of the Intelligent Web》 这本书的介绍吧!

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具