Вопрос

I am using PyElasticsearch (elasticsearch python client library). I am searching strings like Arvind Kejriwal India Today Economic Times and that gives me reasonable results. I was hoping I could increase weight of the first words more in the search query. How can I do that?

res = es.search(index="article-index", fields="url", body={
  "query": {
    "query_string": {
      "query": "keywordstr",
      "fields": [
        "text",
        "title",
        "tags",
        "domain"
      ]
    }
  }
})

I am using the above command to search right now.

Это было полезно?

Решение 3

Другие советы

split given query into multiple terms. In your example it will be Arvind, Kejriwal... Now form query string queries(or field query or any other which fits into the need) for each of the given terms. A query string query will look like this http://www.elasticsearch.org/guide/en/elasticsearch/reference/0.90/query-dsl-query-string-query.html

{
    "query_string" : {
        "default_field" : "content",
        "query" : "<one of the given term>",
        "boost": <any number>
    }
}

Now you have got multiple queries like above with different boost values(depending upon which have higher weight). Combine all of those queries into one query using BOOL query. http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html If you want all of the terms to be present in the result, query will be like this.

{
    "bool" : {
        "must" :  [q1, q2, q3 ...]
    }
}

you can use different options of bool query. for example you want any of 3 terms to present in result then query will be like

{
    "bool" : {
        "should" :  [q1, q2,q3 ...]
    },
    "minimum_should_match" : 3,
}

theoretically:

  1. split into terms using api
  2. query against terms with different boosting
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top