首页 > 解决方案 > 按子串匹配

问题描述

我需要获取Description字段包含 substring的文档28/859。这是我的查询:

{
  "explain": true,
  "query": {
    "bool":{
      "filter":{
        "bool":{"should":[{"query_string":{"default_field":"Description","query":"28//859", 
                                           "analyzer": "keyword"}}]}},
      "must_not":{"exists":{"field":"ParentId"}}}
  }
}

我得到的文件是:

如何28/859仅获取带有子字符串的文档?

更新:解释示例:

{
        "_shard": "[tech_places][4]",
        "_node": "u7QI_gjjRXy4-xdqnK2KMw",
        "_index": "tech_places",
        "_type": "entity",
        "_id": "8403",
        "_score": 0.0,
        "_source": {
          "Id": 8403,
          "Name": "RETE-43424",
          "Description": "SRF-10kv №28 VISO",
          "ParentId": null,
          "OrganizationId": 12,
          "OrganizationPath": "12_27",
          "LocationId": 27,
          "Classification": "",
          "Type": "A",
          "Status": 0,
          "MaintenanceObjectId": null,
          "TreePath": "8403"
        },
        "_explanation": {
          "value": 0.0,
          "description": "sum of:",
          "details": [
            {
              "value": 0.0,
              "description": "match on required clause, product of:",
              "details": [
                {
                  "value": 0.0,
                  "description": "# clause",
                  "details": []
                },
                {
                  "value": 0.0,
                  "description": "sum of:",
                  "details": [
                    {
                      "value": 0.0,
                      "description": "weight(Description:28 in 35112) [], result of:",
                      "details": [
                        {
                          "value": 0.0,
                          "description": "score(doc=35112,freq=1.0), with freq of:",
                          "details": [
                            {
                              "value": 1.0,
                              "description": "termFreq=1.0",
                              "details": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      },

标签: elasticsearch

解决方案


您可以whitespace为此使用分析器。使用如下属性创建/更新您的映射description

{
  "description": {
    "type": "text",
    "analyzer": "whitespace"
  }
}

这将确保像 28/859 这样的东西被视为单个令牌。您甚至可以使用正则表达式创建自己的自定义标记器/分析器。然后您可以使用下面的查询来获得所需的结果:

{
  "query": {
    "query_string": {
      "default_field": "description",
      "query": "28\\/859"
    }
  }
}

推荐阅读