2017-05-16 9 views
0

私はこの文書を持っている:、それは安全ではない含まれているすべての結果を返しますElasticSearchクエリフィールドに値がありますか?

{ 
    "query":{ 
    "query_string": { 
     "query": "unsafe" 
    } 
    } 
} 

しかし:私はこのクエリを実行する場合、私は、contributing_factor_vehicleはテキストunsafeが含まれているフィールドすべてのそれらを取得したい

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": {...}, 
    "hits": { 
    "total": 20584, 
    "max_score": 8.143582, 
    "hits": [ 
     { 
     "_index": "nyc_visionzero", 
     "_type": "logs", 
     "_id": "AVwMozs3iTTcr81oIxfl", 
     "_score": 8.143582, 
     "_source": { 
      "date": "02/12/2017", 
      "number_of_motorist_injured": 2, 
      "contributing_factor_vehicle": "Unsafe Speed", 
     } 
     ... 

フィールドcontributing_factor_vehicleに限定したい。

更新マッピング

{ 
    "nyc_visionzero": { 
    "mappings": { 
     "logs": { 
     "_all": { 
      "enabled": true 
     }, 
     "dynamic_templates": [ 
      { 
      "string_fields": { 
       "match": "*", 
       "match_mapping_type": "string", 
       "mapping": { 
       "fields": { 
        "raw": { 
        "ignore_above": 256, 
        "type": "keyword" 
        } 
       }, 
       "omit_norms": true, 
       "type": "text" 
       } 
      } 
      } 
     ], 
     "properties": { 
      "@timestamp": { 
      "type": "date" 
      }, 
      "@version": { 
      "type": "keyword" 
      }, 
      "borough": { 
      "type": "keyword" 
      }, 
      "contributing_factor_vehicle": { 
      "type": "keyword" 
      }, 
      "coords": { 
      "type": "geo_point" 
      }, 
      "cross_street_name": { 
      "type": "keyword" 
      }, 
      "date": { 
      "type": "text", 
      "norms": false, 
      "fields": { 
       "raw": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "host": { 
      "type": "text", 
      "norms": false, 
      "fields": { 
       "raw": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "hour_of_day": { 
      "type": "integer" 
      }, 
      "intersection": { 
      "type": "keyword" 
      }, 
      "latitude": { 
      "type": "text", 
      "norms": false, 
      "fields": { 
       "raw": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "location": { 
      "type": "text", 
      "norms": false, 
      "fields": { 
       "raw": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "longitude": { 
      "type": "text", 
      "norms": false, 
      "fields": { 
       "raw": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "message": { 
      "type": "text", 
      "norms": false, 
      "fields": { 
       "raw": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "number_of_cyclist_injured": { 
      "type": "long" 
      }, 
      "number_of_cyclist_killed": { 
      "type": "long" 
      }, 
      "number_of_motorist_injured": { 
      "type": "long" 
      }, 
      "number_of_motorist_killed": { 
      "type": "long" 
      }, 
      "number_of_pedestrians_injured": { 
      "type": "long" 
      }, 
      "number_of_pedestrians_killed": { 
      "type": "long" 
      }, 
      "number_of_persons_injured": { 
      "type": "long" 
      }, 
      "number_of_persons_killed": { 
      "type": "long" 
      }, 
      "number_persons_impacted": { 
      "type": "long" 
      }, 
      "off_street_name": { 
      "type": "keyword" 
      }, 
      "on_street_name": { 
      "type": "keyword" 
      }, 
      "query": { 
      "properties": { 
       "match_all": { 
       "type": "object" 
       } 
      } 
      }, 
      "tags": { 
      "type": "text", 
      "norms": false, 
      "fields": { 
       "raw": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "time": { 
      "type": "text", 
      "norms": false, 
      "fields": { 
       "raw": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "unique_key": { 
      "type": "text", 
      "norms": false, 
      "fields": { 
       "raw": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "vehicle_type": { 
      "type": "text", 
      "norms": false, 
      "fields": { 
       "raw": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      }, 
      "zip_code": { 
      "type": "text", 
      "norms": false, 
      "fields": { 
       "raw": { 
       "type": "keyword", 
       "ignore_above": 256 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

答えて

0

contributing_factor_vehicleフィールドは、値がanalyzedを取得せず、そのまま保存されることを意味する、タイプkeywordであるので。

これを解決するには2つの方法があります。どちらのクエリの下に使用して完全一致の検索:

"query": { 
    "term": { 
     "contributing_factor_vehicle": { 
      "value": "Unsafe Speed" <--- Exact Term (check case) 
     } 
     } 
    } 

をそれとも、大文字小文字を区別しない検索がES 5ではデフォルトであるマルチフィールドとしてcontributing_factor_vehicleを作成したくない場合、またはバージョンが< 5.x

であれば、あなたはそれを自分で作成した場合
POST index 
{ 
    "mappings": { 
    "type": { 
    "properties": { 
     "contributing_factor_vehicle": { 
      "type": "string", 
      "fields": { 
       "raw": { 
       "type": "string", 
       "index": "not_analyzed" 
       } 
      } 
     } 
     } 
     } 
    }} 

次に、このようにクエリを実行できます。 : "{contributing_factor_vehicle}" `と` "default_field":

{ 
    "query":{ 
     "query_string": { 
     "query": "unsafe", 
     "default_field" : "contributing_factor_vehicle" 

    } 
    } 
} 
+0

Iは ' "default_field" 追加しようとした "contributing_factor_vehicleを"'と私は ' "ヒット" を得る:{ "合計":0、 「max_score ":null、 "ヒット ":[] } – commonSenseCode

+0

マッピングを共有できますか? 'GET index/type/_mapping' – Richa

+0

元の投稿の更新をご覧ください – commonSenseCode

関連する問題