ElasticSearch学习2 - 查询、修改

x33g5p2x  于2022-05-05 转载在 ElasticSearch  
字(4.1k)|赞(0)|评价(0)|浏览(292)

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 精确查询 - 内部直接通过倒排索引的词条进行查询

查询term:如果字段是text类型内部自动进行分词检索,keyword类型则是精确查询整个文本需匹配一致

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":{}
    }
  }
}

相关文章