如何在URI搜索中搜索多个字段 [英] How to search on multiple fields in URI Search
问题描述
我想使用URI Search(q =)在ElasticSearch中执行AND操作.我该怎么办?
I would like to perform an AND operation in ElasticSearch using the URI Search (q=). How do I do it?
如果我有以下文件:[{名称":测试1",发布":"2"},{名称":测试2",发布":"1"},{名称":"A",发布":" 1}]
If I have document like: [{ "name":"Test 1", "pub":"2"}, { "name":"Test 2", "pub":"1"}, { "name":"A", "pub":"1"}]
我想查询包含名称包含"Test"且pub等于"1"的文档.我该怎么办?
And I would like to query for documents containing with a name containing "Test" AND where pub equals "1". How do I do that?
谢谢!
推荐答案
假设您的文档如下所示:
Assuming your document looks like this:
{
"my_field": [
{ "name":"Test 1", "pub":"2"},
{ "name":"Test 2", "pub":"1"},
{ "name":"A", "pub":"1"}
]
}
my_field
的映射类似于以下内容的 nested
类型:
And the mapping of my_field
is of type nested
similar to this:
{
"mappings": {
"doc_type": {
"properties": {
"my_field": {
"type": "nested",
"properties": {
"name": { "type": "string" },
"pub": {"type": "integer" }
}
}
}
}
}
}
然后,您可以通过以下 nested
查询来查询索引并获取所需的文档:
Then you can query your index and get the expected documents with the following nested
query:
POST /_search
{
"query": {
"nested": {
"path": "my_field",
"query": {
"bool": {
"filter": [
{
"match": {
"name": "Test"
}
},
{
"match": {
"pub": 1
}
}
]
}
}
}
}
}
这篇关于如何在URI搜索中搜索多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!