Elasticsearch通过部分文本查找

aydmsdu9  于 2023-03-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(144)

我有什么案例,我想通过搜索文字找到:“本·弗兰克”,这份文件:

{
   "first_name" : "Benjamin",
   "last_name" : "Franklin"
}
GET /person/_search
{
  "query": {
    "multi_match": {
      "query": "ben frank", 
      "fields": ["first_name", "last_name"],
      "type": "phrase_prefix"
    }
  }
}
41zrol4v

41zrol4v1#

你可以使用edge n-gram。我做了一个创建新分析器的例子。

PUT idx_index
{
  "mappings": {
    "properties": {
      "first_name": {
        "type": "text",
        "fields": {
          "ngram": {
            "type": "text",
            "analyzer": "my_analyzer"
          }
        }
      },
      "last_name": {
        "type": "text",
        "fields": {
          "ngram": {
            "type": "text",
            "analyzer": "my_analyzer"
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer",
          "filter": [
            "lowercase"
          ]
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 5
        }
      }
    }
  }
}

质询

POST idx_index/_doc
{
   "first_name" : "Benjamin",
   "last_name" : "Franklin"
}

POST idx_index/_search
{
  "query": {
    "multi_match": {
      "query": "ben frank",
      "fields": [
        "first_name",
        "first_name.ngram",
        "last_name",
        "last_name.ngram"
      ],
      "type": "best_fields"
    }
  }
}

相关问题