elasticsearch - 字词按not_analysed字符串列表过滤

标签 elasticsearch

假设有一个这样的映射:

"keywords": {
  "type": "string",
  "fields": {
    "raw": {
      "type": "string",
      "index": "not_analyzed"
    }
  }
},

我不会得到这样的查询:
Retrive all of documents that contain(exactly and phrase) "Blah Mlah" AND "Baw"

为了遵循term filters的elasticsearch文档,我尝试这样:
    'query': {
        'filtered': {
            'filter': {
                'bool': {
                    'must': {
                          [
                             {
                                "terms": {
                                    "keywords": ["Blah Mlah", "Baw"],
                                    "execution": "and"
                                }
                             }
                          ]
                        }
                     }
                },
            },
        },
    },

但是“execution:and”不起作用,它将像or一样返回。
我也试过这个:
 [
   {
      "term": {
          "keywords": "Blah Mlah"
      }
   },
   {
      "term": {
          "keywords": "Baw"
      }
   }
]

但是当我不想搜索"Blah Mlah"这样的两个单词关键字时,它不适用于关键字字段。怎么办

最佳答案

该查询应针对keywords.raw,因为这是尚未分析的multi-field

1.x中的示例查询:

 put test 

  put test/test/_mapping
{
   "properties": {
      "keywords": {
         "type": "string",
         "fields": {
            "raw": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}

 put test/test/1
  {
    "keywords" : [ "Baw", "Blah Mlah"]
  }
put test/test/2
  {
    "keywords" : ["Baw"]
  }


post test/test/_search 
   {
       "query": {
          "filtered": {
             "filter": {
                "bool": {
                   "must": [
                      {
                         "terms": {
                            "keywords.raw": [
                               "Blah Mlah",
                               "Baw"
                            ],
                            "execution": "and"
                         }
                      }
                   ]
                }
             }
          }
       }
    }

结果:
"hits": {
  "total": 1,
  "max_score": 1,
  "hits": [
     {
        "_index": "test",
        "_type": "test",
        "_id": "1",
        "_score": 1,
        "_source": {
           "keywords": [
              "Baw",
              "Blah Mlah"
           ]
        }
     }
  ]

}

在2.x中的示例查询:
{
   "query": {
      "bool": {
         "filter": [
            {
               "terms": {
                  "keywords.raw": [
                     "Blah Mlah"
                  ]
               }
            },
            {
               "terms": {
                  "keywords.raw": [
                     "Baw"
                  ]
               }
            }
         ]
      }
   }
}

关于elasticsearch - 字词按not_analysed字符串列表过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38038637/

相关文章:

node.js - 为什么 ElasticSearch 匹配查询返回所有结果?

elasticsearch - Elasticsearch 匹配查询,逗号值不起作用

mysql - 通过 ElasticSearch 搜索时如何保持规范化模型?

java - 查找elasticsearch聚合计数

mongodb - 使用Mongo-Connector从Mongo DB导入到诸如ElasticSearch/Solr之类的目标系统时,如何排除文档字段?

node.js - NodeJs + PostgreSQL + ElasticSearch

elasticsearch - 如何在 Elasticsearch 中必须或必须使用多个匹配短语?

elasticsearch - 如何在 Elasticsearch 中强制执行必填字段?

elasticsearch - Elasticsearch轮胎 gem 。如何搜索 '&',以及我必须按顺序使用哪些分析器索引 '&'?

elasticsearch - 如何在 logstash.conf 文件中创建多个索引?