为什么这个elasticsearch查询不能完全匹配字符串?

时间:2016-01-11 01:27:52

标签: elasticsearch

我有一个与它应该匹配的文档不匹配的elasticsearch查询。

以下是查询:

POST _search
{
   "query": {
      "filtered": {
         "filter": {
            "bool": {
               "must": {
                  "term": {
                     "signupCompany": "NewBay Media"
                  }
               }
            }
         }
      }
   }
}

但是,如果我将术语更改为:

"signupCompanyID": 643

然后找到正确的文件。这是我的映射:

"properties": {
      "body": {
        "type": "string"
      },
      "datetimeIndexed": {
        "type": "date",
        "format": "epoch_millis||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd"
      },
      "datetimeReceived": {
        "type": "date",
        "format": "epoch_millis||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd"
      },
      "duplicateOfEmailID": {
        "type": "long"
      },
      "enabled": {
        "type": "boolean"
      },
      "htmlBodyPath": {
        "type": "string"
      },
      "plainBodyPath": {
        "type": "string"
      },
      "secondsAgoReceived": {
        "type": "long"
      },
      "senderName": {
        "type": "string",
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed"
          }
        }
      },
      "signupCompany": {
        "type": "string"
      },
      "signupCompanyEnabled": {
        "type": "boolean"
      },
      "signupCompanyID": {
        "type": "long"
      },
      "signupCompanyInitial": {
        "type": "string"
      },
      "subject": {
        "type": "string"
      }
    }

我还有一些其他查询已经在生产中,使用datetimeReceived字段和enabled字段,但是无法使这个特定的字符串匹配起作用。

它不适用于任何signupCompany,而不仅仅是这个。

1 个答案:

答案 0 :(得分:1)

您需要将signupCompany的映射类型设置为not_analyzed,以便它不会尝试匹配部分字符串,只匹配确切的值。 - docs:https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping-intro.html

"signupCompany": {
  "type":     "string",
  "index":    "not_analyzed"
}