在Elasticsearch v2.3.5中过滤以及multi_match查询

时间:2016-08-15 20:06:01

标签: elasticsearch

我是Elasticsearch和Nest的新手,所以请原谅我的无知。我想在Elasticsearch v2.3.5中使用过滤器和multi_match查询,但到目前为止我无法弄明白。我想一旦我让它为Elasticsearch工作,我应该能够将它映射到Nest。

以下是我的JSON数据结构:

{
   "contentID":1,
   "categoryID":0,
   "title":"...",
   "description":"...",
   "contentHtml":"...",
   "version":2,
   "parentContentID":0,
   "displayOrder":0,
   "freshdeskID":0,
   "isDraft":false,
   "isCommentingEnabled":false,
   "isArticle":false,
   "grandParentContentID":0,
   "isAnyParentDraft":false
}

及以下是我的工作搜索查询(没有任何过滤器):

POST contents/supportitem/_search?pretty=true
{
  "size": 150,
  "highlight": {
    "fields": {
      "contentHtml": {
        "fragment_size": 245
      }
    }
  },
  "_source": {
    "include": [
      "title",
      "contentID",
      "description",
      "thumbnailUrl",
      "isDraft",
      "isAnyParentDraft",
      "grandParentContentID"
    ]
  },"query": {
    "multi_match": {
      "type": "cross_fields",
      "query": "query typed by user",
      "tie_breaker": 0.3,
      "fields": [
        "title^1.1",
        "additionalContents^1.2",
        "contentHtml^1"
      ]
    }
  }
}

我想在搜索结果中只向用户显示以下内容:

grandParentContentID != 0 and
isDraft != false and
isAnyParentDraft != false

我尝试过不同的查询,但我无法弄清楚如何写这个。

一些不工作的查询,包括:

POST contents/supportitem/_search?pretty=true
{
  "size": 150,
  "highlight": {
    "fields": {
      "contentHtml": {
        "fragment_size": 245
      }
    }
  },
  "_source": {
    "include": [
      "title",
      "contentID",
      "description",
      "thumbnailUrl",
      "isDraft",
      "isAnyParentDraft",
      "grandParentContentID"
    ]
  },"query": {
    "multi_match": {
      "type": "cross_fields",
      "query": "Tile map server resources",
      "tie_breaker": 0.3,
      "fields": [
        "title^1.1",
        "additionalContents^1.2",
        "contentHtml^1"
      ]
    },"filtered": {
      "filter": {
        "bool": {
          "term": {
            "isAnyParentDraft": "false"
          }
        }
      }
    }
  }
}


POST contents/supportitem/_search?pretty=true
{
  "size": 150,
  "highlight": {
    "fields": {
      "contentHtml": {
        "fragment_size": 245
      }
    }
  },
  "_source": {
    "include": [
      "title",
      "contentID",
      "description",
      "thumbnailUrl",
      "isDraft",
      "isAnyParentDraft",
      "grandParentContentID"
    ]
  },"query": {
    "multi_match": {
      "type": "cross_fields",
      "query": "Tile map server resources",
      "tie_breaker": 0.3,
      "fields": [
        "title^1.1",
        "additionalContents^1.2",
        "contentHtml^1"
      ]
    },"filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "field": {"isAnyParentDraft": "false"}
            }
          ]
        }
      }
    }
  }
}

我得到"failed to parse search source. expected field name but got [START_OBJECT]"

我有这个工作,但我无法弄清楚如何添加更多过滤器:

POST contents/supportitem/_search?pretty=true
{
  "size": 150,
  "highlight": {
    "fields": {
      "contentHtml": {
        "fragment_size": 245
      }
    }
  },
  "_source": {
    "include": [
      "title",
      "contentID",
      "description",
      "thumbnailUrl",
      "isDraft",
      "isAnyParentDraft",
      "grandParentContentID"
    ]
  },
  "query": {
    "filtered": {
      "query": {
        "multi_match": {
          "type": "cross_fields",
          "query": "deleted",
          "tie_breaker": 0.3,
          "fields": [
            "title^1.1",
            "additionalContents^1.2",
            "contentHtml^1"
          ]
        }
      },
      "filter": {
        "and": {
          "filters": [
            {
              "term": {
                "isAnyParentDraft": "false"
              }
            }
          ]
        }
      }
    }
  }
}

我在下面提到了一些问题:

ElasticSearch with multi_match AND bool - 只有一个过滤器

Elasticsearch: multi_match no effect with filters

1 个答案:

答案 0 :(得分:2)

非常好的开始,你几乎就在那里:

POST contents/supportitem/_search?pretty=true
{
  "size": 150,
  "highlight": {
    "fields": {
      "contentHtml": {
        "fragment_size": 245
      }
    }
  },
  "_source": {
    "include": [
      "title",
      "contentID",
      "description",
      "thumbnailUrl",
      "isDraft",
      "isAnyParentDraft",
      "grandParentContentID"
    ]
  },
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "type": "cross_fields",
          "query": "deleted",
          "tie_breaker": 0.3,
          "fields": [
            "title^1.1",
            "additionalContents^1.2",
            "contentHtml^1"
          ]
        }
      },
      "must_not": [
         {
           "term": {
             "isAnyParentDraft": "false"
           }
         },
         {
           "term": {
             "isDraft": "false"
           }
         },
         {
           "term": {
             "grandParentContentID": 0
           }
         }
      ]
    }
  }
}