ElasticSearch - 在一个查询中搜索分析和not_analyzed multi_field [英] ElasticSearch - Search analyzed and not_analyzed multi_field in one query
问题描述
我有一个索引字段'properties.language',值为'en sv'。该字段具有多字段映射,由两个字段组成,一个被分析(名称为language),另一个是not_analyzed(name'_exact')。
我发出单个搜索查询,而不必同时查询properties.language和properties.language._exact?
编辑: / p>
这是我的配置:
索引数据:
<
$ _ $$$$ $ b_version:1,
_score:1,
_source:{
properties:{
language:en sv
}
}
}
类型'user'的映射: / p>
{
user:{
properties:{
properties {
properties:{
language:{
type :multi_field,
fields:{
language:{
type:string,
analyzer:standard,
index:已分析
},
_exact:{
type:string,
index:not_analyzed
}
}
}
}
}
}
}
}
搜索查询:
{
query :{
filtered:{
query:{
match_all:{}
},
filter:{
bool:{
must:[{
or:[{
term:{
properties.language:en sv
}
},{
term:{
properties.language._exact :en sv
}
}]
}]
}
}
}
}
}
考虑索引语言
字段使用Elasticsearch内置的多值字段(即。数组): http://www.elasticsearch.org/guide/reference/映射/阵列类型/ 。如您所做,将 index
设置为 not_analyzed
。
索引数据时,而不是单个值'en sv'
,而是转换为 ['en','sv']
,而ES会照顾其余的。
对于查询,这使您可以执行以下操作来查找 en
和 sv
:
{
query:{
filtered:{
query:{
match_all:{}
},
filter :{
bool:{
must:[{
term:{
properties.language:en
}
},{
term:{
properties.language:sv
}
}]
}
}
}
}
}
或者更好的是找到/ code>术语 c
I have an indexed field 'properties.language' with the value 'en sv'. This field has a multi_field mapping that consists of two fields, one analyzed (name 'language'), and one that is not_analyzed (name '_exact').
How do I issue a single search query without having to query both 'properties.language' and 'properties.language._exact'?
Edit:
Here is my configuration:
Indexed data:
{
"_index": "51ded0be98035",
"_type": "user",
"_id": "WUzwcwhTRbKur7J5ZY_hgA",
"_version": 1,
"_score": 1,
"_source": {
"properties": {
"language":"en sv"
}
}
}
Mapping for type 'user':
{
"user": {
"properties": {
"properties": {
"properties": {
"language": {
"type": "multi_field",
"fields": {
"language": {
"type": "string",
"analyzer": "standard",
"index": "analyzed"
},
"_exact": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
Search query:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [{
"or": [{
"term": {
"properties.language": "en sv"
}
}, {
"term": {
"properties.language._exact": "en sv"
}
}]
}]
}
}
}
}
}
Consider indexing the language
field using Elasticsearch builtin multi-valued fields (ie. arrays) instead: http://www.elasticsearch.org/guide/reference/mapping/array-type/. As you currently do, set index
to not_analyzed
.
When indexing your data, instead of a single value 'en sv'
, pass instead ['en', 'sv']
, and ES will take care of the rest.
For querying, this gives you the ability to do the following to find items with both en
and sv
:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [{
"term": {
"properties.language": "en"
}
}, {
"term": {
"properties.language": "sv"
}
}]
}
}
}
}
}
Or even better, find greater brevity/flexibility using the terms
query/filter instead of term
: http://www.elasticsearch.org/guide/reference/query-dsl/terms-query/
这篇关于ElasticSearch - 在一个查询中搜索分析和not_analyzed multi_field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!