ElasticSearch学习2 - 查询、修改

87 阅读3分钟

文章目录

0. 修改索引

大文本字段支持排序

PUT http://localhost:9200/lrc_blog/_mapping

//请求体
{
	"properties": {
		"title": {
			"type": "text",
			"fielddata": true
		}
	}
}


1. 查询


1.1 字段检索-match

post http://localhost:9200/索引名/_search

//请求体
{
	"query": {
		"match": {
			"title": "科技"
		}
	}
}



全部数据
在这里插入图片描述



检索结果数据
在这里插入图片描述


1.2 查询结果仅显示需要的字段-_source

post http://localhost:9200/索引名/_search

//请求体
{
	"query": {
		"match": {
			"title": "科技"
		}
	},
    "_source": ["title", "content"]
}



在这里插入图片描述


1.3 结果集进行排序-sort

post http://localhost:9200/lrc_blog/_search

//请求体

{
    "query": {
        "match": {
            "title": "科技"
        }
    },
    "_source": [
        "title",
        "content"
    ],
    "sort": [
        {
            "title": {
                "order": "desc"
            }
        }
    ]
}

在这里插入图片描述


1.4 分页查询 - from从0开始、size每页行数

post http://localhost:9200/lrc_blog/_search

//请求体
{
    "query": {
        "match": {
            "title": "科技"
        }
    },
    "_source": [
        "title",
        "content"
    ],
    "sort": [
        {
            "title": {
                "order": "desc"
            }
        }
    ],
    "from": 1,
    "size": 2
}

在这里插入图片描述


1.5 多条件查询and-must

post http://localhost:9200/lrc_blog/_search


//请求体
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "捉着"
          }
        },
        {
          "match": {
            "content": "关于庙堂权争与刀剑交错的江湖"
          }
        }
      ]
    }
  },
  "_source": [
    "title",
    "content"
  ],
  "sort": [
    {
      "title": {
        "order": "asc"
      }
    }
  ]
}

全部数据
在这里插入图片描述



检索结果

在这里插入图片描述


1.6 多条件查询or-should

post http://localhost:9200/lrc_blog/_search


//请求体
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "捉着"
          }
        },
        {
          "match": {
            "content": "关于庙堂权争与刀剑交错的江湖"
          }
        }
      ]
    }
  },
  "_source": [
    "title",
    "content"
  ],
  "sort": [
    {
      "title": {
        "order": "asc"
      }
    }
  ]
}

全部数据
在这里插入图片描述



检索结果
在这里插入图片描述


1.7 否定-must_not

post http://localhost:9200/lrc_blog/_search

//请求体
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "title": "科技兴国"
          }
        }
      ],
      "must": [
        {
          "match": {
            "content": "关于庙堂权争与刀剑交错的江湖"
          }
        }
      ]
    }
  },
  "_source": [
    "title",
    "content"
  ],
  "sort": [
    {
      "title": {
        "order": "asc"
      }
    }
  ]
}

在这里插入图片描述


1.8 结果集过滤-filter

post http://localhost:9200/lrc_blog/_search

//请求体
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "title": "科技兴国"
          }
        }
      ],
      "must": [
        {
          "match": {
            "content": "关于庙堂权争与刀剑交错的江湖"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 30,
            "lte": 60
          }
        }
      }
    }
  },
  "_source": [
    "title",
    "content",
    "age"
  ],
  "sort": [
    {
      "title": {
        "order": "asc"
      }
    }
  ]
}

全部数据
在这里插入图片描述

检索数据
在这里插入图片描述


1.9 数组模糊查询 - 多个值使用空格隔开

post http://localhost:9200/lrc_blog/_search


//请求体
{
  "query": {
    "match": {
      "tags": "技术 文章"
    }
  },
  "_source": [
    "title",
    "content",
    "age",
    "tags"
  ]
}

在这里插入图片描述


1.10 精确查询 - 内部直接通过倒排索引的词条进行查询

post http://localhost:9200/lrc_blog3/_search

//请求体1
{
    "query": {
        "term": {
            "name": "懂"
        }
    }
}

//请求体2
{
    "query": {
        "term": {
            "name": "懂法守法所发生的 name 5"
        }
    }


//请求体3
{
  "query": {
    "term": {
      "desc": "懂"
    }
  }
}


//请求体4
{
  "query": {
    "term": {
      "desc": "懂法守法所发生的 desc 5"
    }
  }
}



索引结构
在这里插入图片描述



索引全部数据
在这里插入图片描述



检索结果1-keyword-仅百分百匹配
在这里插入图片描述



检索结果2-keyword-仅百分百匹配
在这里插入图片描述



检索结果3-text-支持单字符
在这里插入图片描述



检索结果4-text-不支持百分百匹配
在这里插入图片描述


1.11 高亮显示检索关键字-highlight

基本使用

post http://localhost:9200/lrc_blog/_search


//请求体
{
    "query": {
        "match": {
            "content": "江湖 刀剑"
        }
    },
    "highlight": {
        "fields": {
            "content": {}
        }
    }
}

在这里插入图片描述

自定义高亮标签

post http://localhost:9200/lrc_blog/_search


//请求体
{
  "query": {
    "match": {
      "content": "江湖 刀剑"
    }
  },
  "highlight": {
    "pre_tags": "<span style='color:red'>", 
    "post_tags": "</span>", 
    "fields": {
      "content":{}
    }
  }
}

在这里插入图片描述