简体   繁体   中英

Elasticsearch ignores query string

I am using Elasticsearch to query jobs in my database. Below is the query that I am using.

The query should ask for the following :
-Query matches text, name or description
-Job must contain all of the given categories and segments

However the problem that i'm having, is that when i give a query, and add segments and categories. The query is ignored appearantly, and the request returns ALL jobs with the given segments and categories.

{
  "index": "jobs",
  "type": "job",
  "body": {
    "query": {
      "filtered": {
        "query": {
          "bool": {
            "must": [
              {
                "match": {
                  "categories": "23"
                }
              },
              {
                "match": {
                  "segments": "10"
                }
              }
            ],
            "should": [
              {
                "match": {
                  "name": "php"
                }
              },
              {
                "match": {
                  "text": "php"
                }
              },
              {
                "match": {
                  "description": "php"
                }
              }
            ]
          }
        },
        "filter": {
          "nested": {
            "path": "networks",
            "filter": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "networks.id": 1
                    }
                  },
                  {
                    "term": {
                      "networks.status.raw": "PRODUCTION"
                    }
                  },
                  {
                    "range": {
                      "networks.start": {
                        "lte": "now"
                      }
                    }
                  },
                  {
                    "range": {
                      "networks.end": {
                        "gte": "now"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    },
    "aggs": {
      "categories": {
        "terms": {
          "field": "categories"
        }
      },
      "segments": {
        "terms": {
          "field": "segments"
        }
      }
    }
  }
}

As a sidenote, i am using laravel and elasticsearch's php implementation, and the above is the json_encoded representation of the query array. (types could have been juggled)

Ok, i figured it out myself:

Replacing the should parameters in the bool query with a multi_match in the must clause will solve my problem.

{
  "index": "jobs",
  "type": "job",
  "body": {
    "query": {
      "filtered": {
        "query": {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "php",
                  "fields": [
                    "name",
                    "text",
                    "description"
                  ]
                }
              },
              {
                "match": {
                  "segments": "10"
                }
              }
            ]
          }
        },

It turned out pretty obvious because it was noted in the documentation:

The clause (query) should appear in the matching document. In a boolean query with no must clauses, one or more should clauses must match a document . The minimum number of should clauses to match can be set using the minimum_should_match parameter.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#query-dsl-bool-query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM