2014-12-27 86 views
2

我很难尝试用elasticsearch构建查询。Elasticsearch如何使用通配符执行OR查询

我想查询是这样的:

WHERE field_1 is 'match' $string OR field_2 is 'wildcard_match' $string OR field_3 is 'fuzzy' $string 

所以我试图构建是这样的:

{ 
    "bool" : { 
     "should" : [ 
      { 
       "match" : { "field_1" : "testing" } 
      }, 
      { 
       "wildcard" : { "field_2" : "*testing*" } 
      }, 
      { 
       "fuzzy" : { "field_3" : "testing" } 
      } 
     ], 
     "minimum_should_match" : 1, 
    } 
} 

但这似乎返回一个错误。

任何人都可以给一个指针我应该如何看待用elasticsearch做这种OR查询?

我目前datasent:

{ 
    "_index": "twitter", 
    "_type": "tweet", 
    "_id": "1", 
    "_version": 1, 
    "found": true, 
    "_source": { 
    "field_1": "some data", 
    "field_2": "testing data", 
    "field_3": "other things" 
    } 
} 

和我的查询:

curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d ' 
"query" : { 
    "bool" : { 
     "should" : [ 
      { 
       "match" : { "field_1" : "testing" } 
      }, 
      { 
       "wildcard" : { "field_2" : "*testing*" } 
      }, 
      { 
       "fuzzy" : { "field_3" : "testing" } 
      } 
     ], 
     "minimum_should_match" : 1, 
    } 
}' 

返回此错误:

SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; 
    shardFailures {[ZmwpcILwSEyufHf-t9xQ6g][twitter][0]: 
    SearchParseException[[twitter][0]: from[-1],size[-1]: 
    Parse Failure [Failed to parse source [ 
     { 
     "query": { 
      "bool": { 
      "should": [ 
       { 
       "match": { 
        "field_1": "testing" 
       } 
       }, 
       { 
       "wildcard": { 
        "field_2": "*testing*" 
       } 
       }, 
       { 
       "fuzzy": { 
        "field_3": "testing" 
       } 
       } 
      ], 
      "minimum_should_match": 1, 
      } 
     } 
     } 
    ]]]; 
    nested: ElasticsearchParseException[Expected START_OBJECT but got VALUE_STRING null]; }{[ZmwpcILwSEyufHf-t9xQ6g][twitter][1]: 
    SearchParseException[[twitter][1]: from[-1],size[-1]: 
    Parse Failure [Failed to parse source [ 
     { 
     "query": { 
      "bool": { 
      "should": [ 
       { 
       "match": { 
        "field_1": "testing" 
       } 
       }, 
       { 
       "wildcard": { 
        "field_2": "*testing*" 
       } 
       }, 
       { 
       "fuzzy": { 
        "field_3": "testing" 
       } 
       } 
      ], 
      "minimum_should_match": 1, 
      } 
     } 
     } 
    ]]]; 
    nested: ElasticsearchParseException[Expected START_OBJECT but got VALUE_STRING null]; }{[ZmwpcILwSEyufHf-t9xQ6g][twitter][2]: 
    SearchParseException[[twitter][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [ 
    { 
     "query": { 
     "bool": { 
      "should": [ 
      { 
       "match": { 
       "field_1": "testing" 
       } 
      }, 
      { 
       "wildcard": { 
       "field_2": "*testing*" 
       } 
      }, 
      { 
       "fuzzy": { 
       "field_3": "testing" 
       } 
      } 
      ], 
      "minimum_should_match": 1, 
     } 
     } 
    } 
    ]]]; 
    nested: ElasticsearchParseException[Expected START_OBJECT but got VALUE_STRING null]; }{[ZmwpcILwSEyufHf-t9xQ6g][twitter][3]: 
    SearchParseException[[twitter][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [ 
    { 
     "query": { 
     "bool": { 
      "should": [ 
      { 
       "match": { 
       "field_1": "testing" 
       } 
      }, 
      { 
       "wildcard": { 
       "field_2": "*testing*" 
       } 
      }, 
      { 
       "fuzzy": { 
       "field_3": "testing" 
       } 
      } 
      ], 
      "minimum_should_match": 1, 
     } 
     } 
    } 
    ]]]; 
    nested: ElasticsearchParseException[Expected START_OBJECT but got VALUE_STRING null]; }{[ZmwpcILwSEyufHf-t9xQ6g][twitter][4]: 
    SearchParseException[[twitter][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [ 
    { 
     "query": { 
     "bool": { 
      "should": [ 
      { 
       "match": { 
       "field_1": "testing" 
       } 
      }, 
      { 
       "wildcard": { 
       "field_2": "*testing*" 
       } 
      }, 
      { 
       "fuzzy": { 
       "field_3": "testing" 
       } 
      } 
      ], 
      "minimum_should_match": 1, 
     } 
     } 
    } 
    ]]]; nested: ElasticsearchParseException[Expected START_OBJECT but got VALUE_STRING null]; }] 
+0

返回的错误是什么? – Jordan 2014-12-27 19:16:01

+0

@Jordan我编辑了这个问题,包括更多关于我的数据集和错误返回的信息 – 2014-12-27 19:26:04

回答

2

这是由于恶​​劣的JSON格式。 此查询的右侧JSON格式如下 -

{ // --->> This was missing 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "match": { 
      "field_1": "testing" 
      } 
     }, 
     { 
      "wildcard": { 
      "field_2": "*testing*" 
      } 
     }, 
     { 
      "fuzzy": { 
      "field_3": "testing" 
      } 
     } 
     ], 
     "minimum_should_match": 1 
    } 
    } 
} // --->> This was missing 
相关问题