Elasticsearch-不同字段的不同搜索字符串

时间:2018-07-07 19:36:05

标签: elasticsearch

是否可以在一个查询中使用不同的搜索字符串查询不同的字段? 例如:

{
  "query": [
    { "match": { "name": "Bob" }},
    { "match": { "title": "Awesome Title" }}]
}

其中“名称”和“标题”是文档的字段。 我知道这里有一个multi_match查询,但在那里查询了所有具有相同字符串的字段列表...

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此查询来搜索同时符合两个条件(afterSave()"name": "Bob")的文档。将"title": "Awesome Title"替换为索引名称。

<index_name>

插图:

(a)索引4文档

$ curl -XGET 'localhost:9200/<index_name>/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "should": [
          {
          "match_phrase": {
            "name": "Bob"
            }
          },
          {
          "match_phrase": {
            "title": "Awesome Title"
            }
          }
      ],
      "minimum_should_match": 2
    }
  }
}
'

(b)确认文档已被索引

# Doc 1 - Only "name" matches
$ curl -X PUT "localhost:9200/office/doc/o001" -H 'Content-Type: application/json' -d'
{
    "name" : "Bob",
    "title" : "Senior Staff",
    "description" : "Developing new products"
}
'

# Doc 2 - None of the criteria match
$ curl -X PUT "localhost:9200/office/doc/o002" -H 'Content-Type: application/json' -d'
{
    "name" : "Tom",
    "title" : "Marketing Manager",
    "description" : "Shows and events"
}
'

# Doc 3 - Only "title" matches
$ curl -X PUT "localhost:9200/office/doc/o003" -H 'Content-Type: application/json' -d'
{
    "name" : "Liz",
    "title" : "Awesome Title",
    "description" : "Recruiting talent"
}
'

# Doc 4 - Both "name" and "title" match - Expected in result
$ curl -X PUT "localhost:9200/office/doc/o004" -H 'Content-Type: application/json' -d'
{
    "name" : "Bob",
    "title" : "Awesome Title",
    "description" : "Finance Operations"
}
'

(c)运行查询。结果是一个符合两个条件的文档(带有$ curl 'localhost:9200/office/_search?q=*' # Output {"took":19,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":4,"max_score":1.0,"hits":[{"_index":"office","_type":"doc","_id":"o003","_score":1.0,"_source": { "name" : "Liz", "title" : "Awesome Title", "description" : "Recruiting talent" } },{"_index":"office","_type":"doc","_id":"o001","_score":1.0,"_source": { "name" : "Bob", "title" : "Senior Staff", "description" : "Developing new products" } },{"_index":"office","_type":"doc","_id":"o004","_score":1.0,"_source": { "name" : "Bob", "title" : "Awesome Title", "description" : "Finance Operations" } },{"_index":"office","_type":"doc","_id":"o002","_score":1.0,"_source": { "name" : "Tom", "title" : "Marketing Manager", "description" : "Shows and events" } ):

id=o004

(d)获取查询结果

$ curl -XGET 'localhost:9200/office/_search?pretty' -H 'Content-Type: application/json' -d'
    {
      "query": {
        "bool": {
          "should": [
              {
              "match_phrase": {
                "name": "Bob"
                }
              },
              {
              "match_phrase": {
                "title": "Awesome Title"
                }
              }
          ],
          "minimum_should_match": 2
        }
      }
    }
    '