一、复杂查询

1.1、复杂条件搜索

  1. # 复杂条件搜索
  2. GET tindex/_doc/_search
  3. {
  4. "query":{
  5. "match":{
  6. "name": "wells"
  7. }
  8. }
  9. }

【Elasticsearch实践】(六)ES搜索 - 图1

1.2、指定输出字段

  1. # 指定输出字段
  2. GET /tindex/_doc/_search
  3. {
  4. "query": {
  5. "match":{
  6. "name": "wells"
  7. }
  8. },
  9. "_source": ["name", "age"]
  10. }

【Elasticsearch实践】(六)ES搜索 - 图2

1.3、排序

  1. # 排序
  2. GET /tindex/_doc/_search
  3. {
  4. "query": {
  5. "match": {
  6. "name": "wells"
  7. }
  8. },
  9. "_source": ["name", "age"],
  10. "sort": [
  11. {
  12. "age": {
  13. "order": "desc"
  14. }
  15. }
  16. ]
  17. }

【Elasticsearch实践】(六)ES搜索 - 图3

1.4、分页查询

  1. # 分页查询
  2. GET /tindex/_doc/_search
  3. {
  4. "query": {
  5. "match": {
  6. "name": "wells"
  7. }
  8. },
  9. "from": 0,
  10. "size": 1
  11. }

【Elasticsearch实践】(六)ES搜索 - 图4

1.5、布尔值查询

1.5.1、must (and)

  1. GET /tindex/_doc/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "match": {
  8. "name": "wells"
  9. }
  10. },
  11. {
  12. "match": {
  13. "age": 17
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }

【Elasticsearch实践】(六)ES搜索 - 图5

1.5.2、should (or)

  1. # bool: should 查询 where name=wells or age = 17
  2. GET /tindex/_doc/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "should": [
  7. {
  8. "match": {
  9. "name": "wells"
  10. }
  11. },
  12. {
  13. "match": {
  14. "age": 17
  15. }
  16. }
  17. ]
  18. }
  19. }
  20. }

【Elasticsearch实践】(六)ES搜索 - 图6

1.5.3、must_not (not)

  1. # bool: must_not 查询 where name != wells
  2. GET /tindex/_doc/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must_not": [
  7. {
  8. "match": {
  9. "name": "wells"
  10. }
  11. },
  12. {
  13. "match": {
  14. "name": "tom"
  15. }
  16. }
  17. ]
  18. }
  19. }
  20. }
  21. # 或者 bool: must_not 查询 where name != wells
  22. GET /tindex/_doc/_search
  23. {
  24. "query": {
  25. "bool": {
  26. "must_not": [
  27. {
  28. "match": {
  29. # 通过空格隔开,设置多个值
  30. "name": "wells tom"
  31. }
  32. }
  33. ]
  34. }
  35. }
  36. }

【Elasticsearch实践】(六)ES搜索 - 图7

1.5.4、过滤器filter

  1. # filter
  2. GET /tindex/_doc/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must": [
  7. {
  8. "match": {
  9. "name": "wells"
  10. }
  11. }
  12. ],
  13. "filter": {
  14. "range": {
  15. "age": {
  16. "lt": 30,
  17. "gt": 18
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }

【Elasticsearch实践】(六)ES搜索 - 图8

1.6、匹配多个值

  1. # 匹配多个值
  2. GET /tindex/_doc/_search
  3. {
  4. "query": {
  5. "match": {
  6. "tags": "man 技术"
  7. }
  8. }
  9. }

【Elasticsearch实践】(六)ES搜索 - 图9

二、精确查询

两个类型:

  • text:会被分词
  • keyword:不会被分词

通过 _analyze 对词进行分析查看,text与keyword都是fields的类型,通过类型可以区分是否精确查询

  1. # text 与 keyword
  2. GET _analyze
  3. {
  4. "analyzer": "standard",
  5. "text": "wells学es"
  6. }
  7. # text 与 keyword
  8. GET _analyze
  9. {
  10. "analyzer": "keyword",
  11. "text": "wells学es"
  12. }

【Elasticsearch实践】(六)ES搜索 - 图10
【Elasticsearch实践】(六)ES搜索 - 图11
关于分词:

  • term:查询是直接通过倒排索引指定的词条进行精确查找的,不会进行分词
  • match:使用分词器进行解析,再进行查询
  1. # term 与 match
  2. GET /tindex/_doc/_search
  3. {
  4. "query": {
  5. "match": {
  6. "tags": "技术宅男"
  7. }
  8. }
  9. }
  10. # term 与 match
  11. GET /tindex/_doc/_search
  12. {
  13. "query": {
  14. "term": {
  15. "tags": "技术宅男"
  16. }
  17. }
  18. }

2.1、精确匹配多个值

2.2、高亮

2.2.1、默认高亮

高亮

  1. GET /tindex/_doc/_search
  2. {
  3. "query": {
  4. "match": {
  5. "tags": "技术"
  6. }
  7. },
  8. "highlight":{
  9. "fields":{
  10. "name":{}
  11. }
  12. }
  13. }

2.2.2、自定义高亮

  1. GET /tindex/_doc/_search
  2. {
  3. "query": {
  4. "match": {
  5. "tags": "技术"
  6. }
  7. },
  8. "highlight":{
  9. "pre_tags": "<p color='red'>",
  10. "post_tags": "</p>",
  11. "fields":{
  12. "name":{}
  13. }
  14. }
  15. }