jjl
jjl

Reputation: 31

Can't get wild card query to work on multiple fields in elasticsearch

I'm trying to get wild card to work on multiple fields in elastic search but it doesn't seem to work. When I use this it just returns result that is same as submitting an empty query. Here is my query :

{
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "ratingCount": {
        "order": "desc"
      }
    },
    {
      "avgRating": {
        "order": "desc"
      }
    }
  ],
  "from": 20,
  "size": 20,
  "query": {
    "bool": {
      "filter": {
        "term": {
          "cities": "59c95a090338d4fe4aea6af8"
        }
      },
      "should": [
        {
          "wildcard": {
            "firstName": "Mich*"
          }
        },
        {
          "wildcard": {
            "lastName": "Mich*"
          }
        }
      ]
    }
  }
}

Upvotes: 0

Views: 61

Answers (1)

Piotr Pradzynski
Piotr Pradzynski

Reputation: 4535

Maybe the from property is your problem? Take a look on the From/Size documentation. From 20 means that the offset from the first result is 20. This means that you need to have at least 21 results overall to see one record in your query.

Take a look on example. First put three records:

PUT index/_doc/1
{
  "cities": "59c95a090338d4fe4aea6af8",
  "firstName": "Michael",
  "lastName": "Jordan"
}

PUT index/_doc/2
{
  "cities": "59c95a090338d4fe4aea6af8",
  "firstName": "Tomasz",
  "lastName": "Michalowicz"
}

PUT index/_doc/3
{
  "cities": "59c95a090338d4fe4aea6af8",
  "firstName": "Bartosz",
  "lastName": "Michalski"
}

And then search with your query and from set to 3:

GET _search
{
  "from": 3,
  "query": {
    "bool": {
      "filter": {
        "term": {
          "cities": "59c95a090338d4fe4aea6af8"
        }
      },
      "should": [
        {
          "wildcard": {
            "firstName": "Mich*"
          }
        },
        {
          "wildcard": {
            "lastName": "Mich*"
          }
        }
      ]
    }
  }
}

Response will be:

{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 10,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  }
}

You can see that there is no visible hits but total is equal to 3.

Then change from to 2 and query again:

GET _search
{
  "from": 2,
  "query": {
    "bool": {
      "filter": {
        "term": {
          "cities": "59c95a090338d4fe4aea6af8"
        }
      },
      "should": [
        {
          "wildcard": {
            "firstName": "Mich*"
          }
        },
        {
          "wildcard": {
            "lastName": "Mich*"
          }
        }
      ]
    }
  }
}

The answer is:

{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 10,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": [
      {
        "_index": "twitter",
        "_type": "_doc",
        "_id": "3",
        "_score": 0,
        "_source": {
          "cities": "59c95a090338d4fe4aea6af8",
          "firstName": "Bartosz",
          "lastName": "Michalski"
        }
      }
    ]
  }
}

Then change from to 0 and query again:

GET _search
{
  "from": 0,
  "query": {
    "bool": {
      "filter": {
        "term": {
          "cities": "59c95a090338d4fe4aea6af8"
        }
      },
      "should": [
        {
          "wildcard": {
            "firstName": "Mich*"
          }
        },
        {
          "wildcard": {
            "lastName": "Mich*"
          }
        }
      ]
    }
  }
}

Response:

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 10,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": [
      {
        "_index": "twitter",
        "_type": "_doc",
        "_id": "2",
        "_score": 0,
        "_source": {
          "cities": "59c95a090338d4fe4aea6af8",
          "firstName": "Tomasz",
          "lastName": "Michalowicz"
        }
      },
      {
        "_index": "twitter",
        "_type": "_doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "cities": "59c95a090338d4fe4aea6af8",
          "firstName": "Michael",
          "lastName": "Jordan"
        }
      },
      {
        "_index": "twitter",
        "_type": "_doc",
        "_id": "3",
        "_score": 0,
        "_source": {
          "cities": "59c95a090338d4fe4aea6af8",
          "firstName": "Bartosz",
          "lastName": "Michalski"
        }
      }
    ]
  }
}

Upvotes: 1

Related Questions