首页 > 代码库 > 分布式搜索elasticsearch 索引文档的检索 入门

分布式搜索elasticsearch 索引文档的检索 入门

1、首先将如下数据提交到ES中建立索引

{"number":32768,"singer":"杨坤","size":"5109132","song":"今夜二十岁","tag":"中国好声音","timelen":319}
{"number":32769,"singer":"汪峰","size":"6001697","song":"我爱你中国","tag":"中国好声音","timelen":375}
{"number":32780,"singer":"汪峰","size":"4070469","song":"我如此爱你","tag":"中国好声音","timelen":254}
{"number":32796,"singer":"大小姐","size":"3046005","song":"登大人","tag":"儿歌","timelen":190}
{"number":32896,"singer":"Bandari","size":"3869593","song":"The Golden Land","tag":"胎教音乐","timelen":241}
{"number":32977,"singer":"Bandari","size":"3477514","song":"Childhood Memory","tag":"欧美","timelen":217}

2、ElasticSearch‘s query DSL

搜索的RESTful接口是_search

URL形式:http://ip:port/index/type/_search

query查询语句通过POST的方式发送到ES。

a、基本的全文检索

查找索引库中包含"音乐"的记录

{
  "query": {
    "query_string": {
      "query": "音乐"
    }
  }
}


b、指定字段进行检索

查找song字段中含有中国的记录

{
  "query": {
    "query_string": {
      "query": "中国",
      "fields": [
        "song"
      ]
    }
  }
}


c、多字段权重查询

在song、tag两个字段中搜索关键字“中国”,如果在song字段中出现权重是2,tag中是默认的1。通过结果可以看到歌曲名中含有中国是排在前面。

{
  "query": {
    "multi_match": {
      "query": "中国",
      "fields": [
        "song^2",
        "tag"
      ]
    }
  }
}


更多详细的查询语法可以参考:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-queries.html

分布式搜索elasticsearch 索引文档的检索 入门