在Elasticsearch中的数组中进行精确的字符串搜索

问题描述 投票:1回答:2

我想在数组中搜索确切的字符串。我在ES中的数据如下:

         { category": [
              "abc test"
           ],
           "es_flag": false,
           "bullet_points": [],
           "content": "",
           "description": false }

我有多个类别,如“abc test”,“new abc test”等...

我正在尝试下面的查询,但我得到多个类别的结果,我正在搜索“abc测试”,但“新的abc测试”类别也在结果中。

    {
    "from": 0,
    "size": 30,
    "query": {
        "bool" : {
            "must": [
                { "match_phrase": { "category": "abc test" } }
            ]
        }
    },
    "sort": [ { "createdAt": { "order": "desc" } } ]
}

帮助将不胜感激。

arrays string elasticsearch
2个回答
0
投票

我假设你正在使用默认分析器。在这种情况下,针对match_phrase"field": "abc test"将匹配所有具有相邻abc test标记字段的文档,包括:

  • new abc test
  • abc test new
  • foo abc test bar

它不匹配:

  • abc new test - 查询令牌不相邻
  • test abc - 查询令牌是相邻的,但顺序错误

实际上有什么帮助你在你的领域使用keyword分析器(你需要从头开始构建新的索引或更新你的映射)。如果你是从scrach构建的:

curl -XPUT http://localhost:9200/my_index -d '
{
  "mappings": {
    "categories": {
      "properties": {
        "category": {
          "type": "text",
          "analyzer": "keyword"
        }
      }
    }
  }
}'

然后你需要使用简单的查询,例如像这样(matchterm会这样做):

curl -XGET http://localhost:9200/my_index/_search -d '
{
    "query": {
        "match" : {
            "message" : "abc test"
        }
    }
}'

0
投票

我的弹性搜索版本是6.0.1。我正在使用这种方法:

GET <your index>/_search
{
  "query": {
    "bool": {
      "must": [{
        "query_string": {
          "query": "category:abc OR category:test"
        }
      }]
    }    
  },
  "sort":[{"createdAt": {
    "order": "desc"
  }}]
}
© www.soinside.com 2019 - 2024. All rights reserved.