ElasticSearch学习(十四) --高级查询

转载自:https://blog.csdn.net/chengyuqiang/column/info/18392,ES版本号6.3.0
转载自:https://blog.csdn.net/qq_23536449/article/details/91365617

创建索引

PUT website
{
  "settings": {
    "number_of_replicas": 1,
    "number_of_shards": 5
  },
  "mappings": {
    "blog":{
      "properties": {
        "title":{
          "type": "text",
          "analyzer": "ik_max_word"
        },
        "author":{
          "type": "text"
        },
        "postdate":{
          "type": "date",
          "format": "yyyy-MM-dd"
        },
        "abstract":{
          "type": "text",
          "analyzer": "ik_max_word"
        },
        "url":{
          "type": "text"
        }
      }
    }
  }
}

准备数据

POST /_bulk
{ "index":{ "_index": "website", "_type": "blog", "_id": "1" }}
{ "title": "Ambari源码编译","author":"程裕强","postdate":"2016-12-21","abstract":"CentOS7.x下的Ambari2.4源码编译","url":"http://url.cn/53788351"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "2" }}
{ "title": "watchman源码编译","author":"程裕强","postdate":"2016-12-23","abstract":"CentOS7.x的watchman源码编译","url":"http://url.cn/53844169"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "3" }}
{ "title": "CentOS升级gcc","author":"程裕强","postdate":"2016-12-25","abstract":"CentOS升级gcc","url":"http://url.cn/53868915"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "4" }}
{ "title": "vmware复制虚拟机","author":"程裕强","postdate":"2016-12-29","abstract":"vmware复制虚拟机","url":"http://url.cn/53946664"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "5" }}
{ "title": "libstdc++.so.6","author":"程裕强","postdate":"2016-12-30","abstract":"libstdc++.so.6问题解决","url":"http://url.cn/53946911"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "6" }}
{ "title": "CentOS更换国内yum源","author":"程裕强","postdate":"2016-12-30","abstract":"CentOS更换国内yum源","url":"http://url.cn/53946911"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "7" }}
{ "title": "搭建Ember开发环境","author":"程裕强","postdate":"2016-12-30","abstract":"CentOS下搭建Ember开发环境","url":"http://url.cn/53947507"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "8" }}
{ "title": "es高亮","author":"程裕强","postdate":"2017-01-03","abstract":"Elasticsearch查询关键字高亮","url":"http://url/53991802"}
{ "index":{ "_index": "website", "_type": "blog", "_id": "9" }}
{ "title": "to be or not to be","author":"somebody","postdate":"2018-01-03","abstract":"to be or not to be,that is the question","url":"http://url/63991802"}

term查询

GET website/_search
{
 "query": {
   "term": {
     "title":"vmware"
   }
 }
}

返回结果

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "4",
        "_score": 0.9227539,
        "_source": {
          "title": "vmware复制虚拟机",
          "author": "程裕强",
          "postdate": "2016-12-29",
          "abstract": "vmware复制虚拟机",
          "url": "http://url.cn/53946664"
        }
      }
    ]
  }
}

分页查询

GET website/_search
{
  "from": 0,
  "size": 3,
  "query": {
    "match_all": {}
  }
}

返回

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": 1,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "5",
        "_score": 1,
        "_source": {
          "title": "libstdc++.so.6",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "libstdc++.so.6问题解决",
          "url": "http://url.cn/53946911"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "8",
        "_score": 1,
        "_source": {
          "title": "es高亮",
          "author": "程裕强",
          "postdate": "2017-01-03",
          "abstract": "Elasticsearch查询关键字高亮",
          "url": "http://url/53991802"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "9",
        "_score": 1,
        "_source": {
          "title": "to be or not to be",
          "author": "somebody",
          "postdate": "2018-01-03",
          "abstract": "to be or not to be,that is the question",
          "url": "http://url/63991802"
        }
      }
    ]
  }
}

过滤字段

