Elasticsearch过滤器不适用于字符串过滤器术语

时间:2015-04-20 09:40:00

标签: filter elasticsearch

使用与下面相同的记录,我使用多种方法进行过滤以获得它,我不知道为什么有某种方式工作,某些方式不起作用。有什么我应该关注的吗

   //not work:  
     filtered: {
                 query: {"match_all": {}},
                 filter: {"term" : {  "name": "Road to Rio 2016"}
     }

  //not work:
    filtered: {
                 query: {"match_all": {}},
                 filter: {"term" : {   "isTemplate": "N"}
    }

  //work:
    filtered: {
                query: {"match_all": {}},
                filter: {"term" : { "teamId": 147}
     }

  //work:
        filtered: {
                    query: {"match_all": {}},
                    filter: {"term" : { "programId": 12615}
         }

这是我得到的第二种方式的记录

 "hits": [
      {
        "_index": "bridge_tracker_cli_v0.0.7",
        "_type": "program",
        "_id": "12615",
        "_score": null,
        "_source": {
          "programId": 12615,
          "sportId": null,
          "name": "Road to Rio 2016",
          "description": "Program Overview",
          "isTemplate": "N",
          "editedById": 2170,
          "createdById": 1491,
          "clonedFromProgramId": 12608,
          "teamId": 147,
          "organizationId": 117,
          "createdAt": "2015-02-26T07:45:50.000Z",
          "updatedAt": "2015-04-13T04:47:41.000Z"
        },
        "sort": [
          1424936750000
        ]
      },

以下是记录映射:

 "_all": {
        "index_analyzer": "nGram_analyzer",
        "search_analyzer": "whitespace_analyzer"
      },

  "properties": {
    "clonedFromProgramId": {
      "type": "long",
      "include_in_all": false
    },
    "createdAt": {
      "type": "date",
      "format": "dateOptionalTime",
      "include_in_all": false
    },
    "createdById": {
      "type": "long",
      "include_in_all": false
    },
    "createdByScope": {
      "type": "string",
      "include_in_all": false
    },
    "dateEdit": {
      "type": "date",
      "format": "dateOptionalTime",
      "include_in_all": false
    },
    "description": {
      "type": "string"
    },
    "editedById": {
      "type": "long",
      "include_in_all": false
    },
    "editedByScope": {
      "type": "string",
      "include_in_all": false
    },
    "isTemplate": {
      "type": "string"
      "include_in_all": false
    },
    "name": {
      "type": "string"
    },
    "organizationId": {
      "type": "long",
      "include_in_all": false
    },
    "programId": {
      "type": "long"
    },
    "updatedAt": {
      "type": "date",
      "format": "dateOptionalTime",
      "include_in_all": false
    }
  }

以下是分析仪:

 "analysis": {
        "filter": {
          "nGram_filter": {
             "type": "nGram",
             "min_gram": 1,
             "max_gram": 20,
             "token_chars": [
                "letter",
                "digit",
                "punctuation",
                "symbol"
             ]
          }
       },
       "analyzer": {
          "nGram_analyzer": {
             "type": "custom",
             "tokenizer": "whitespace",
             "filter": [
                "lowercase",
                "asciifolding",
                "nGram_filter"
             ]
          },
          "whitespace_analyzer": {
             "type": "custom",
             "tokenizer": "whitespace",
             "filter": [
                "lowercase",
                "asciifolding"
             ]
          }

2 个答案:

答案 0 :(得分:2)

如果要对字符串使用术语过滤器,请确保将索引更改为not_analyzed。但在这种情况下,全文搜索将无法在此字段上运行。要确保,过滤器和全文搜索都可以将此字段更改为多字段。 例如,在名称的情况下,将映射更改为

"name":{
          "type": "string",
          "fields": {
            "not_analyzed":{
              "index": "not_analyzed"
            }
          }
        }

并使用name.not_analyzed进行过滤。

答案 1 :(得分:0)

对我来说,这有助于将字段类型设置为keyword而不是string