Elasticsearch --- 4. term与match ,修改器,建议器

Posted sc-1067178406

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch --- 4. term与match ,修改器,建议器相关的知识,希望对你有一定的参考价值。

一.term与match 

  1.区别

term查询查找包含文档精确的倒排索引指定的词条。也就是精确查找(没经过分词)。
term和match的区别是:

  match是经过analyer的,也就是说,文档首先被分析器给处理了。根据不同的分析器,分析的结果也稍显不同,然后再根据分词结果进行匹配。
  term则不经过分词,它是直接去倒排索引中查找了精确的值了。
建立数据结构

PUT w1 {
"mappings": { "doc": { "properties":{ "t1":{ "type": "text" }, "t2": { "type": "keyword" } } } } } PUT w1/doc/1 { "t1": "hi single dog", "t2": "hi single dog" }

 

GET w1/doc/_search
{
  "query": {
    "term": {
      "t1": "hi" 
    }
  }
}

#能查到

GET w1
/doc/_search { "query": { "term": { "t2": "hi" } } }

#查不到

GET w1
/doc/_search { "query": { "match": { "t2": "hi single dog" } } }
#只能这样查询keyword的值
GET w1
/doc/_search { "query": { "match": { "t1": "hi" } } }

#t1 进行了分词,可以查到

 

  2.查找多个精确值

①原样式


PUT w1/doc/2 { "t1": "20", "t2": "2019-4-16" } PUT w1/doc/3 { "t1": "30", "t2": "2019-4-17" } GET w1/doc/_search { "query": { "bool": { "should": [ { "term": { "t1": "20" } }, { "term": { "t1": "30" } } ] } } }

 

②简单样式

GET w1/doc/_search { "query": { "terms": { "t1": ["20", "30"] } } } GET w1/doc/_search { "query": { "terms": { "t2": ["2019-4-16", "2019-4-17"] } } }

 

二.修改器

  1.词条建议器 

  两种顺序都可以得到结果

GET c12/doc/_search
{
  "suggest": {
    "text": "appl",    #要修改词
    "my1": {            #别名
      
      "term": {
        "field": "title"     #对照字段
      }
    }
  }
}

    

GET c12/doc/_search
{
   
  "suggest": {
    "my1": {          #别名
      "text": "appl",    
      "term": {
        "field": "title"
      }
    }
    
  }
}

 

  2.词组建议器

GET s1/doc/_search
{
  "suggest": {
    "my_s1": {
      "text": "luce is coo",
      "phrase": {
        "field": "title"
      }
    }
  }
}

 

加高亮显示

GET s4/doc/_search { "suggest": { "my_s4": { "text": "lucen elasticsearc rock", "phrase": { "field": "title", "highlight":{ "pre_tag":"<em class=‘xxx‘>", "post_tag":"</em>" } } } } }

 

 

 

三.建议器(前缀提示)

 

  1.基本使用

GET s8/doc/_search
{
  "suggest": {
    "s3": {
      "text": "bl",
      "completion": {
        "field": "title"
      }
    }
  }
}

 

 

修改器和建议器一起用


GET c12/doc/_search { "suggest": { "text": "appl", "s1": { "term": { "field": "title" } }, "s2": { "phrase": { "field": "title" } }, "s3": { "completion": { "field": "title" } } } }

 

  2.权重

建立数据结构

PUT s8 {
"mappings": { "doc":{ "properties":{ "title":{ "type": "completion" } } } } }

 

插入数据

PUT s8/doc/3 { "title": [ { "input":"appel", "weight": 2 }, { "input":"apple", "weight": 3 } ] }

 

查询

GET s8/doc/_search { "suggest": { "my_s8": { "text": "app", "completion": { "field": "title" } } } }

  3. size参数(返回数据量,默认是5)

GET s8/doc/_search
{
  "suggest": {
    "completion_suggest": {
      "prefix": "app",
      "completion": {
        "field": "title",
        "size": 1
      }
    }
  },
  "_source": "title"

 

  4.skip_duplicates(过滤重复)

GET s8/doc/_search
{
  "suggest": {
    "completion_suggest": {
      "prefix": "app",
      "completion": {
        "field": "title",
        "size": 5,
        "skip_duplicates":true
      }
    }
  },
  "_source": "title"
}

 

 

 

四.解决 size 最大10000的问题

进行修改

PUT s18/_settings { "index":{ "max_result_window": "11000" } }

 

这样就可以返回10000以上的数据量了

GET s18/doc/_search { "size": 11000, "query": { "match_all": {} } }

 

以上是关于Elasticsearch --- 4. term与match ,修改器,建议器的主要内容,如果未能解决你的问题,请参考以下文章

ElasticSearch stack 6.5.4下载地址

elasticsearch2.4升级elasticsearch6.2心得

Elasticsearch6.4.3安装

搜索引擎ElasticSearch系列: ElasticSearch2.4.4 sql插件安装

elasticsearch5.4.1中怎样创建客户端连接

ElasticSearch 6.2.4 实践