有这个映射:
curl -XPUT 'localhost:9200/testindex?pretty=true' -d '{
"mappings": {
"items": {
"dynamic": "strict",
"properties" : {
"title" : { "type": "string" },
"body" : { "type": "string" },
"tags" : { "type": "string" }
}}}}'
我添加了两个简单的items
:
curl -XPUT 'localhost:9200/testindex/items/1' -d '{
"title": "This is a test title",
"body" : "This is the body of the java",
"tags" : "csharp"
}'
curl -XPUT 'localhost:9200/testindex/items/2' -d '{
"title": "Another text title",
"body": "My body is great and Im super handsome",
"tags" : ["cplusplus", "python", "java"]
}'
如果我搜索字符串java
:
curl -XGET 'localhost:9200/testindex/items/_search?q=java&pretty=true'
...它将匹配这两个项目。第一项将与body
匹配,另一项与tags
匹配。
如何避免在某些字段中搜索?在示例中,我不知道它与字段tags
匹配。但我希望保持tags
索引,因为我使用它们来获取聚合。
我知道我可以用这个来做:
{
"query" : {
"query_string": {
"query": "java AND -tags:java"
}},
"_source" : {
"exclude" : ["*.tags"]
}
}'
但是还有其他更优雅的方式,比如在映射中放一些东西吗?
PS:我的搜索始终是query_strings
和term
/ terms
,我正在使用ES 2.3.2
答案 0 :(得分:0)
如果您只想与某些字段匹配,则可以指定fields选项
{
"query_string" : {
"fields" : ["body"],
"query" : "java"
}
}
编辑1
您可以在映射中使用"include_in_all": false
param。检查documentation。查询字符串查询默认为_all
,因此您可以将"include_in_all": false
添加到不想要匹配的所有字段中,之后此查询只会查找正文字段
{
"query_string" : {
"query" : "java"
}
}
这有帮助吗?