elasticsearch如何搜索%这样的字符?

mm9b1k5b  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(369)

例如,我需要搜索一个值 100% ,我有文档,例如,文本 "100% guarantee" 相反,它是根据 100% ,也就是说,在hits中只突出显示数字(10010021000-500500-100….)
查询:

GET _search
{
  "query": {
    "multi_match": {
      "query": "100%",
      "type": "phrase_prefix", 
      "fields": ["title", "body"]
    }
  },
  "size": 5
  }

我试着:

GET _search
{
  "query": {
    "query_string": {
      "default_field": "body",
      "query": "100%",
      "allow_leading_wildcard": false
    }
  },
  "size": 5,
  "highlight": {
    "fields": {
      "body": {
        "type": "plain",
        "fragment_size": 20,
        "pre_tags": "<span class='bold'>",
        "post_tags": "</span>", 
        "number_of_fragments": 1
      }
    }
  }
  }

第一个突出显示示例:

"highlight" : {
          "body" : [
            "-<span class='bold'>100</span> - you number"
          ]
        }
vlf7wbxs

vlf7wbxs1#

在突出显示的查询中,只突出显示数字而不突出显示特殊字符,这是因为 standard 分析仪。因为,您还没有为您的 title 字段,因此标准分析器是默认分析器,如果未指定,则使用该分析器。

GET/_analyze
{
  "analyzer" : "standard",
  "text" : "100% guarantee"
}

生成以下令牌:

{
  "tokens": [
    {
      "token": "100",
      "start_offset": 0,
      "end_offset": 3,
      "type": "<NUM>",
      "position": 0
    },
    {
      "token": "guarantee",
      "start_offset": 5,
      "end_offset": 14,
      "type": "<ALPHANUM>",
      "position": 1
    }
  ]
}

您可以看到elasticsearch的标准分析器只是去掉了“%”字符。分析器是在索引时应用的,因此您的文本永远不会按您的需要进入索引。
添加索引数据、Map、搜索查询和搜索结果的工作示例
索引Map:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "filter": [
            "lowercase"
          ],
          "tokenizer": "whitespace"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "my_analyzer"
      }
    }
  }
}

分析api:

GET/_analyze
{
  "analyzer" : "my_analyzer",
  "text" : "100% guarantee"
}

生成以下令牌:

{
  "tokens": [
    {
      "token": "100%",
      "start_offset": 0,
      "end_offset": 4,
      "type": "word",
      "position": 0
    },
    {
      "token": "guarantee",
      "start_offset": 5,
      "end_offset": 14,
      "type": "word",
      "position": 1
    }
  ]
}

索引数据:

{
  "title": "100% guarantee"
}
{
  "title": "100 guarantee"
}

搜索查询:

{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "100%",
      "allow_leading_wildcard": false
    }
  },
  "size": 5,
  "highlight": {
    "fields": {
      "title": {
        "type": "plain",
        "fragment_size": 20,
        "pre_tags": "<span class='bold'>",
        "post_tags": "</span>",
        "number_of_fragments": 1
      }
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "64608747",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.6931471,
        "_source": {
          "title": "100% guarantee"
        },
        "highlight": {
          "title": [
            "<span class='bold'>100%</span> guarantee"
          ]
        }
      }
    ]

相关问题