zoukankan      html  css  js  c++  java
  • elasticsearch中的mapping映射配置与查询典型案例

    elasticsearch中的mapping映射配置与查询典型案例

    elasticsearch中的mapping映射配置示例
    比如要搭建个中文新闻信息的搜索引擎,新闻有"标题"、"内容"、"作者"、"类型"、"发布时间"这五个字段;
    我们要提供"标题和内容的检索"、"排序"、"高亮"、"统计"、"过滤"等一些基本功能。
    ES提供了smartcn的中文分词插件,测试的话建议使用IK分词插件。
    内容中properties对应mapping里的内容,里面5个字段。
    type指出字段类型、内容、标题字段要进行分词和高亮因此要设置分词器和开启term_vector。
    {
      "news": {
        "properties": {
          "content": {#内容
            "type": "string",  #字段类型
            "store": "no", #是否存储
            "term_vector": "with_positions_offsets",#开启向量,用于高亮
            "index_analyzer": "ik",#索引时分词器
            "search_analyzer": "ik"#搜索时分词器
          },
          "title": {
            "type": "string",
            "store": "no",
            "term_vector": "with_positions_offsets",
            "index_analyzer": "ik",
            "search_analyzer": "ik",
            "boost": 5
          },
          "author": {
            "type": "string",
            "index": "not_analyzed"#该字段不分词
          },
          "publish_date": {
            "type": "date",
            "format": "yyyy/MM/dd",
            "index": "not_analyzed"#该字段不分词
          },
          "category": {
            "type": "string",
            "index": "not_analyzed"#该字段不分词
          }
        }
      }
    }

    查询示例:内容包括几个部分:

    分页:from/size、字段:fields、排序sort、查询:query、过滤:filter、高亮:highlight、统计:facet
    {
      "from": 0,
      "size": 10,
      "fields": [
        "title",
        "content",
        "publish_date",
        "category",
        "author"
      ],
      "sort": [
        {
          "publish_date": {
            "order": "asc"
          }
        },
        "_score"
      ],
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "title": "中国"
              }
            },
            {
              "term": {
                "content": "中国"
              }
            }
          ]
        }
      },
      "filter": {
        "range": {
          "publish_date": {
            "from": "2010/07/01",
            "to": "2010/07/21",
            "include_lower": true,
            "include_upper": false
          }
        }
      },
      "highlight": {
        "pre_tags": [
          "<tag1>",
          "<tag2>"
        ],
        "post_tags": [
          "</tag1>",
          "</tag2>"
        ],
        "fields": {
          "title": {},
          "content": {}
        }
      },
      "facets": {
        "cate": {
          "terms": {
            "field": "category"
          }
        }
      }
    }
    结果包含需要的几个部分。
    值得注意的是,facet的统计是命中的结果进行统计,filter是对结果进行过滤,filter不会影响facet,如果要统计filter掉的的就要使用filter facet。

  • 相关阅读:
    HDU 5115 Dire Wolf (区间DP)
    HDU 4283 You Are the One(区间DP(最优出栈顺序))
    ZOJ 3469 Food Delivery(区间DP好题)
    LightOJ 1422 Halloween Costumes(区间DP)
    POJ 1651 Multiplication Puzzle(区间DP)
    NYOJ 石子合并(一)(区间DP)
    POJ 2955 Brackets(括号匹配一)
    POJ 1141 Brackets Sequence(括号匹配二)
    ZOJ 3537 Cake(凸包+区间DP)
    Graham求凸包模板
  • 原文地址:https://www.cnblogs.com/guozhe/p/5121616.html
Copyright © 2011-2022 走看看