【问题标题】:Match by substring按子串匹配
【发布时间】:2019-01-28 11:20:06
【问题描述】:

我需要获取Description 字段包含子字符串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/859Description 字段中(没关系)
  • 但我也会在Description 字段中获得带有28 的文档(我不需要它)

我怎样才能只获取带有子字符串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": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      },

【问题讨论】:

  • 您能否在查询中添加 explain:true 并显示输出?
  • @Mysterion,我已经更新了我的问题。

标签: elasticsearch


【解决方案1】:

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

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

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

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

【讨论】:

  • 谢谢,对我有帮助。
【解决方案2】:

Match_Phrase 应该满足要求。

示例代码:

GET <indexname>/_search
{

  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "Description": """28/859"""
          }
        }
      ]
    }
  }
}

【讨论】:

  • 它只有在28/859 完整Description 时才有效,但在我的情况下它只是一个子字符串。
  • 你试过了吗?当 28/859 是相同顺序的字符串的一部分时,这意味着字段描述。然而,你也可以试试这个:
      GET <indexname>/_search { "query": { "bool": { "must": [ { "match_phrase": { "Description": "28" } }, { "match_phrase": { "Description": "/859" } } ] } } }</indexname>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 2018-05-31
相关资源
最近更新 更多