Elasticsearch如何使用通配符进行OR查询 [英] Elasticsearch How to do a OR query with wildcard

查看:336
本文介绍了Elasticsearch如何使用通配符进行OR查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想查询一下如下:

  WHERE field_1是'match'$ string OR field_2是'wildcard_match'$ string OR field_3是'fuzzy'$ string 
pre>

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

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

但这似乎返回一个错误。



任何人都可以给一个指针,我应该怎么看待这样的OR查询弹性搜索?



我的当前数据:

  {
_index:twitter,
_type:tweet
_id:1,
_version:1,
found:true,
_source:{
field_1 :一些数据,
field_2:测试数据,
field_3:其他东西
}
}

和我的查询:

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

返回此错误:

  SearchPhaseExecutionException [Failed to执行阶段[查询],所有分片失败; 
shardFailures {[ZmwpcILwSEyufHf-t9xQ6g] [twitter] [0]:
SearchParseException [[twitter] [0]:from [-1],size [-1]:
解析失败[无法解析源码[
{
查询:{
bool:{
should:[
{
match {
field_1:test
}
},
{
通配符:{
field_2:* tests *
}
},
{
fuzzy:{
field_3:testing
}
}
],
minimum_should_match:1,
}
}
}
]]];
嵌套:ElasticsearchParseException [预计START_OBJECT但VALUE_STRING为空]; } {[ZmwpcILwSEyufHf-t9xQ6g] [twitter] [1]:
SearchParseException [[twitter] [1]:from [-1],size [-1]:
解析失败[无法解析源[
{
查询:{
bool:{
应该:[
{
匹配:{
field_1:test
}
},
{
wildcard:{
field_2:* testing *

},
{
fuzzy:{
field_3:testing
}
}
],
minimum_should_match:1,
}
}
}
]]];
嵌套:ElasticsearchParseException [预计START_OBJECT但VALUE_STRING为空]; } {[ZmwpcILwSEyufHf-t9xQ6g] [twitter] [2]:
SearchParseException [[twitter] [2]:from [-1],size [-1]:解析失败[解析源无效[
{
query:{
bool:{
should:[
{
match:{
field_1 :test
}
},
{
wildcard:{
field_2:* testing *
}
$,
{
fuzzy:{
field_3:testing
}
}
],
minimum_should_match :1,
}
}
}
]]];
嵌套:ElasticsearchParseException [预计START_OBJECT但VALUE_STRING为空]; } {[ZmwpcILwSEyufHf-t9xQ6g] [twitter] [3]:
SearchParseException [[twitter] [3]:from [-1],size [-1]:Parse Failure [解析源无效[
{
query:{
bool:{
should:[
{
match:{
field_1 :test
}
},
{
wildcard:{
field_2:* testing *
}
$,
{
fuzzy:{
field_3:testing
}
}
],
minimum_should_match :1,
}
}
}
]]];
嵌套:ElasticsearchParseException [预计START_OBJECT但VALUE_STRING为空]; } [[ZmwpcILwSEyufHf-t9xQ6g] [twitter] [4]:
SearchParseException [[twitter] [4]:from [-1],size [-1]:解析失败[解析源无效[
{
query:{
bool:{
should:[
{
match:{
field_1 :test
}
},
{
wildcard:{
field_2:* testing *
}
$,
{
fuzzy:{
field_3:testing
}
}
],
minimum_should_match :1,
}
}
}
]]];嵌套:ElasticsearchParseException [预计START_OBJECT但已获得VALUE_STRING null]; }]


解决方案

这是由于JSON格式错误。
此查询的正确JSON格式如下 -

  {// ---&>>这是缺少
查询:{
bool:{
应该:[
{
匹配:{
field_1 :test
}
},
{
wildcard:{
field_2:* testing *
}


fuzzy:{
field_3:testing
}
}
],
minimum_should_match:1
}
}
} // --->>这是缺少


I am having a hard time trying to construct a query with elasticsearch.

I want to query something like:

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

so what I tried to construct is something like this:

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

But this seems to return an error.

Can anyone give a pointer how should I look into doing this kind of OR query with elasticsearch?

my current datasent:

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

and my query:

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,
    }
}'

returns this error:

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]; }]

解决方案

This is due to bad JSON format. The right JSON format for this query is as below -

{   // --->> 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

这篇关于Elasticsearch如何使用通配符进行OR查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