MyElasticsearch
  • Introduction
  • 基本查询
  • 简介
  • 安装
    • Window下安装
  • 基础知识
    • 理解 document
    • 简单的集群管理
    • 简单实例:简单的curd操作
    • 简单实例:批量curd操作
    • 简单实例:多种搜索方式
    • 简单实例:聚合分析
    • 附录: _index,_type,_id,_source元数据
    • 附录:手动&自动生成document id
    • 附录:全量替换、强制创建、lazy delete机制
    • 附录:search timeout机制
    • _source && _all
  • 倒排索引
  • 查询附录
    • 分页搜索
    • multi-index&multi-type搜索模式
  • 查询
    • 测试数据
    • 简单查询
    • 基本查询
      • Term,Terms,Wildcard查询
        • Term查询
        • Terms查询
      • match相关查询
      • query_string查询
      • prefix前缀查询
      • fuzzy相关查询
        • fuzzy_like_this查询
        • fuzzy_like_this_field查询
        • fuzzy查询
    • 复合查询
  • groovy脚本
    • 执行部分更新(partial update)
  • 锁机制(悲观锁、乐观锁)
    • 基于_version乐观锁并发控制
    • 基于external version乐观锁并发控制
  • 查询方式
    • Query string方式
    • Query DSL 方式
    • query filter 方式
    • 各种query搜索语法
    • 多搜索条件组合查询
    • 检验不合法的Quqery查询
    • 搜索结果的排序规则
    • field索引两次来解决字符串排序
    • 使用scoll滚动搜索
    • 分词器
  • document mapping
    • 自动mapping带来的问题
    • field类型
    • mapping中的field type类型
    • 定制化dynamic mapping策略
  • 资料
  • 原理
    • 相关度评分TF&IDF算法
    • doc values 正排索引
  • 索引的CURD
  • 附录:基于scoll+bulk+索引别名实现零停机重建索引
Powered by GitBook
On this page

Was this helpful?

  1. 查询方式

field索引两次来解决字符串排序

如果对一个string field进行排序,结果往往不准确,因为比如"test my elasticsearch "分词后是多个单词,再排序就不是我们想要的结果了 ,有可能的出来的就是分词后某一个单词的评分高,导致排在前面。而我们是想"test my elasticsearch "整个字符串的搜索结果排在最前面。

通常解决方案是,将一个string field建立两次索引

例子:

PUT /website 
{
  "mappings": {
    "article": {
      "properties": {
        "title": {
          "type": "text",
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          },
          "fielddata": true
        },
        "content": {
          "type": "text"
        },
        "post_date": {
          "type": "date"
        },
        "author_id": {
          "type": "long"
        }
      }
    }
  }
}

另外再建立一个raw索引字段的index的参数是not_analyzed,不分词

fielddata的true正排索引才可以进行排序

 "title": {
          "type": "text",
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          },
          "fielddata": true
        }

插入数据

PUT /website/article/1
{
  "title": "first article",
  "content": "this is my first article",
  "post_date": "2017-01-01",
  "author_id": 110
}

PUT /website/article/2
{
  "title": "second article",
  "content": "this is my second article",
  "post_date": "2017-02-01",
  "author_id": 110
}

PUT /website/article/3
{
  "title": "third article",
  "content": "this is my third article",
  "post_date": "2017-03-01",
  "author_id": 110
}
GET /website1/article/_search
{
  "query": {
    "match_all": {}
  }
}

对比

GET /website/article/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "title.raw": {
        "order": "desc"
      }
    }
  ]
}
Previous搜索结果的排序规则Next使用scoll滚动搜索

Last updated 6 years ago

Was this helpful?