【问题标题】:Issue with a basic elasticsearch "terms" query基本弹性搜索“术语”查询的问题
【发布时间】:2015-01-29 21:00:20
【问题描述】:

我正在尝试运行一个简单的弹性搜索术语查询,如下所示(使用 sense chrome 扩展):

GET _search
{
   "query": {
      "terms": {
         "childcareTypes": [
            "SHARED_CHARGE",
            "OUT_OF_SCHOOL"
         ],
         "minimum_match": 2
      }
   }
}

这会返回 0 次点击:

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }
}

我不知道为什么,因为 match_all 查询确实显示三个记录中的两个匹配:

GET _search
{
   "query": {
    "match_all": {}
   }
}

产量:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
         {
            "_index": "bignibou",
            "_type": "advertisement",
            "_id": "1",
            "_score": 1,
            "_source": {
               "id": 1,
               "childcareWorkerType": "AUXILIAIRE_PARENTALE",
               "childcareTypes": [
                  "SHARED_CHARGE",
                  "OUT_OF_SCHOOL"
               ],
               "giveBath": "YES"
            }
         },
         {
            "_index": "bignibou",
            "_type": "advertisement",
            "_id": "2",
            "_score": 1,
            "_source": {
               "id": 2,
               "childcareWorkerType": "AUXILIAIRE_PARENTALE",
               "childcareTypes": [
                  "SHARED_CHARGE",
                  "OUT_OF_SCHOOL"
               ],
               "giveBath": "EMPTY"
            }
         },
         {
            "_index": "bignibou",
            "_type": "advertisement",
            "_id": "3",
            "_score": 1,
            "_source": {
               "id": 3,
               "childcareWorkerType": "AUXILIAIRE_PARENTALE",
               "childcareTypes": [
                  "SHARED_CHARGE"
               ],
               "giveBath": "YES"
            }
         }
      ]
   }
}

我的映射确实显示了 childcareTypes 字段已被分析:

{
  "advertisement": {
    "dynamic": "false",
    "properties": {
      "id": {
        "type": "long",
        "store": "yes"
      },
      "childcareWorkerType": {
        "type": "string",
        "store": "yes",
        "index": "analyzed"
      },
      "childcareTypes": {
        "type": "string",
        "store": "yes",
        "index": "analyzed"
      },
      "giveBath": {
        "type": "string",
        "store": "yes",
        "index": "analyzed"
      }
    }
  }
}

谁能解释一下为什么我的条款查询返回 0 次点击?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    之所以会这样,是因为terms 不会分析输入。这意味着它将准确搜索SHARED_CHARGEOUT_OF_SCHOOL(大写字母)。而您将该字段设置为 "index": "analyzed",这意味着 ES 将使用 standard 分析器来索引数据。

    对于SHARED_CHARGE ES 存储shared_charge。 对于OUT_OF_SCHOOL ES 商店out_of_school

    【讨论】:

      【解决方案2】:

      我的映射确实显示了 childcareTypes 字段已被分析:

      这正是您的问题所在:该字段已被分析,但是,terms 查询直接查找未分析的术语,未分析(请参阅here)。

      更准确地说,索引值如下所示:

      shared_charge
      out_of_school
      

      你的terms查询搜索:

      SHARED_CHARGE
      OUT_OF_SCHOOL
      

      您可以像尝试此查询一样检查此行为...

      POST /bignibou/_search
      {
        "query": {
          "terms": {
            "childcareTypes": [
              "shared_charge",
              "out_of_school"
            ]
          }
        }
      }
      

      ...您会找到您的文档。

      您应该在 not_analyzed 版本的字段上使用之前的查询,或者使用来自 match 系列的查询。

      【讨论】:

      • 非常感谢你们两位的回复。如果可能的话,我会将两者都标记为已接受...
      猜你喜欢
      • 2021-07-25
      • 2014-06-02
      • 2016-05-23
      • 2020-06-12
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      相关资源
      最近更新 更多