如何为普通搜索查询添加模糊性

How to add fuzziness for normal search query

查询如下

{"from": 0, "size": 1000, "query": {"bool": {"must": {"query_string": {"query": "Love"}}}}}

如果我也通过 Live 那么我还需要获得 Love

的搜索结果

映射

{'movie_data': {'mappings': {'properties': {'Creative Type': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'Director': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'Distributor': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'IMDB Rating': {'type': 'float'}, 'IMDB Votes': {'type': 'long'}, 'MPAA Rating': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'Major Genre': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'Production Budget': {'type': 'long'}, 'Release Date': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'Rotten Tomatoes Rating': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'Running Time min': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'Source': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'Title': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'US DVD Sales': {'type': 'text', 'fields': {'keyword': {'type': 'keyword', 'ignore_above': 256}}}, 'US Gross': {'type': 'long'}, 'Worldwide Gross': {'type': 'long'}, 'id': {'type': 'long'}}}}}

match_all查询结果如下

{'took': 8, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 30, 'relation': 'eq'}, 'max_score': 1.0, 'hits': [{'_index': 'new_index', '_type': '_doc', '_id': '0', '_score': 1.0, '_source': {'id': 0, 'Title': 'The Land Girls', 'US Gross': 146083, 'Worldwide Gross': 146083, 'US DVD Sales': None, 'Production Budget': 8000000, 'Release Date': 'Jun 12 1998', 'MPAA Rating': 'R', 'Running Time min': None, 'Distributor': 'Gramercy', 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': None, 'IMDB Rating': 6.1, 'IMDB Votes': 1071}}, {'_index': 'new_index', '_type': '_doc', '_id': '1', '_score': 1.0, '_source': {'id': 1, 'Title': 'First Love, Last Rites', 'US Gross': 10876, 'Worldwide Gross': 10876, 'US DVD Sales': None, 'Production Budget': 300000, 'Release Date': 'Aug 07 1998', 'MPAA Rating': 'R', 'Running Time min': None, 'Distributor': 'Strand', 'Source': None, 'Major Genre': 'Drama', 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': None, 'IMDB Rating': 6.9, 'IMDB Votes': 207}}, {'_index': 'new_index', '_type': '_doc', '_id': '2', '_score': 1.0, '_source': {'id': 2, 'Title': 'I Married a Strange Person', 'US Gross': 203134, 'Worldwide Gross': 203134, 'US DVD Sales': None, 'Production Budget': 250000, 'Release Date': 'Aug 28 1998', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': 'Lionsgate', 'Source': None, 'Major Genre': 'Comedy', 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': None, 'IMDB Rating': 6.8, 'IMDB Votes': 865}}, {'_index': 'new_index', '_type': '_doc', '_id': '3', '_score': 1.0, '_source': {'id': 3, 'Title': "Let's Talk About Sex", 'US Gross': 373615, 'Worldwide Gross': 373615, 'US DVD Sales': None, 'Production Budget': 300000, 'Release Date': 'Sep 11 1998', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': 'Fine Line', 'Source': None, 'Major Genre': 'Comedy', 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': 13, 'IMDB Rating': None, 'IMDB Votes': None}}, {'_index': 'new_index', '_type': '_doc', '_id': '4', '_score': 1.0, '_source': {'id': 4, 'Title': 'Slam', 'US Gross': 1009819, 'Worldwide Gross': 1087521, 'US DVD Sales': None, 'Production Budget': 1000000, 'Release Date': 'Oct 09 1998', 'MPAA Rating': 'R', 'Running Time min': None, 'Distributor': 'Trimark', 'Source': 'Original Screenplay', 'Major Genre': 'Drama', 'Creative Type': 'Contemporary Fiction', 'Director': None, 'Rotten Tomatoes Rating': 62, 'IMDB Rating': 3.4, 'IMDB Votes': 165}}, {'_index': 'new_index', '_type': '_doc', '_id': '5', '_score': 1.0, '_source': {'id': 5, 'Title': 'Mississippi Mermaid', 'US Gross': 24551, 'Worldwide Gross': 2624551, 'US DVD Sales': None, 'Production Budget': 1600000, 'Release Date': 'Jan 15 1999', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': 'MGM', 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': None, 'IMDB Rating': None, 'IMDB Votes': None}}, {'_index': 'new_index', '_type': '_doc', '_id': '6', '_score': 1.0, '_source': {'id': 6, 'Title': 'Following', 'US Gross': 44705, 'Worldwide Gross': 44705, 'US DVD Sales': None, 'Production Budget': 6000, 'Release Date': 'Apr 04 1999', 'MPAA Rating': 'R', 'Running Time min': None, 'Distributor': 'Zeitgeist', 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': 'Christopher Nolan', 'Rotten Tomatoes Rating': None, 'IMDB Rating': 7.7, 'IMDB Votes': 15133}}, {'_index': 'new_index', '_type': '_doc', '_id': '7', '_score': 1.0, '_source': {'id': 7, 'Title': 'Foolish', 'US Gross': 6026908, 'Worldwide Gross': 6026908, 'US DVD Sales': None, 'Production Budget': 1600000, 'Release Date': 'Apr 09 1999', 'MPAA Rating': 'R', 'Running Time min': None, 'Distributor': 'Artisan', 'Source': 'Original Screenplay', 'Major Genre': 'Comedy', 'Creative Type': 'Contemporary Fiction', 'Director': None, 'Rotten Tomatoes Rating': None, 'IMDB Rating': 3.8, 'IMDB Votes': 353}}, {'_index': 'new_index', '_type': '_doc', '_id': '8', '_score': 1.0, '_source': {'id': 8, 'Title': 'Pirates', 'US Gross': 1641825, 'Worldwide Gross': 6341825, 'US DVD Sales': None, 'Production Budget': 40000000, 'Release Date': 'Jul 01 1986', 'MPAA Rating': 'R', 'Running Time min': None, 'Distributor': None, 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': 'Roman Polanski', 'Rotten Tomatoes Rating': 25, 'IMDB Rating': 5.8, 'IMDB Votes': 3275}}, {'_index': 'new_index', '_type': '_doc', '_id': '9', '_score': 1.0, '_source': {'id': 9, 'Title': 'Duel in the Sun', 'US Gross': 20400000, 'Worldwide Gross': 20400000, 'US DVD Sales': None, 'Production Budget': 6000000, 'Release Date': 'Dec 31 2046', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': None, 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': 86, 'IMDB Rating': 7, 'IMDB Votes': 2906}}, {'_index': 'new_index', '_type': '_doc', '_id': '10', '_score': 1.0, '_source': {'id': 10, 'Title': 'Tom Jones', 'US Gross': 37600000, 'Worldwide Gross': 37600000, 'US DVD Sales': None, 'Production Budget': 1000000, 'Release Date': 'Oct 07 1963', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': None, 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': 81, 'IMDB Rating': 7, 'IMDB Votes': 4035}}, {'_index': 'new_index', '_type': '_doc', '_id': '11', '_score': 1.0, '_source': {'id': 11, 'Title': 'Oliver!', 'US Gross': 37402877, 'Worldwide Gross': 37402877, 'US DVD Sales': None, 'Production Budget': 10000000, 'Release Date': 'Dec 11 1968', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': 'Sony Pictures', 'Source': None, 'Major Genre': 'Musical', 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': 84, 'IMDB Rating': 7.5, 'IMDB Votes': 9111}}, {'_index': 'new_index', '_type': '_doc', '_id': '12', '_score': 1.0, '_source': {'id': 12, 'Title': 'To Kill A Mockingbird', 'US Gross': 13129846, 'Worldwide Gross': 13129846, 'US DVD Sales': None, 'Production Budget': 2000000, 'Release Date': 'Dec 25 1962', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': 'Universal', 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': 97, 'IMDB Rating': 8.4, 'IMDB Votes': 82786}}, {'_index': 'new_index', '_type': '_doc', '_id': '13', '_score': 1.0, '_source': {'id': 13, 'Title': 'Tora, Tora, Tora', 'US Gross': 29548291, 'Worldwide Gross': 29548291, 'US DVD Sales': None, 'Production Budget': 25000000, 'Release Date': 'Sep 23 1970', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': None, 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': 'Richard Fleischer', 'Rotten Tomatoes Rating': None, 'IMDB Rating': None, 'IMDB Votes': None}}, {'_index': 'new_index', '_type': '_doc', '_id': '14', '_score': 1.0, '_source': {'id': 14, 'Title': 'Hollywood Shuffle', 'US Gross': 5228617, 'Worldwide Gross': 5228617, 'US DVD Sales': None, 'Production Budget': 100000, 'Release Date': 'Mar 01 1987', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': None, 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': 87, 'IMDB Rating': 6.8, 'IMDB Votes': 1532}}, {'_index': 'new_index', '_type': '_doc', '_id': '15', '_score': 1.0, '_source': {'id': 15, 'Title': 'Over the Hill to the Poorhouse', 'US Gross': 3000000, 'Worldwide Gross': 3000000, 'US DVD Sales': None, 'Production Budget': 100000, 'Release Date': 'Sep 17 2020', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': None, 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': None, 'IMDB Rating': None, 'IMDB Votes': None}}, {'_index': 'new_index', '_type': '_doc', '_id': '16', '_score': 1.0, '_source': {'id': 16, 'Title': 'Wilson', 'US Gross': 2000000, 'Worldwide Gross': 2000000, 'US DVD Sales': None, 'Production Budget': 5200000, 'Release Date': 'Aug 01 2044', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': None, 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': None, 'IMDB Rating': 7, 'IMDB Votes': 451}}, {'_index': 'new_index', '_type': '_doc', '_id': '17', '_score': 1.0, '_source': {'id': 17, 'Title': 'Darling Lili', 'US Gross': 5000000, 'Worldwide Gross': 5000000, 'US DVD Sales': None, 'Production Budget': 22000000, 'Release Date': 'Jan 01 1970', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': None, 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': 'Blake Edwards', 'Rotten Tomatoes Rating': None, 'IMDB Rating': 6.1, 'IMDB Votes': 858}}, {'_index': 'new_index', '_type': '_doc', '_id': '18', '_score': 1.0, '_source': {'id': 18, 'Title': 'The Ten Commandments', 'US Gross': 80000000, 'Worldwide Gross': 80000000, 'US DVD Sales': None, 'Production Budget': 13500000, 'Release Date': 'Oct 05 1956', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': None, 'Source': None, 'Major Genre': None, 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': 90, 'IMDB Rating': 2.5, 'IMDB Votes': 1677}}, {'_index': 'new_index', '_type': '_doc', '_id': '19', '_score': 1.0, '_source': {'id': 19, 'Title': '12 Angry Men', 'US Gross': 0, 'Worldwide Gross': 0, 'US DVD Sales': None, 'Production Budget': 340000, 'Release Date': 'Apr 13 1957', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': 'United Artists', 'Source': 'Original Screenplay', 'Major Genre': 'Drama', 'Creative Type': None, 'Director': 'Sidney Lumet', 'Rotten Tomatoes Rating': None, 'IMDB Rating': 8.9, 'IMDB Votes': 119101}}, {'_index': 'new_index', '_type': '_doc', '_id': '20', '_score': 1.0, '_source': {'id': 20, 'Title': 'Twelve Monkeys', 'US Gross': 57141459, 'Worldwide Gross': 168841459, 'US DVD Sales': None, 'Production Budget': 29000000, 'Release Date': 'Dec 27 1995', 'MPAA Rating': 'R', 'Running Time min': None, 'Distributor': 'Universal', 'Source': 'Based on Short Film', 'Major Genre': 'Drama', 'Creative Type': 'Science Fiction', 'Director': 'Terry Gilliam', 'Rotten Tomatoes Rating': None, 'IMDB Rating': 8.1, 'IMDB Votes': 169858}}, {'_index': 'new_index', '_type': '_doc', '_id': '21', '_score': 1.0, '_source': {'id': 21, 'Title': 1776, 'US Gross': 0, 'Worldwide Gross': 0, 'US DVD Sales': None, 'Production Budget': 4000000, 'Release Date': 'Nov 09 1972', 'MPAA Rating': 'PG', 'Running Time min': None, 'Distributor': 'Sony/Columbia', 'Source': 'Based on Play', 'Major Genre': 'Drama', 'Creative Type': 'Historical Fiction', 'Director': None, 'Rotten Tomatoes Rating': 57, 'IMDB Rating': 7, 'IMDB Votes': 4099}}, {'_index': 'new_index', '_type': '_doc', '_id': '22', '_score': 1.0, '_source': {'id': 22, 'Title': 1941, 'US Gross': 34175000, 'Worldwide Gross': 94875000, 'US DVD Sales': None, 'Production Budget': 32000000, 'Release Date': 'Dec 14 1979', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': 'Universal', 'Source': 'Original Screenplay', 'Major Genre': 'Comedy', 'Creative Type': 'Historical Fiction', 'Director': 'Steven Spielberg', 'Rotten Tomatoes Rating': 33, 'IMDB Rating': 5.6, 'IMDB Votes': 13364}}, {'_index': 'new_index', '_type': '_doc', '_id': '23', '_score': 1.0, '_source': {'id': 23, 'Title': 'Chacun sa nuit', 'US Gross': 18435, 'Worldwide Gross': 18435, 'US DVD Sales': None, 'Production Budget': 1900000, 'Release Date': 'Jun 29 2007', 'MPAA Rating': 'Not Rated', 'Running Time min': None, 'Distributor': 'Strand', 'Source': 'Original Screenplay', 'Major Genre': 'Thriller/Suspense', 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': None, 'IMDB Rating': 6.3, 'IMDB Votes': 365}}, {'_index': 'new_index', '_type': '_doc', '_id': '24', '_score': 1.0, '_source': {'id': 24, 'Title': '2001: A Space Odyssey', 'US Gross': 56700000, 'Worldwide Gross': 68700000, 'US DVD Sales': None, 'Production Budget': 10500000, 'Release Date': 'Apr 02 1968', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': None, 'Source': 'Original Screenplay', 'Major Genre': None, 'Creative Type': 'Science Fiction', 'Director': 'Stanley Kubrick', 'Rotten Tomatoes Rating': 96, 'IMDB Rating': 8.4, 'IMDB Votes': 160342}}, {'_index': 'new_index', '_type': '_doc', '_id': '25', '_score': 1.0, '_source': {'id': 25, 'Title': '20,000 Leagues Under the Sea', 'US Gross': 28200000, 'Worldwide Gross': 28200000, 'US DVD Sales': None, 'Production Budget': 5000000, 'Release Date': 'Dec 23 1954', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': 'Walt Disney Pictures', 'Source': 'Based on Book/Short Story', 'Major Genre': 'Adventure', 'Creative Type': None, 'Director': 'Richard Fleischer', 'Rotten Tomatoes Rating': 92, 'IMDB Rating': None, 'IMDB Votes': None}}, {'_index': 'new_index', '_type': '_doc', '_id': '26', '_score': 1.0, '_source': {'id': 26, 'Title': '20,000 Leagues Under the Sea', 'US Gross': 8000000, 'Worldwide Gross': 8000000, 'US DVD Sales': None, 'Production Budget': 200000, 'Release Date': 'Dec 24 2016', 'MPAA Rating': None, 'Running Time min': None, 'Distributor': None, 'Source': 'Based on Book/Short Story', 'Major Genre': 'Adventure', 'Creative Type': None, 'Director': None, 'Rotten Tomatoes Rating': None, 'IMDB Rating': None, 'IMDB Votes': None}}, {'_index': 'new_index', '_type': '_doc', '_id': '27', '_score': 1.0, '_source': {'id': 27, 'Title': '24 7: Twenty Four Seven', 'US Gross': 72544, 'Worldwide Gross': 72544, 'US DVD Sales': None, 'Production Budget': 2000000, 'Release Date': 'Apr 15 1998', 'MPAA Rating': 'R', 'Running Time min': None, 'Distributor': 'October Films', 'Source': 'Original Screenplay', 'Major Genre': 'Comedy', 'Creative Type': None, 'Director': 'Shane Meadows', 'Rotten Tomatoes Rating': None, 'IMDB Rating': 6.9, 'IMDB Votes': 1417}}, {'_index': 'new_index', '_type': '_doc', '_id': '28', '_score': 1.0, '_source': {'id': 28, 'Title': 'Twin Falls Idaho', 'US Gross': 985341, 'Worldwide Gross': 1027228, 'US DVD Sales': None, 'Production Budget': 500000, 'Release Date': 'Jul 30 1999', 'MPAA Rating': 'R', 'Running Time min': None, 'Distributor': 'Sony Pictures Classics', 'Source': 'Original Screenplay', 'Major Genre': 'Drama', 'Creative Type': 'Contemporary Fiction', 'Director': 'Michael Polish', 'Rotten Tomatoes Rating': 77, 'IMDB Rating': 7.1, 'IMDB Votes': 2810}}, {'_index': 'new_index', '_type': '_doc', '_id': '29', '_score': 1.0, '_source': {'id': 29, 'Title': 'Three Kingdoms: Resurrection of the Dragon', 'US Gross': 0, 'Worldwide Gross': 22139590, 'US DVD Sales': None, 'Production Budget': 20000000, 'Release Date': 'Apr 03 2008', 'MPAA Rating': 'R', 'Running Time min': None, 'Distributor': None, 'Source': 'Based on Book/Short Story', 'Major Genre': 'Action', 'Creative Type': 'Historical Fiction', 'Director': None, 'Rotten Tomatoes Rating': None, 'IMDB Rating': None, 'IMDB Votes': None}}]}}

您可以将 fuzziness parameter 添加到您的查询中,如下所示:

{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "must": {
        "query_string": {
          "query": "Germany",
          "fuzziness": "auto"           <--- add this
        }
      }
    }
  }
}

对于查询字符串,您需要显式使用~operator。试试下面的查询

{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "must": {
        "query_string": {
          "query": "live~",         // note this
          "fuzziness": "auto"     
        }
      }
    }
  }
}

更新 1:

{
  "from": 0,
  "size": 1000,
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "query": "live~",
            "fuzziness": "auto"
          }
        },
        {
          "query_string": {
            "query": "lave~",
            "fuzziness": "auto"
          }
        }
      ]
    }
  }
}