0

下面是我的映射:

{
  "mappings": {
    "_doc": {
      "properties": {
        "text": { 
          "type": "text",
          "fields": {
            "raw": { 
              "type":     "keyword",
              "normalizer": "case_insensitive"
            }
          }
        }
      }
    }
  }
}

设置如下所示:

{
  "settings": {
    "index": {
      "analysis" : {
        "normalizer" : {
          "case_insensitive" : {
            "filter" : "lowercase"
          }
        },
        "analyzer" : {
          "en_std" : {
            "type" : "standard",
            "stopwords" : "_english_"
          }
        }
      },
    }
  }
} 

以下是我的查询:

{
  "query": {
    "bool" : {
      "must" : {
        "query_string" : {
          "query" : "hawaii beach 2019",
          "analyze_wildcard: true,
          "fields": [
            "text"
          ]
        }
      },
    }
  }
}

下面是存储在 Elasticsearch 中的示例数据:

[
  {
     "text": "blue hawaii hotel"
  },
  {
     "text": "costa beach"
  },
  {
     "text": "white hawaii beach"
  },
  {
     "text": "nice hotel 2019"
  },
  {
     "text": " some 2019 white beach hawaii photo"
  },
  {
     "text": "hawaii vacation 2019"
  },
]

如果我的搜索词是hawaii,我会得到三个结果:

[
  {
     "text": "blue hawaii hotel"
  },
  {
     "text": "white hawaii beach"
  },
  {
     "text": " some 2019 white beach hawaii beach photo"
  },
]

如果我的搜索词是hawaii beach,我会得到四个结果,它们是:

[
  {
     "text": "blue hawaii hotel"
  },
  {
     "text": "costa beach"
  },
  {
     "text": "white hawaii beach"
  },
  {
     "text": " some 2019 white beach hawaii photo"
  },
]

如果我的搜索词是hawaii beach 2019,我会得到五个结果,它们是:

[
  {
     "text": "blue hawaii hotel"
  },
  {
     "text": "costa beach"
  },
  {
     "text": "white hawaii beach"
  },
  {
     "text": "nice hotel 2019"
  },
  {
     "text": " some 2019 white beach hawaii photo"
  },
]

这是因为每条记录都包含一个搜索文本的单词。这是有道理的,但这并不是我想要的。我希望包含最多匹配词的记录出现在搜索结果的顶部,而包含较少匹配词的记录出现在搜索结果的底部。如何在 Elasticsearch 6.8 中做到这一点?如果这不能实现,也希望只显示包含最多匹配词的记录作为搜索结果。

如果我的搜索文本是例如所需的搜索结果hawaii beach 2019

[
  {
     "text": " some 2019 white beach hawaii photo" // Contains most matching words.
  },
  {
     "text": "white hawaii beach"
  },
  {
     "text": "blue hawaii hotel" // Contains less matching words.
  },
  {
     "text": "costa beach" // Contains less matching words.
  },

  {
     "text": "nice hotel 2019" // Contains less matching words.
  },

]

或者

[
  {
     "text": " some 2019 white beach hawaii photo" // Contains most matching words
  },
]
4

2 回答 2

0

您可以修改输入查询:

hawaii AND beach AND 2019

然后,您将获得所有 3 个单词的结果。

于 2019-08-26T12:47:11.443 回答
0

我想我已经找到了一种解决方法,方法是*按以下方式将搜索字符串中的每个单词括起来。

{ 
  "query": { 
    "bool": { 
      "must": { 
        "bool": { 
          "should": { 
            "query_string": { 
              "query": "*hawaii* *beach* *2019*", 
              "fields": ["text"]
            } 
          } 
        } 
      } 
    } 
  } 
}

通过这个查询,我得到所有包含至少一个搜索字符串单词的文档。具有最匹配搜索词的文档显示在列表顶部。

于 2019-08-27T11:38:26.053 回答