简体   繁体   中英

Can't configure elasticsearch

I enter the word "Booking" into the search, and already starting from the 5th result it returns "Cooking", but there are still results with the word "Booking", they contain it in the line.

$this->aQuery['query']['bool']['must'][]['multi_match'] = [
   'type' => 'cross_fields',
   'query' => 'Booking',
   'fields' => ['prod_name', 'prod_prefix'],
   'operator' => 'or'
];

I need results containing only the word "Booking". But if you enter "Travel Booking", then the results may contain both "Travel" and "Booking".

To get an exact match, use the term query

Match uses the, so called, analyzers to transform your search and find all matching words. Terms check for exact match on the search strings, so searching for Booking should not return documents that contain "Travel Booking".

As explained on the page:

By default, Elasticsearch changes the values of text fields during analysis. For example, the default standard analyzer changes text field values as follows:

  • Removes most punctuation
  • Divides the remaining content into individual words, called tokens
  • Lowercases the tokens

The term query does not analyze the search term. The term query only searches for the exact term you provide. This means the term query may return poor or no results when searching text fields.

Also note that you need to include a Keyword type in your index mappings

"prod_name": {
    "type":  "text",
    "fields": {
      "raw": { 
        "type":  "keyword"
      }
    }
  }

Then when you search for your word, you need to use the Keyword field name:

"term": {
  "prod_name.raw": {
    "value": "Booking",
    "boost": 1.0
  }
}

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