GET website/_search
{
  "_source": ["title","author"],
  "query": {
    "term": {
      "title": "centos"
    }
  }
}

返回

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": {
          "author": "程裕强",
          "title": "CentOS更换国内yum源"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "author": "程裕强",
          "title": "CentOS升级gcc"
        }
      }
    ]
  }
}

显示version

GET website/_search
{
  "_source": ["title"],
  "version": true, 
  "query": {
    "term": {
      "title": "centos"
    }
  }
}

返回

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_version": 1,
        "_score": 0.9227539,
        "_source": {
          "title": "CentOS更换国内yum源"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_version": 1,
        "_score": 0.2876821,
        "_source": {
          "title": "CentOS升级gcc"
        }
      }
    ]
  }
}

评分过滤

GET website/_search
{
  "min_score":"0.5",
  "query": {
    "term": {
      "title":"centos"
    }
  }
}

返回

{
  "took": 43,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": {
          "title": "CentOS更换国内yum源",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "CentOS更换国内yum源",
          "url": "http://url.cn/53946911"
        }
      }
    ]
  }
}

高亮关键字

GET website/_search
{
  "query": {
    "term": {
      "title":"centos"
    }
  },
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}

返回

{
  "took": 124,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": {
          "title": "CentOS更换国内yum源",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "CentOS更换国内yum源",
          "url": "http://url.cn/53946911"
        },
        "highlight": {
          "title": [
            "<em>CentOS</em>更换国内yum源"
          ]
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "title": "CentOS升级gcc",
          "author": "程裕强",
          "postdate": "2016-12-25",
          "abstract": "CentOS升级gcc",
          "url": "http://url.cn/53868915"
        },
        "highlight": {
          "title": [
            "<em>CentOS</em>升级gcc"
          ]
        }
      }
    ]
  }

高级别全文检索通常用于在全文本字段(如电子邮件正文)上运行全文检索。 他们了解如何分析被查询的字段,并在执行之前将每个字段的分析器(或search_analyzer)应用于查询字符串。

match查询
(1)引例

GET website/_search
{
  "query": {
    "term": {
        "title": "centos升级"
    }
  }
}

返回

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

(2)and操作符

GET website/_search
{
  "query": {
    "match": {
        "title": {
          "query":"centos升级",
          "operator":"and"
        }
    }
  }
}

返回结果

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 0.5753642,
        "_source": {
          "title": "CentOS升级gcc",
          "author": "程裕强",
          "postdate": "2016-12-25",
          "abstract": "CentOS升级gcc",
          "url": "http://url.cn/53868915"
        }
      }
    ]
  }
}

(3)or操作符

GET website/_search
{
  "query": {
    "match": {
        "title": {
          "query":"centos升级",
          "operator":"or"
        }
    }
  }
}

返回

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": {
          "title": "CentOS更换国内yum源",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "CentOS更换国内yum源",
          "url": "http://url.cn/53946911"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 0.5753642,
        "_source": {
          "title": "CentOS升级gcc",
          "author": "程裕强",
          "postdate": "2016-12-25",
          "abstract": "CentOS升级gcc",
          "url": "http://url.cn/53868915"
        }
      }
    ]
  }
}

总结:term代表精确匹配,title必须为centos升级才能被查出,match先分词再进行匹配,加上operator操作符,代表分词的结果中必须包含centos升级才能被查出。

match_phrase查询(短语查询)
match_phrase与match query类似,但用于匹配精确词语,可称为短语查询。
match_parase查询会将查询内容分词,分词器可以自定义,文档中同时满足以下两个条件才会被检索到:a.分词后所有个此项都要出现在该字段内;b.字段中的词项顺序要一致
(1)创建索引,插入数据

DELETE test
PUT test
PUT test/hello/1
{ "content":"World Hello"}
PUT test/hello/2
{ "content":"Hello World"}
PUT test/hello/3
{ "content":"I just said hello world"}

(2)使用match_phrase查询"hello word"

GET test/_search
{
  "query": {
    "match_phrase": {
      "content": "hello world"
    }
  }
}

返回结果为

{
  "took": 16,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "test",
        "_type": "hello",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "content": "Hello World"
        }
      },
      {
        "_index": "test",
        "_type": "hello",
        "_id": "3",
        "_score": 0.5753642,
        "_source": {
          "content": "I just said hello world"
        }
      }
    ]
  }
}

match_phrase_prefix查询(前缀查询)
match_phrase_prefix与match_phrase相同,只是它允许在文本中的最后一个词的前缀匹配。也就是说对match_phrase进行了扩展,查询内容的分词只要满足前缀匹配即可。

GET test/_search
{
  "query": {
    "match_phrase_prefix": {
      "content": "hello worl"
    }
  }
}

返回

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "test",
        "_type": "hello",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "content": "Hello World"
        }
      },
      {
        "_index": "test",
        "_type": "hello",
        "_id": "3",
        "_score": 0.5753642,
        "_source": {
          "content": "I just said hello world"
        }
      }
    ]
  }
}

multi_match
multi_match查询是match查询的升级版,用于多字段检索

GET website/_search
{
  "query": {
    "multi_match": {
      "query": "centos",
      "fields": ["title","abstract"]
    }
  }
}

返回结果

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": {
          "title": "CentOS更换国内yum源",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "CentOS更换国内yum源",
          "url": "http://url.cn/53946911"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "2",
        "_score": 0.41360322,
        "_source": {
          "title": "watchman源码编译",
          "author": "程裕强",
          "postdate": "2016-12-23",
          "abstract": "CentOS7.x的watchman源码编译",
          "url": "http://url.cn/53844169"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "title": "CentOS升级gcc",
          "author": "程裕强",
          "postdate": "2016-12-25",
          "abstract": "CentOS升级gcc",
          "url": "http://url.cn/53868915"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "7",
        "_score": 0.20725916,
        "_source": {
          "title": "搭建Ember开发环境",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "CentOS下搭建Ember开发环境",
          "url": "http://url.cn/53947507"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "1",
        "_score": 0.1627405,
        "_source": {
          "title": "Ambari源码编译",
          "author": "程裕强",
          "postdate": "2016-12-21",
          "abstract": "CentOS7.x下的Ambari2.4源码编译",
          "url": "http://url.cn/53788351"
        }
      }
    ]
  }
}

可见文档中title和abstract字段有一个匹配就会被检索出来。

common_terms查询(常用词查询)
(1)停用词
有些词在文本中出现的频率非常高,但是对文本锁携带的基本信息不产生影响。比如英文中的a、an、the、of,中文的“的”、”了”、”着”、”是” 、标点符号等。文本经过分词之后,停用词通常被过滤掉,不会被进行索引。在检索的时候,用户的查询中如果含有停用词,检索系统也会将其过滤掉(因为用户输入的查询字符串也要进行分词处理)。排除停用词可以加快建立索引的速度,减小索引库文件的大小。
(2)虽然停用词对文档评分影响不大,但是有时停用词仍然具有重要意义,去除停用词显然不合适。如果去除停用词,就无法区分“happy”和”not happy”, “to be or not to be”就不能被索引,搜索的准确率就会降低。
(3)common_terms查询提供了一种解决方案,把查询分次后的词项分为重要词项(比如low frequency terms,低频词)和不重要词(high frequency terms which would previously have been stopwords,高频的停用词)。在搜索时,首先搜索与重要词匹配的文档,然后执行第二次搜索,搜索评分较小的高频词。
词项是高频词还是低频词,可以通过cutoff_frequency来设置阀值,取值可以是绝对频率 (>=1)或者相对频率(0.0 ~1.0)

GET website/_search
{
    "query": {
        "common": {
            "title": {
                "query": "to be",
                "cutoff_frequency": 0.0001,
                "low_freq_operator": "and"
            }
        }
    }
}

返回结果

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

