douchuifk90315 2014-12-27 19:05
浏览 111
已采纳

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

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]; }]
  • 写回答

1条回答 默认 最新

  • douji6735 2014-12-29 04:43
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退