• 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。

  • 相关阅读:
    django中使用redis保存session(转)
    Python操作Redis(转)
    Django 中 app_name (应用命名空间) 和 namespace (实例命名空间) 的区别
    零开始Android逆向教程(一)——初探Android逆向
    python基础网络编程--转
    安全建设之平台搭建
    从信息安全弃坑到虚拟化的悲哀和无奈之感
    Apache 性能配置优化
    反爬虫和抗DDOS攻击技术实践
    DockerCon2017前瞻
  • 原文地址:https://www.cnblogs.com/guozhe/p/5121616.html
Copyright © 2020-2023  润新知