Elasticsearch学习第二篇--常用的几种搜索方式

一、Query String Search

类似HTTP的GET请求,参数拼接。

查询全部

get test_index/base/_search
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "xiaomei",
          "age": 18,
          "sex": "F"
        }
      },
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "xiaoming",
          "age": 18,
          "sex": "M"
        }
      },
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "xiaojiejie",
          "age": 25,
          "sex": "F"
        }
      }
    ]
  }
}

这里为了学习方便,先不用中文了。
took:耗费了几毫秒
timed_out:是否超时,这里是没有
_shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
hits.total:查询结果的数量,3个document
hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
hits.hits:包含了匹配搜索的document的详细数据

条件查询

GET /test_index/base/_search?q=age:18&q=name:xiaomei&sort=age:desc

查询结果

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": null,
    "hits": [
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "2",
        "_score": null,
        "_source": {
          "name": "xiaomei",
          "age": 18,
          "sex": "F"
        },
        "sort": [
          18
        ]
      }
    ]
  }
}

这种只适合比较简单的查询,比如根据id;那些复杂的查询不适用。一般不使用这种查询方法

二、Query DSL

有点类似POST请求。可以用json的格式来构建查询语法,支持复杂的语法,更加强大。常用的就是此方法。

查询全部

GET /test_index/base/_search
{
  "query": {
    "match_all": {}
  }
}

可以查得全部。

条件查询

GET /test_index/base/_search
{
  "query": {
    "match": {
      "name": "xiaomei"
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

查询结果

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": null,
    "hits": [
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "2",
        "_score": null,
        "_source": {
          "name": "xiaomei",
          "age": 18,
          "sex": "F"
        },
        "sort": [
          18
        ]
      }
    ]
  }
}

三、Query Filter

对数据过滤
如下:查询10-20岁的女性

GET /test_index/base/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "sex": "F"
        }}
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 10,
            "lte": 20
          }
        }
      }
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

查询结果
过滤掉了25岁的xiaojiejie

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": null,
    "hits": [
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "2",
        "_score": null,
        "_source": {
          "name": "xiaomei",
          "age": 18,
          "sex": "F"
        },
        "sort": [
          18
        ]
      }
    ]
  }
}

四、Full-text Search 全文检索

我先加个兴趣属性,方便测试

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "xiaomei",
          "age": 18,
          "sex": "F",
          "interests": "music movie"
        }
      },
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "xiaoming",
          "age": 18,
          "sex": "M",
          "interests": "sport movie"
        }
      },
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "xiaojiejie",
          "age": 25,
          "sex": "F",
          "interests": "movie"
        }
      }
    ]
  }
}

然后开始查询兴趣为movie的数据

GET /test_index/base/_search
{
  "query": {
    "match": {
      "interests": "music movie"
    }
  }
}

结果

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "name": "xiaomei",
          "age": 18,
          "sex": "F",
          "interests": "music movie"
        }
      },
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "xiaoming",
          "age": 18,
          "sex": "M",
          "interests": "sport movie"
        }
      },
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "name": "xiaojiejie",
          "age": 25,
          "sex": "F",
          "interests": "movie"
        }
      }
    ]
  }
}

可以看出查到3条记录,但score有差异。
先查看interests字段的type

GET /test_index/_mapping/base

结果

{
  "test_index": {
    "mappings": {
      "base": {
        "properties": {
          "age": {
            "type": "long"
          },
          "interests": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "sex": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

可以看出为text类型,所以我们查询music movie时,会被拆解为music 、movie 。现在总共3条数据 满足情况如下:

nameinterestsmatch
xiaomeimusic movie2
xiaomingsport movie1
xiaojiejiemovie1

所以匹配出3条数据,其中xiaomei的匹配度最高,score最大。

六、Phase Search 短语搜索

和全文检索不同,全部检索是拆解,然后倒排索引中搜索;短语检索是整个短语作为条件搜索。

GET /test_index/base/_search
{
  "query": {
    "match_phrase": {
      "interests": "music movie"
    }
  }
}

结果:只有xiaomei匹配上

{
  "took": 14,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "name": "xiaomei",
          "age": 18,
          "sex": "F",
          "interests": "music movie"
        }
      }
    ]
  }
}

六、Highlight search 高亮搜索

就是把一些字段高亮显示

GET /test_index/base/_search
{
  "query": {
    "match_phrase": {
      "interests": "music movie"
    }
  },
  "highlight": {
    "fields": {
      "interests": {}
    }
  }
}

结果:除了返回的数据外,另外返回了高亮显示的部分
music movie

{
  "took": 56,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
        "_index": "test_index",
        "_type": "base",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
          "name": "xiaomei",
          "age": 18,
          "sex": "F",
          "interests": "music movie"
        },
        "highlight": {
          "interests": [
            "<em>music</em> <em>movie</em>"
          ]
        }
      }
    ]
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值