参考学习的博客上又返回

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 2.364739,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "9",
        "_score": 2.364739,
        "_source": {
          "title": "to be or not to be",
          "author": "somebody",
          "postdate": "2018-01-03",
          "abstract": "to be or not to be,that is the question",
          "url": "http://url/63991802"
        }
      }
    ]
  }
}

不知道什么原因。

词项查询介绍
全文查询将在执行之前分析查询字符串,但词项级别查询将按照存储在倒排索引中的词项进行精确操作。
这些查询通常用于数字,日期和枚举等结构化数据,而不是全文本字段。或者,他们允许您制作低级查询,并在分析过程之前进行。

term查询
term查询用于词项搜索

terms查询
terms查询可以用来查询文档中包含任一个给定多词项的文档;

terms_set查询
terms_set查询是一个新的查询,它的语法将来可能会变
查找与一个或多个指定词项匹配的文档,其中必须匹配的术语数量取决于指定的最小值,应匹配字段或脚本

PUT my-index
{
 "mappings": {
   "doc":{
     "properties": {
       "required_matches":{
         "type": "long"
       }
     }
   }
 } 
}
PUT /my-index/doc/1?refresh
{
    "codes": ["ghi", "jkl"],
    "required_matches": 2
}
PUT /my-index/doc/2?refresh
{
    "codes": ["def", "ghi"],
    "required_matches": 2
}

最小值匹配的字段

GET my-index/_search
{
  "query": {
    "terms_set":{
      "codes":{
        "terms" : ["abc", "def", "ghi"],
        "minimum_should_match_field":"required_matches"
      }
    }
  }
}

查询结果

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "codes": [
            "def",
            "ghi"
          ],
          "required_matches": 2
        }
      }
    ]
  }
}

一个总是限制匹配条件数量永远不会超过指定词项数量的例子如下,其中params.num_terms参数在脚本中可用,以指示已指定的词项数量

GET my-index/_search
{
  "query": {
    "terms_set":{
      "codes":{
        "terms" : ["abc", "def", "ghi"],
        "minimum_should_match_script":{
          "source": "Math.min(params.num_terms, doc['required_matches'].value)"
        }
      }
    }
  }
}

返回结果

{
  "took": 163,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "codes": [
            "def",
            "ghi"
          ],
          "required_matches": 2
        }
      }
    ]
  }
}

range查询
range查询用于匹配数值型、日期型或字符串字段在某一范围内的文档
(1)搜索age字段在10到20的所有文档

DELETE my-index
PUT my-index
PUT my-index/doc/1
{"age":12}
 
PUT my-index/doc/2
{"age":18}
 
PUT my-index/doc/3
{"age":21}
GET _search
{
  "query": {
    "range": {
      "age": {
        "gte": 10,
        "lte": 20,
        "boost": 2
      }
    }
  }
}

返回

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 47,
    "successful": 47,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 2,
    "hits": [
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "2",
        "_score": 2,
        "_source": {
          "age": 18
        }
      },
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "1",
        "_score": 2,
        "_source": {
          "age": 12
        }
      }
    ]
  }
}

(2)日期范围查询

GET website/_search
{
  "query": {
    "range": {
      "postdate": {
        "gte": "2017-01-01",
        "lte": "2017-12-31",
        "format": "yyyy-MM-dd"
      }
    }
  }
}

返回结果

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "8",
        "_score": 1,
        "_source": {
          "title": "es高亮",
          "author": "程裕强",
          "postdate": "2017-01-03",
          "abstract": "Elasticsearch查询关键字高亮",
          "url": "http://url/53991802"
        }
      }
    ]
  }
}

exists查询
返回原始字段中至少包含一个非空值的文档

PUT my-index/doc/1
{ "user": "jane" }
 
PUT my-index/doc/2
{ "user": "" } 
 
PUT my-index/doc/3
{ "user": [] }
 
PUT my-index/doc/4
{ "user": ["jane", null ] }
 
PUT my-index/doc/5
{ "age": 28 }
GET _search
{
  "query": {
    "exists":{"field":"user"}
  }
}

