miller
发布于

es match_phase 调整内容搜索, 全包含

优化了下,每次想搜索 全包含时候,带上prefix 引号 ' | " 即可

 if strings.HasPrefix(keyword, "\"") || strings.HasPrefix(keyword, "'") {
        keyword = keyword[1:]
        // terms 是包含多个搜索词, match_phase 是分词后,都包含
        // 如果使用term查询,要确保字段是no analyzed的。建索引的时候要注意。
        bq := elastic.NewBoolQuery()
        bq.Should(elastic.NewMatchPhraseQuery("title", keyword))
        bq.Should(elastic.NewMatchPhraseQuery("content", keyword))
        bq.Should(elastic.NewMatchPhraseQuery("tags", keyword))
        bq.MinimumNumberShouldMatch(1)
        query.Must(bq)
    } else {
        // multi_match 查询为能在多个字段上反复执行相同查询提供了一种便捷方式。
        query.Must(elastic.NewMultiMatchQuery(keyword, "title", "content", "tags"))
    }

BTW:

  • 用chatgpt 搜索问题
  • mysql sql语句转化 为es
{
	"bool": {
		"must": [
			{
				"term": {
					"status": 0
				}
			},
			{
				"bool": {
					"minimum_should_match": "1",
					"should": [
						{
							"match_phrase": {
								"title": {
									"query": "游戏行业"
								}
							}
						},
						{
							"match_phrase": {
								"content": {
									"query": "游戏行业"
								}
							}
						},
						{
							"match_phrase": {
								"tags": {
									"query": "游戏行业"
								}
							}
						}
					]
				}
			}
		]
	}
}
浏览 (314)
点赞
收藏
评论