gpt4 book ai didi

php - Elasticsearch 如何使用通配符进行 OR 查询

转载 作者:行者123 更新时间:2023-11-29 02:55:14 27 4
gpt4 key购买 nike

我很难尝试使用 elasticsearch 构建查询。

我想查询类似的东西:

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 查询吗?

我当前的数据发送:

{
"_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]; }]

最佳答案

这是由于错误的 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

关于php - Elasticsearch 如何使用通配符进行 OR 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27670573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com