返回结果

{
  "took": 34,
  "timed_out": false,
  "_shards": {
    "total": 47,
    "successful": 47,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "user": ""
        }
      },
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "4",
        "_score": 1,
        "_source": {
          "user": [
            "jane",
            null
          ]
        }
      },
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "user": "jane"
        }
      }
    ]
  }
}

prefix查询
(1)查询以ki开头的用户

GET /_search
{
  "query": {
    "prefix": {"user": "ki"}
  }
}

返回

{
  "took": 58,
  "timed_out": false,
  "_shards": {
    "total": 47,
    "successful": 47,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

(2)查询以ja开头的用户

GET /_search
{
  "query": {
    "prefix": {"user": "ja"}
  }
}

返回

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 47,
    "successful": 47,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "4",
        "_score": 1,
        "_source": {
          "user": [
            "jane",
            null
          ]
        }
      },
      {
        "_index": "my-index",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "user": "jane"
        }
      }
    ]
  }
}

wildcard查询(通配符匹配)

GET website/_search
{
  "query": {
    "wildcard": {
      "title":"*yum*"
    }
  }
}

返回

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 1,
        "_source": {
          "title": "CentOS更换国内yum源",
          "author": "程裕强",
          "postdate": "2016-12-30",
          "abstract": "CentOS更换国内yum源",
          "url": "http://url.cn/53946911"
        }
      }
    ]
  }
}

regexp查询(正则表达式查询)
正则表达式查询的性能很大程度上取决于所选的正则表达式。类似.的匹配内容的正则表达式非常缓慢,并且使用了lookaround正则表达式。如果可以的话,请尝试在正则表达式开始之前使用长前缀。像.?+这样的通配匹配符器大多会降低性能。
大多数正则表达式引擎,允许您匹配字符串的任何部分。如果你想让正则表达式从字符串开头开始,或者在字符串的末尾完成,那么你必须明确的定位它,使用^表示开始或$结束
在这里插入图片描述

GET website/_search
{
  "query": {
    "regexp":{
      "title":"gc.*"
    }
  }
}

返回

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 1,
        "_source": {
          "title": "CentOS升级gcc",
          "author": "程裕强",
          "postdate": "2016-12-25",
          "abstract": "CentOS升级gcc",
          "url": "http://url.cn/53868915"
        }
      }
    ]
  }
}

fuzzy查询(模糊查询)

GET website/_search
{
  "query": {
    "fuzzy":{
      "title":"vmwere"
    }
  }
}

返回

{
  "took": 45,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.76896155,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "4",
        "_score": 0.76896155,
        "_source": {
          "title": "vmware复制虚拟机",
          "author": "程裕强",
          "postdate": "2016-12-29",
          "abstract": "vmware复制虚拟机",
          "url": "http://url.cn/53946664"
        }
      }
    ]
  }
}

type查询

GET /_search
{
    "query": {
        "type" : {
            "value" : "my_type"
        }
    }
}

返回

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 47,
    "successful": 47,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "2",
        "_score": 1,
        "_source": {
          "city": "York"
        }
      },
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "1",
        "_score": 1,
        "_source": {
          "city": "New York"
        }
      }
    ]
  }
}

ids查询

GET _search
{
  "query": {
    "ids":{
      "type": "blog",
      "values":["2","3"]
    }
  }
}

返回

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 47,
    "successful": 47,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "watchman源码编译",
          "author": "程裕强",
          "postdate": "2016-12-23",
          "abstract": "CentOS7.x的watchman源码编译",
          "url": "http://url.cn/53844169"
        }
      },
      {
        "_index": "website",
        "_type": "blog",
        "_id": "3",
        "_score": 1,
        "_source": {
          "title": "CentOS升级gcc",
          "author": "程裕强",
          "postdate": "2016-12-25",
          "abstract": "CentOS升级gcc",
          "url": "http://url.cn/53868915"
        }
      }
    ]
  }
}

下一篇:ElasticSearch学习(十五) --高亮操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值